Skip to content
Yanis André
FRRésumé ↓
← Back to home

S8 project · ENSEIRB-MATMECA

Arrhythmia detection using Legendre polynomials

Classifying heartbeats with 13 Legendre coefficients. The project’s 98.6% score mostly measures how well one patient is recognised; on patients never seen, 9 premature ventricular contractions out of 10 are found, with many false alarms.

Period
January to May 2026, 180 hours
Role
Lead on the biomedical strand within a team of seven, supervised by a professor; evaluation redone afterwards, outside the team, on patients never seen
Skills
  • Biomedical signal processing
  • Supervised classification
  • Evaluation on unseen patients
Tools
MATLAB · Python · scikit-learn · Orthogonal polynomials · PCA · MIT-BIH
9 in 10
premature ventricular contractions found in patients never seen, with many false alarms
98.6 → 64⁠–⁠69%
the project’s score, then balanced accuracy with the same definition of an anomaly, on patients never seen
44
patient recordings: 22 to learn from, 22 to test on
13 numbers
per beat, instead of 181 samples

Context

STOCHA-FORGE is a MATLAB environment for signal modelling, built by seven students, me included. It was validated on two applications: the S&P 500 index, handled by other team members, and the detection of abnormal heartbeats, my strand. In it I did the classification, the Legendre / Chebyshev comparison, a fetal-ECG trial and three modules of the MATLAB application.

The question: can an abnormal beat be recognised from a few numbers, rather than with a neural network?

An electrocardiogram (ECG) records the heart’s electrical activity: a series of beats with similar shapes. I isolate each one with a window of 181 samples centred on its R peak, the tall, narrow spike.

A heartbeat
−1−0.500.51time (ms, 0 = R peak) and sample numbernormalised amplitude (no unit)−250 mssample 0−125 mssample 450 mssample 90125 mssample 135250 mssample 180P wave (faint)T waveR peakQRS complexS wavewindow of 181 samples: ±250 ms around the R peak

A real normal heartbeat (record 215 of the MIT-BIH database, MLII lead, 360 samples per second), scaled between −1 and 1. I only name what can be seen: the P wave is faint on this beat and there is no marked Q wave, so I do not label one. The grey shading covers the QRS complex, delimited by a simple rule (as long as the signal stays away from its baseline), not by hand.

The data come from MIT-BIH, a public database where cardiologists annotated every beat; I use 44 recordings. A premature ventricular contraction is a beat triggered too early by the ventricles. A bundle branch block is a delay in electrical conduction that distorts the beat.

Method

I normalise each window between −1 and 1, then project it onto Legendre polynomials: reference curves of degree 0, 1, 2 and so on. The projection gives one coefficient per polynomial. With degrees 0 to 12, a beat is described by 13 numbers instead of 181.

Rebuild a heartbeat

Pick a real heartbeat, then move the slider. The computation is redone in your browser, from the 181 points of the beat.

N = 12

N is the highest polynomial degree kept. The school project used N = 12, that is 13 coefficients.

Reconstruction error
68.5%
relative gap between the two curves
Values kept
13 of 181
  • Real beat
  • Reconstruction (N = 12)
−1−0.500.51−250−1250125250time around the R peak (ms)normalised amplitude

With 13 coefficients out of 181 values, the reconstruction error is 68.5%. It only gives the slow outline of the beat. The most marked point of the beat has not emerged yet.

For comparison, at N = 12 the mean error over a random sample of training beats is 39.8%.

The coefficients

Each bar is one coefficient, one per polynomial degree n. Solid bars are kept, hollow bars are dropped.

  • kept (n ≤ N)
  • dropped (n > N)
−0.50−0.250.000.250.500510152025polynomial degree ncoefficient value
Coefficients kept
Degree nCoefficient
0-0.123
10.228
20.033
3-0.087
4-0.184
5-0.209
6-0.201
70.171
80.350
9-0.097
10-0.250
110.155
120.209

Normal beat, record 215, N = 12: 13 coefficients out of 181, reconstruction error 68.5%.

