# Down sample incoming stream of data to plot a dynamic graph

I am working on a module where I need to plot ECG streaming data on a graph. This graph has configurable speed i.e. plot 1 heartbeat data sample within say 50mm,25mm, etc. For this, I am required to down sample based on the screen resolution and plot the same.

I am using Qt QML for plotting. What is the exact algorithm to achieve this? I don't have a clue on what exactly I have to google

• What have you tried for now? What you're trying to do doesn't seem too complex. Have you tried to decompose the problem in small simple blocks? Like the down sampling for example – Florent Jul 27 '17 at 10:53
• @FlorentEcochard I need some pointers on this down sample part. I have never worked in this area and so I am just blinded here on where to begin with. – Xavier Geoffrey Jul 27 '17 at 11:12

# Downsampling

Let's define our downsampling factor : $$r = \frac{F_s'}{F_s}$$

Where $F_s$ is the original sampling frequency and $F_s'$ the reduced one.

If $F_s$ is a multiple of $F_s'$ (i.e. $1/r$ is an integer), then the dowsampling is straightforward : you just take one sample out of $1/r$. For example, if you want to half your sampling frequency, just discard every $2^{nd}$ sample and that's all (well not really, see next paragraph).

However, if that's not the case, you're going to have to interpolate between the samples. To make it simple, interpolating means trying to guess the value of a sample by knowing the value of the surrounding samples. You brain does interpolation all the time : when you see several points forming a line you instinctively draw the line in your head. Several types of interpolation exist, the more simple beeing the less precise. It usually depends on your application.

# But there's more...

You've maybe heard of Shannon-Nyquist sampling theorem, which states that to sample a signal, you have to have a sampling frequency at least twice as big as your signal's highest harmonic. If you don't, it's going to cause something called aliasing. I won't explain this theorem here because there is plenty of information available everywhere.

But keeping this theorem in mind, you can probably understand that downsampling your signal might cause aliasing if you're not careful. The simple solution to that is to filter the signal prior to downsampling, at a cutoff frequency of $\frac{F_s'}{2}$. The steeper the filter's transition band, the less aliasing.

# Back to your application

From what I understood, you have a signal that you want to map to a screen. For that, you need to downsample the signal to your screen's horizontal resolution. Let's call this resolution $H_x$ and your signal's sampling frequency $F_s$. For the sake of the example, let's fix $H_x=240\text{px}$ and $F_s=5\text{kHz}$.

You also have a time parameter (let's call it $t_r$) which sets the "zooming" of your scope, ie how long of a signal corresponds the length of the screen. Let's set it to $t_r=100\text{ms}$ (Disclaimer : I have absolutely no idea about the frequency of ECG so this is completely arbitrary).

That gives you : $$F_s' = \frac{H_x}{t_r} = \frac{240}{0.100}=2400\text{Hz}$$ Note that that is not a multiple of $F_s$.

Applying what I said before, you have : $$r = \frac{F_s'}{F_s} = \frac{2400}{5000} = 0.48$$

So first, you filter your signal with a brickwall at $f_c=\frac{F_s'}{2} = 1.2\text{kHz}$, then you downsample it by a factor of $0.48$ using interpolation.

Another thing to consider is the vertical resolution. For that it is a bit different as you are not downsampling the signal but quantizing it. However, the process is similar and I think you can extrapolate without our help.

• Thanks a lot. :-) Got some idea on how to start now. One thing, the timing is like the distance right? Because that's how I am understanding it. In that case, since I need to plot one heart beat within 50mm I have to use that right? – Xavier Geoffrey Jul 29 '17 at 12:45
• Well you're mapping your time axis on a screen, which means you're using a conversion factor. Remember when you used to plot graphs in physics class (or whatever), for example the tension of a battery during time? Think of it the same. And more in terms of pixels than distance. Your time unit (your sampling period) is mapped to a spatial unit (one pixel). The mapping coefficient depends on how long of the signal you want to plot on the screen – Florent Jul 29 '17 at 12:55
• Yeah, which means I need as many samples as the pixels that falls within that distance, considering 1 sample will be 1 pixel. – Xavier Geoffrey Jul 29 '17 at 15:08
• No re-read my question, especially the part about interpolation. Of course, if you want to plot 5s of data but only have 3 seconds it's another question. Edit: I might have misunderstood your comment. Your statement is true after downsampling/interpolation – Florent Jul 30 '17 at 6:30