Serious Problem with FiltFilt Function
55 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I designed a Bandpass Filter using the filterDesigner app. It is an 18th order IIR filter.
Then I export the filter both as df2sos object and "SOS and G" matrices.
Afterwards, I try filtering my data with:
y = filtfilt(Hd,x);
It gives the error: Not enough input arguments.
Well. Why don't I use:
y = filtfilt(Hd.sosMatrix,Hd.scaleValues,x);
% or
y = filtfilt(SOS,G,x);
These do not work for my filter; they result in unstable filters. Even though my originally designed filter is stable, they give unstable results. Here is the MATLAB page for this issue.
So, how to use FiltFilt function with my df2sos object or my "SOS and G" matrices, without getting an unstable result? I'd really appreaciate some help.
Regards,
Berk
Additional Details:
Here is the code to generate my filter object:
Fstop1 = 0.0001; % First Stopband Frequency
Fpass1 = 0.0004; % First Passband Frequency
Fpass2 = 0.08; % Second Passband Frequency
Fstop2 = 0.12; % Second Stopband Frequency
Astop1 = 50; % First Stopband Attenuation (dB)
Apass = 0.5; % Passband Ripple (dB)
Astop2 = 50; % Second Stopband Attenuation (dB)
h = fdesign.bandpass('fst1,fp1,fp2,fst2,ast1,ap,ast2', Fstop1, Fpass1, ...
Fpass2, Fstop2, Astop1, Apass, Astop2);
Hd = designfilt(h, 'cheby2', ...
'MatchExactly', 'stopband', ...
'SystemObject', true);
and the screenshot from the filterDesigner app:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/604755/image.png)
0 个评论
回答(1 个)
Star Strider
2021-5-2
I have no idea what function was used to design the filter, since ‘FilterDesignerToolbox’ is nowhere to be found in the MATLAB or Signal Processing Toolbox documentation.
Use designfilt to create the filter. See the filtfilt documentation section on d for help with that.
9 个评论
Paul
2021-5-5
I'm becoming a bit more skeptical about the implementation of filtfilt(). Consider the following, where the filter is a harmless, 2nd order filter.
Fs = 2000;
Ts = 1./Fs;
n = 0:1:1*Fs;
f_max = Fs/10;
f = linspace(0,f_max,length(n));
x = cos(2*pi.*f.*n.*Ts); % Basic chirp
d = designfilt('lowpassiir', ... % Response type
'PassbandFrequency',50, ... % Frequency constraints
'StopbandFrequency',100, ...
'PassbandRipple',4, ... % Magnitude constraints
'StopbandAttenuation',15, ... % 15 for second order filter
'DesignMethod','ellip', ... % Design method
'MatchExactly','passband', ... % Design method options
'SampleRate',Fs); % Sample rate
[b,a] = tf(d);
[b;a]
y1 = filtfilt(d,x);
y2 = filtfilt(b,a,x);
plot([y1 ; y2].')
As we see, in this case filtfilt() returns the same result operating on either the digitalFilter object or the numerator and denominator coefficients directly.
Now change a filter design parameter to increase the order to a fourth-order filter
d = designfilt('lowpassiir', ... % Response type
'PassbandFrequency',50, ... % Frequency constraints
'StopbandFrequency',100, ...
'PassbandRipple',4, ... % Magnitude constraints
'StopbandAttenuation',55, ... % 15, 55 for second/fourth order filter
'DesignMethod','ellip', ... % Design method
'MatchExactly','passband', ... % Design method options
'SampleRate',Fs); % Sample rate
[b,a] = tf(d);
[b;a]
y1 = filtfilt(d,x);
y2 = filtfilt(b,a,x);
plot([y1 ; y2].')
Now there is a clear difference in the transient at the end, and zooming in at the beginning reveals a difference as well.
plot([y1 ; y2].')
set(gca,'XLim',[0 10])
Same filter, different results, and the results seem to be too different to ascribe to round-off effects for what appears to be very simple filter.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Filter Design 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!