The tool also shows what these numbers do not do. At N = 12, the reconstruction error is about 40% on average over training beats. The QRS peak is narrow, and degree-12 polynomials do not follow it closely. So I do not claim that 13 coefficients give the beat back. I show further down that they are enough to classify about as well as the 181 raw samples. Try the atrial premature beat: its shape is almost that of a normal beat. That matters later.

Results

Sensitivity is the share of anomalies found, specificity the share of normal beats recognised, balanced accuracy their average (50% for chance). Raw accuracy misleads when a class is rare: always answering “normal” is enough to make it high.

In the project, a KNN (it classifies a beat from its neighbours) reaches 98.6% on 881 test beats drawn at random from four patients.

On the left, a healthy beat and a premature contraction overlaid; on the right, a PCA projection of the beats of the school project’s four patients, anomalies and normal beats as two clouds
Left, a healthy beat and a premature contraction. Right, a PCA projection (reduction to two axes) of the beats of the project’s four patients. The separation you see is largely patient 111, who supplies almost all the anomalies, against the other three.

This score misleads. Patient 111 supplies 96.5% of the anomalies and has no normal beat. A random draw puts beats of the same patient on both sides. A rule that answers “anomaly” if the beat comes from patient 111 scores 98.1%.

So I redid the evaluation on records never seen (split of de Chazal et al., 2004): 22 to learn from, 22 to test on, none in common (one exception: 201 and 202 come from the same person). Settings are chosen by patient-wise cross-validation, on the training records only: one group of patients is set aside in turn for validation, and the test is never touched.

Results explorer

Three tabs: the school project’s figure, what happens on patients never seen before, then the choice of the number of coefficients.

The school project shuffled the beats of four patients (100, 101, 105 and 111), then set a share aside for testing: 881 beats, as many normal as abnormal, drawn at random (seed 42).

The project’s two classifiers

KNN (k-nearest neighbours): 98.6% accuracy

True class / PredictedPredicted: abnormalPredicted: normal
True class: abnormal448442 98.7%detected6 1.3%missed
True class: normal4336 1.4%false alarms427 98.6%recognised as normal

SVM with RBF kernel: 98.5% accuracy

True class / PredictedPredicted: abnormalPredicted: normal
True class: abnormal448438 97.8%detected10 2.2%missed
True class: normal4333 0.7%false alarms430 99.3%recognised as normal

In each cell: the number of beats, then the share of the row (the true class).

The beats of the four patients

PatientNormalAbnormalShare of abnormal beats
1002,237341.5%
1011,85930.1%
1052,526411.9%
11102,12496.5%

96.5% of the abnormal beats come from a single patient, number 111, who has no normal beat at all. The rule “abnormal if and only if the beat comes from patient 111” reaches 98.1% accuracy on average, from 97.3% to 99.0% depending on the split (20 draws), against 98.6% for the KNN. This score mostly measures how well one patient is recognised.

On the abnormal beats of the three other patients (3.5% of the abnormal beats), the KNN is right only 77.3% of the time and the SVM 46.2% (summed over 20 draws).

It is not a lucky draw: over 21 seeds, the original script’s KNN gives 98.6% to 99.4%. The score is stable; what it measures is the problem.

What this does not tell you: this score says nothing about how the classifier does on a patient it has never seen. That is the next tab.

Tab: The school project.

What holds. On patients never seen, for premature ventricular contractions against normal beats (bundle branch blocks count as normal), 87.5% (KNN) to 93.4% (SVM, support vector machine) of the contractions are found, about 9 in 10.

What does not hold. With the project’s broad definition, “any anomaly”, balanced accuracy drops to 63.8% (KNN) and 69.3% (SVM). Part of the failure comes from anomalies that show in the rhythm, not in the shape, such as atrial premature beats (too early, born in the atria): my pipeline throws the rhythm away.

