# Correct method for drawing waveforms

I need to draw waveforms for biometric data like ECG and EEG signals. When I have more samples than pixels at the X-axis, I need to draw a vertical line between the MIN and MAX sample-value for that period.

Now I was wondering, suppose the signal is highly volatile, and the MAX for the next point is lower than the MIN from the previous point, there will be a 'gap' in the waveform.

What is the correct way to connect these points? When drawn using standard polyline routines the pen will draw from the last position (MAX) to the start of the new line (MIN), which is a much larger distance, causing the signal look thicker than it would be if I just connected the previous MIN to the new MAX (if the trend is downwards), or the previous MAX to the new MIN (if the trend is upwards).

Is there a standardized way to do this, or is it just a matter of taste?

-
dsp.stackexchange.com/q/184/29 is similar, but trying to show the true density of the waveform rather than just the max and min. Can you post some plots? –  endolith Dec 1 '14 at 21:18

Remember that the signal is not the samples; the signal passes through the samples and exists between them, too. So the maximum value of one chunk cannot be less than the minimum value of the next; they can only be equal, as the signal passes from a lower sample in one chunk to a higher sample in the next chunk.

Plotting a solid line from the min of a chunk to the max sample value of a chunk is not a very accurate representation of the signal anyway (because of intersample peaks and not showing the density of the wave). If that's good enough of a representation for you, I'd just approximate it by drawing to the midpoint in this case:

find max of chunk 1
find min of chunk 2
if max(chunk 1) < min(chunk 2):
in column 1: draw line from min of chunk 1 to average(max(chunk 1), min(chunk 2))
in column 2: draw line from average(max(chunk 1), min(chunk 2)) to max(chunk 2)


If you want to get fancier, you could upsample the signal to get a better idea of the intersample peaks, and then do the same thing based on those.

And the best is to use brightness to show the density of the wave at different amplitudes, but that's significantly more complicated: https://flic.kr/p/7S8oHA

-
I like your idea about including density, but your expertise seems to be mainly around audio-plotting, and brainwaves look completely different: upload.wikimedia.org/wikipedia/commons/2/26/Spike-waves.png Would you think density could be usefull there too? –  Muis Dec 4 '14 at 6:38
That doesn't look completely different to me, it looks about the same. But from your description, I thought you were plotting "zoomed out" from this picture? Like there are multiple oscillations per horizontal pixel and you are showing only the maximum and minimum of each? Or you are plotting similar to what you linked to? –  endolith Dec 4 '14 at 14:49
Im plotting similar to what I linked, but still there are more datapoints than pixels in X. –  Muis Dec 5 '14 at 17:52
@muis well for waves like you've shown, I think plotting the density as lightness would just give a sort of "antialiased" look to the lines, which might be nice, but might not be with the effort –  endolith Dec 7 '14 at 6:06

You can probably use "error bar plots" for this. Error bar plots are like regular 2-D plots, but you can specify vertical bars above and below each plotted point. See section 4.12, page 259, in this PDF document for many examples: http://pgfplots.sourceforge.net/pgfplots.pdf

Matlab also supports error plots, with the errorbar command.

-
These are 512 Hz signals, it's impossible to plot them like "error bars" because the maximum width of each bar cannot exceed 1 pixel. –  Muis Dec 2 '14 at 18:19