Math of ECGs: Fourier Series

By Murray Bourne, 30 Mar 2010

I recently had a medical checkup that included an ECG (electro-cardiograph).

This is what my ECG looked like:

ECG
[Click the image for a larger view.]

How an ECG is done

ecg assembly ECG setup

(Left) Electrodes used for an ECG. Image source
(Right) Nurse performing an ECG. Image source

The electrodes are connected to various parts of your anatomy (chest, legs, arms, feet) and voltage differences over time are measured to give the ECG readout.

The horizontal axis of the ECG printout represents time and the vertical axis is the amplitude of the voltage.

ecg - reference pulse

Amplitude units are millivolts (mV) and on the graph, 1 mV = 10 mm high.

The time scale is 25 mm = 1 second (or 1 mm per 0.04 seconds on the graph).

So here’s my readout for Lead II, representing the voltage between the positive electrode on my left leg and the electrode on my right arm. Each thicker red vertical line represents a time of 1 second.

ECG II

Apparently (according to the doctor), this indicates my heart is quite healthy.

In more detail, the features of the repeated pulse we are looking at are as follows.

PQRST waves ECG
[Image source]

The P wave is caused by contraction of the right atrium followed by the left atrium (the chambers at the top of the heart).

The QRS complex represent the point in time when most of the heart muscles are in action, so has highest amplitude.

The T wave represents the polarization of the ventricles (the chambers at the bottom of the heart).

heart
Human heart showing atria and ventricles.
[Image by UCSD, source page no longer available]

Modeling the Heartbeat Using Fourier Series

A heartbeat is roughly regular (if it isn’t, it indicates something is wrong). Mathematically, we say something that repeats regularly is periodic.

Such waves can be represented using a Fourier Series.

Assumptions

In my case, my heart rate was about 70 beats per minute. For the sake of simplicity, I’ll assume 60 beats per minute or 1 per second. So the period = 1 second = 1000 milliseconds.

Also for simplicity, I will only model the R wave for this article. To get a more accurate model for the heartbeat, I would just need to do a similar process for the P, Q, S and T waves and add them to my model.

I observed that my R wave was about 2.5 mV high and lasted for a total of 40 ms. The shape of the R wave is almost triangular and so I could have used straight lines for my model, but these don’t give us a smooth curve (especially at the top – it must be continuously differentiable).

A better approach is to use a polynomial (where the ascending and descending lines are close enough to being straight), so my model is as follows (the time units are milliseconds):

f(t) = -0.0000156(t − 20)⁴ + 2.5

f(t) = f(t + 1000)

Explanation of the Model

The model is based on a quartic (power 4) since this will give me close to the shape I need (a parabola would be too broad). I’m using similar thinking to what I was doing in this article, where I move a curve around to where I want it.

The (t − 20) term comes from deciding the curve should start at (0,0), (which makes our lives easier), it will pass through (40,0) since the pulse is 40 ms long, and be centered on t = 20.

The "+2.5" comes from the fact the amplitude of the pulse is 2.5 mV.

The -0.0000156 comes from solving the following for a when t = 0.

a(t − 20)⁴ + 2.5 = 0.

The "f(t) = f(t + 1000)" part means the function (pulse in this case) is repeated every 1000 ms.

Graph of the Model

This is the graph of part of one period (the part above the t-axis from t = 0 to t = 40):

ECG model

Of course. this is just one pulse. How do we produce a graph that repeats this pulse at regular intervals?

This is where we use Fourier Series.

I’ll spare you all the details, but essentially the Fourier Series is an infinite series involving trigonometric terms. When all the terms are added, you get a mathematical model of the original periodic function.

To obtain the Fourier Series, wee need to find the mean value, a0, and 2 coefficient expressions involving n, an and bn which are multiplied by trigonometric terms and summed for n = 1 to infinity.

Mean Value Term

a0 is obtained by integration as follows (L is half of the period):

a_{0}=\dfrac{1}{L}\int_{-L}^{L}f(t)dt

