MATLAB: Question about operation of ‘filtfilt’ MATLAB command.

filterfiltfilt

Hi folks,
This might seem as much of a signal processing question as a MATLAB one. Recently while doing work some work I came across a problem while working with some large datasets. I found that when downsampling the data before filtering, like this arbitrary example: data(1:downsample:end), that I didn't get an expected response. Afterwards I then downsampled after filtering, like so: filtered_data(1:downsample:end) and got an expected response.
Therefore it seems as though it matters when you downsample, which I didn't realize or understand a the time.
So for the sake of this example, to make the question simple I made two vectors in Matlab. One a 90 element matrix, m=1:90.
Then another, a 45 element matrix with every second sample from 'm' missing, n=1:2:90.
Now using arbitrary filter coefficients 'a' and 'b', I filtered both datasets.
filter1= filtfilt(b,a,m); filter2=filtfilt(b,a,n);
Now, I would have expected every element of'filter2' to be identical to every second element of 'filter1' i.e. filter2(2)=filter1(3), however that was not the case. For example, the first four elements of each filtered data set were: filter1 = [4.1776, 4.8390, 5.5221, 6.2264] and filter 2 = [10.0973, 11.3274, 12.5811, 13.8564].
I was wondering whether anybody perhaps knew why every element of filter 2 was not equal to every second element of filter 1, or could point me in the direction of further reading. To this point, I have not managed to find an answer.
Thanks a lot!

Best Answer

The problem is not filtfilt but using the same filter on a signal it was not designed for. If you used the filter designed for the original signal on the downsampled signal, the filter output will change, regardless of the filtering algorithm (such as filter or filtfilt) used. Discrete filters have to be designed for the signals they are expected to filter.
The reason is straightforward. The filter passbands and stopbands exist on the interval (0,pi), where pi is defined as the Nyquist frequency, equal to half the sampling frequency. When you change the sampling frequency by downsampling the signal first, you change the filter passbands. So for example if you designed a lowpass filter with a stopband of 0.5*pi with a sampling frequency of 1000 Hz, the stopband will be 250 Hz (half the Nyquist frequency of 500 Hz). If you then use the same filter to filter a signal with a sampling frequency of 5000 Hz, the filter will now have a stopband of 1250 Hz (half the Nyquist frequency of 2500 Hz).
So you either have to completely re-design your filter to be appropriate for the downsampled input, or downsample after filtering. I would downsample after filtering, since discrete filters tend to be more efficient at higher sampling frequencies, although the filters are necessarily longer.