What it costs.

  • Many false alarms: contractions are only 7% of the test beats, so wrongly flagging 9 to 13% of normal beats is enough to outnumber the true detections.
  • A large spread: from 10% to 100% sensitivity depending on the patient and the classifier (9 patients with 20 or more contractions).
  • One random draw, 22 test patients: gaps of a few points prove nothing. This is not a clinical device.

How many coefficients? N = 12 is not an optimum: another order does better by 3 to 7 points in cross-validation, in the same direction in 4 or 5 folds out of 5, but less than the spread between folds, the groups of patients set aside in turn (about 30 to 45 points). For contractions, five coefficients do at least as well as thirteen. The 13 coefficients are worth about the 181 raw samples (in cross-validation, the raw samples do from 4.2 points worse to 1.3 points better), except on the test patients for the broad definition: the raw SVM does better there, 75.8% against 69.3%.

Legendre or Chebyshev against noise

In comparaison_L_T.m, I compared two polynomial families against noise. I take three test signals, add white noise at nine levels, reconstruct, and measure the error over 80 draws. The signal-to-noise ratio, in decibels, is higher when there is less noise. The figures are those of my original script, rerun as it was under MATLAB.

  • Legendre
  • Chebyshev
  • band: ± one standard deviation across trials

exp(x)

0.1%1%10%100%−15−551525signal-to-noise ratio (dB)relative error (log scale)

sin(π x)

0.1%1%10%100%−15−551525signal-to-noise ratio (dB)relative error (log scale)

Composite

0.1%1%10%100%−15−551525signal-to-noise ratio (dB)relative error (log scale)

At 25 dB, Legendre’s error against Chebyshev’s: exp(x) 0.36% against 7.4%; sin(π x) 0.71% against 0.68%; Composite 12.0% against 13.1%.

For sin(π x) and Composite, the two families differ by at most 8.4% (in relative terms): a gap of a few percent is not significant, because the script sets no seed and the means move by a few percent from one noise draw to another. For exp(x), the gap exceeds 10% from −5 dB (in relative terms).

Relative reconstruction error of three noisy test signals, against the signal-to-noise ratio (the higher it is, the less noise). 80 trials per noise level: the line is the mean, the band one standard deviation across trials. Logarithmic scale, the same for the three charts.

On exp(x), the Legendre error keeps falling as the noise falls, while the Chebyshev error levels off around 7%. On the other two signals, the two families are within a few percent of each other. These are smooth test signals, not heartbeats: I do not conclude that Legendre classifies better.

What was redone after the project

The patient-wise split and the choice of settings come after the project, outside the team.

  • Kept. The pipeline rewritten in Python. Its coefficients match MATLAB’s up to numerical rounding, and my script rerun gives 98.6% and 98.5% again.
  • Changed. Split and settings by patient, metrics suited to a rare class.
  • Two mistakes in my scripts. The window was shifted by one sample (2.8 ms at 360 Hz) from the R peak, and the K of the KNN was chosen on the test set. I kept them to recover my figures, then corrected them. The second did not change the 98.6%.

One command replays the whole study. The code is available on request.

What I take from it

A score is worth only its protocol: the same definition of an anomaly falls from 98.6% to 64–69% when the protocol changes and the test uses patients never seen. A compact representation can also be enough to classify without being enough to reconstruct: 13 coefficients leave about 40% of reconstruction error, and still classify about as well as 181 samples. Finally, I learned to work in a team of seven, with work packages, a schedule and a shared interface, and to present my contribution to classmates working on other strands.

Data and references

Data: MIT-BIH Arrhythmia Database, hosted by PhysioNet, under the Open Data Commons Attribution License v1.0. Moody GB, Mark RG. The impact of the MIT-BIH Arrhythmia Database. IEEE Eng in Med and Biol 20(3):45-50 (2001).

Patient-wise split: de Chazal P, O’Dwyer M, Reilly RB. Automatic classification of heartbeats using ECG morphology and heartbeat interval features. IEEE Trans Biomed Eng 51(7):1196-1206 (2004).

Code access

Public repository

Links

Next projectDecoding ADS-B messages from radio recordings