=\dfrac{1}{500}\int_{-500}^{500}f(t)dt

=\dfrac{1}{500}\int_{0}^{40}(-0.0000156(t-20)^{4}+2.5)dt

=0.16

First Coefficient Term, an

Next, we compute an:

a_{n}=\frac{1}{L}\int_{-L}^{L}f(t)\cos\frac{n\pi{t}}{L}dt

=\dfrac{1}{500}\int_{-500}^{500}f(t)\cos\frac{n\pi{t}}{500}dt

=\dfrac{1}{500}\int_{0}^{40}(-0.0000156(t-20)^{4}+2.5)\cos\frac{n\pi{t}}{500}dt

The answer for this integral is pretty ugly. I’ve included it in the PDF solution.

Second Coefficient Term, bn

Now for bn:

b_{n}=\frac{1}{L}\int_{-L}^{L}f(t)\sin\frac{n\pi{t}}{L}dt

=\dfrac{1}{500}\int_{-500}^{500}f(t)\sin\frac{n\pi{t}}{500}dt

=\dfrac{1}{500}\int_{0}^{40}(-0.0000156(t-20)^{4}+2.5)\sin\frac{n\pi{t}}{500}dt

Once again, I have spared you from the full details.

Finally, we put it all together and obtain the Fourier Series for our simple model of a heart beat:

f(t)=\dfrac{a_{0}}{2}+\sum_{n=1}^{\infty}a_{n}\cos\dfrac{n\pi{t}}{L}+\sum_{n=1}^{\infty}b_{n}\sin\dfrac{n\pi{t}}{L}

f(t)=\dfrac{0.16}{2}

+\sum_{n=1}^{\infty}\left(\dfrac{1}{500}\int_{0}^{40}(-0.0000156(t-20)^{4}+2.5)\cos\frac{n\pi{t}}{500}dt\right)\cos\dfrac{n\pi{t}}{500}

+\sum_{n=1}^{\infty}\left(\dfrac{1}{500}\int_{0}^{40}(-0.0000156(t-20)^{4}+2.5)\sin\frac{n\pi{t}}{500}dt\right)\sin\dfrac{n\pi{t}}{500}

When we graph this for just the first 5 terms (n = 1 to 5), we can see the beginnings of a regular 1-second heart beat.

ECG model n=5

The above graph shows the "noise" you get in a Fourier Series expansion, especially if you haven’t taken enough terms.

Taking more terms (this time, adding the first 100 terms) gives us the following, and we see we get a reasonable approximation for a regular R wave with period 1 second.

ECG model

I added the T wave for this next model (in blue).

ECG model

I used a parabola for the T wave because the shape of the T wave is broader than the shape of the R wave.

We could keep going, adding the P, Q and S waves to get an even better model.

See the complete solution (up to the T wave, created using Scientific notebook) here:

Model of Human Heartbeat (PDF)

What have we done?

We have taken a single spike representing one R wave of my heartbeat. We then found a formula that repeats our spike at regular time intervals. The Fourier Series (an infinite sum of trigonometric terms) gave us that formula.

Finally, we added the T wave, using the same theory as before.

Fourier Series is very useful in electronics and acoustics, where waveforms are periodic.

For more on Fourier Series go to:

Fourier Series

Don’t miss the section on how Fourier is used to create Digital Audio, in

Applications – the Fast Fourier Transform

See the 18 Comments below.

Share this page

