MATLAB: High Pass Butterworth Filter increases unwanted frequencies using Filtfilt

butterworthfftfilterfiltfiltfrequencyhigh passhighpassMATLAB

Hello,
I am trying to high-pass filter some data using filtfilt. For my application, I only need to effectively remove data close to DC, so I am using a fairly low frequency cutoff.
However, I have noticed that my implementation (butterworth filter using filtfilt) is causing increased spectral content around my cutoff.
Does anyone know why this is happening?
I am thinking it may be a result of having a small ensemble size (50 samples) or possible a filter ripple effect (?)
For example:
N = 50;
data = randn( N,1);
prf = 500;
cutoff = 2;
[B A ] = butter(2, cutoff/ (prf / 2), 'high');
data2 = filtfilt( B,A, data);
figure();
plot( linspace(-prf/2, prf/2, 5000), fftshift( abs( fft( data, 5000))), 'k')
hold on;
plot( linspace(-prf/2, prf/2, 5000), fftshift( abs( fft( data2, 5000))), 'b')
Thanks!

Best Answer

First, if you want to eliminate the D-C value, simply subtract the mean of your signal from your signal. You do not need to filter it. (Since you are using the randn function, the mean should already be close to 0.) Butterworth filters are popular because they are easier to design than others. However, using the Signal Processing Toolbox functions, this is no longer a significant problem. Elliptical filters are much more efficient. See the documentaion on ellip (link) and related functions (ellipord, zp2sos, and others) as well.
Second, an order 2 filter of any design is not likely to be very efficient. If you have R2018a or later, use the highpass (link) function, although the related bandpass function is usually a better option, since highpass filters amplify noise. If you use the second ‘d’ output from highpass (or any of the others), use it with filtfilt, not filter.