5
$\begingroup$

I have EEG data recorded with 128Hz sampling rate. As my goal is to reduce the amount of data (and maybe noise), I want to downsample the data to 64Hz (I am only interested in range 0.5 - 30Hz).

I would perform downsampling (64Hz) and use a bandpassfilter (0.5 - 30Hz).

Now my question is, what is the best order to perform those two steps and why? Which impacts will these methods have to the quality of my data?

$\endgroup$
2
  • 1
    $\begingroup$ It's also possible to combine the two using a polyphase filter. This can save some compute overhead by not computing values that will be discarded. $\endgroup$ Commented Dec 21, 2016 at 22:35
  • $\begingroup$ @alex.forencich Good point, I will consider that. Are there any disadvantages for this kind of filter? $\endgroup$
    – ppasler
    Commented Dec 22, 2016 at 8:32

2 Answers 2

14
$\begingroup$

You need to filter first and then downsample. Otherwise, you will run into aliasing problems. I.e. frequencies that are above 30 Hz will create images within your frequencies of interest. You can consider the little script below to compare both methods:

Fs = 128.0
t = np.arange(0, 10, 1/Fs)
signal = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*50*t)
sigma2 = 0.5

rx = signal + np.sqrt(sigma2) * np.random.randn(len(signal))

downsampled = rx[::2]
(b1, a1) = scipy.signal.butter(6, 30/(Fs/4))
down_and_filtered = scipy.signal.lfilter(b1, a1, downsampled)

(b2, a2) = scipy.signal.butter(6, 30/(Fs/2))
filtered_and_down = scipy.signal.lfilter(b2, a2, rx)[::2]
t2 = t[::2]


plt.figure(figsize=(20,20))
plt.subplot(221)
plt.plot(t, rx)
plt.plot(t, signal)
plt.xlim((1, 1.3))
plt.title('Received signal')

plt.subplot(222)
plt.plot(t2, down_and_filtered)
plt.plot(t2, filtered_and_down)
plt.title('Compare of both methods in time domain')
plt.xlim((1,1.3))

f = np.linspace(-Fs/2, Fs/2, 4*len(t))
plt.subplot(223)
plt.plot(f, np.fft.fftshift(abs(np.fft.fft(rx, 4*len(t)))))
plt.title('RX spectrum. Note the two peaks, one is interference, having frequency over 30Hz')

f2 = np.linspace(-Fs/4, Fs/4, 4*len(t2))
plt.subplot(224)
plt.plot(f2, np.fft.fftshift(abs(np.fft.fft(down_and_filtered, 4*len(t2)))), '-o')
plt.plot(f2, np.fft.fftshift(abs(np.fft.fft(filtered_and_down, 4*len(t2)))))
plt.title('Spectrum of both alternatives. NOte the blue curve has a wrong component')

program output

In this script you have a signal that is composed of two sines of frequency 10 and 50Hz plus some noise. In the FFT of the full signal, both spectral lines clearly occur. Now, what you want to have after your signal processing is only one spectral line at 10Hz (because the 50Hz signal should be filtered out). HOwever, if you downsample first, the 50Hz wave is mirrored to $14Hz=64Hz-50Hz$ and cant be filtered out subsequently. Hence, you need to do the filtering before and then downsample your signal.

$\endgroup$
8
  • $\begingroup$ Thank you! Your explanation and the plots makes it easy to understand. $\endgroup$
    – ppasler
    Commented Dec 20, 2016 at 13:57
  • 2
    $\begingroup$ To avoid getting angry comments from referees, @ppasler should also carefully think what kind of filter to use. For example, for some neuroscience research questions, it would be catastrophic if the filter is not zero-phase. $\endgroup$
    – mmh
    Commented Dec 20, 2016 at 18:59
  • 1
    $\begingroup$ ... and if you are hoping that the low-pass takes care of the line noise at 50 or 60 Hz as well, please carefully check whether the filter provides enough attenuation at those frequencies. $\endgroup$
    – mmh
    Commented Dec 20, 2016 at 19:00
  • $\begingroup$ IMO proper downsampling includes low-pass filtering. $\endgroup$ Commented Dec 21, 2016 at 0:19
  • 1
    $\begingroup$ @leftaroundabout Absolutely - the point is to remove the part of the signal for which the downsampling does not work at all, and creates only garbage. Unfourtunately, the garbage only looks like random noise in the best case, but practically it is well structured. One common example is the Moiree pattern. It consists of nothing as this garbage. $\endgroup$ Commented Dec 21, 2016 at 4:17
4
$\begingroup$

Since I can't comment on this particular site I'd say this, consider the following before you do what you're trying to do.

Due to the Nyquist law you want your sampling frequency to be that of the DOUBLE of the maximum frequency your analog signal has. If you downsample to 64 hz that means you'll only be able to see signal data up to 32 Hz. EEG contains the gamma band above that frequency in the 30~50 range, which is WHY it is sampled at 128 Hz to begin with. You need at least 100 Hz to get all of EEG. That is of course, unless you want to get rid of gamma band.

Example: http://journal.frontiersin.org/article/10.3389/fnhum.2013.00056/full

Site admin: Feel free to move this to a comment in the original question or given answer. Thank you!

$\endgroup$
1
  • $\begingroup$ Good point, I am aware of this fact. I am not interested in the gamma band, so it's no problem to remove it. $\endgroup$
    – ppasler
    Commented Dec 21, 2016 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.