18 Comments on “Math of ECGs: Fourier Series”

  1. vonjd says:

    Just to let u know: “The answer for this integral is pretty ugly. I’ve included it in the PDF solution.” – brings up a 404

  2. Murray says:

    Oops – the second link was fine but I forgot to fix the first. Thanks for your feedback.

  3. nahum says:

    i liked the easy-to-understand English.It made it not boring.

  4. sharma k k says:

    it is quite easy to under stand a mathematical approach.

  5. gagangc says:

    Sir,

    i have modelled R curve in different manner
    f(t)=(2.5)*(sin(pi*t/40)) for 0<t<40
    = 0 for 40<t<1000

    and fourier series upto two terms is

    f(t)=(-0.06366)+(0.12613*(cos(pi*x/500)))+(0.12258*(cos(pi*x/250)))+(0.01593*(sin(pi*x/500)))+(0.03147*(sin(pi*x/250)))

    when i plotted it i got satisfactory answer as i considered only two terms.

    please check my answer and let me know if i am correct.

    it took lot of effort and time from me to calculate all coefficients by hand.

  6. Murray says:

    Hi gagangc. This looks good to me! Good on you for churning away with this.

  7. biofredo says:

    did you calculate de PRS equations?

  8. gagangc says:

    Sir,

    i have a doubt regarding fourier transform of rectangular function.If FT indicates frequency contents of time domain signal,then FT of rect function is sinc function which have infinite frequencies.Does this mean a simple rect function has infinite frequencies??

  9. Murray says:

    Hi gagangc. The rectangular function is a single pulse, so it’s not relevant to talk about infinite frequencies. These 2 sources may help:
    Wikipedia’s article
    Wolfram Demonstrations (requires a plugin)

  10. Dan Adam says:

    Hi Murray,
    I’m interested to know how you have generated your last 3 graphs. If possible could you please email me your matlab codes. My email address is [hidden for protection from spam]

    I’m waiting for your reply.
    thanks

  11. Murray says:

    @Dan: Two of the last 3 graphs are based on the formulas I developed in the article (I did 5 terms for the first, then added 100 terms for the next.)

    The last one adds the T-wave using the same process (I estimated the “blip” above the horizontal axis, then built it into the Fourier series. I suspect the details are hidden in some archive by now.

    I’m using Scientific Notebook for these graphs. It allows for graphing of a summation notation expression.

    Sorry it wasn’t so helpful!

  12. EKG’nin Matemati?i : Fourier Serileri – Yazd?klar?m Beni Yans?t?r says:

    [...] http://tr.wikipedia.org/wiki/Elektrokardiyografi http://www.intmath.com/blog/math-of-ecgs-fourier-series/4281…]

  13. Kate says:

    Hello! I was curious if you could post your findings for the P, Q, and S waves? I have done it myself, but would love to check if I did it correctly. Many thanks!!

  14. Murray says:

    @Kate: As I mentioned in the article, I stopped after the T wave.

    Basically, in this work, if it looks right, it is (very likely) right! Have you graphed your solution? That usually tells you right away whether you are on the right track. Use any of the online graphers (like Desmos) to graph the first few terms for you.

  15. Kate says:

    No, I havent graphed it. I was planning on using MatLab to get a graph.
    Quick question — when solving a(t-20)^4+2.5=0 where you got -0.0000156 — what is a? Or, could you explain this process in more detail? And, why use a quartic for the R wave and a power of 2 when you solved for the T wave? Thank you!!

  16. Murray says:

    @Kate: I’ve written some additions to the article which explain your questions. (You may need to refresh the page to see the changes.)

  17. hari says:

    Sir,

    Can u clearly explain me how did u get this function f(t)
    as shown below. it wil be very helpful for me if u clarify my doubt.
    f(t) = -0.0000156(t − 20)⁴ + 2.5

    f(t) = f(t + 1000)

    thanq

  18. Murray says:

    @Hari: Did you go to the article I linked to, http://www.intmath.com/blog/how-to-draw-y2-x-2/2301?

    I’m using similar thinking to arrive at the function you are asking about.

    The minus at front is there to turn the curve “upside down” (so it is n-shaped and not u-shaped).

    The “-20″ is there because I need to move it over to the right by 20 milliseconds.

    It’s power 4 because it’s more narrow than a parabola (which would be power 2).

    The “+2.5″ is to move the curve up by 2.5.

    I just experimented using this graph facility until I was satisfied.

    The f(t) = f(t + 1000) just means “repeat the curve every 1000 milliseconds”.

    Hope that helps.

Leave a comment