FiltFilt function giving NaN values

41 views (last 30 days)
Anjali Agarwal
Anjali Agarwal on 11 Jun 2021
Edited: Star Strider on 15 Jun 2021
I designed a low pass filter of 170 Hz and filtered my signal using 'filtfilt' function. But I am getting NaN values in the output signal. I have checked my original signal and it doesn't contain any NaN or Inf values. I also used the filter function for fitering but still getting NaN values.
[b,a] = ellip(14,5,30,2*(170/srate),'low');
freqz(b,a)
lpfSig = filtfilt(b,a,Sig);

Answers (2)

Star Strider
Star Strider on 11 Jun 2021
Edited: Star Strider on 15 Jun 2021
The signal itself (as well as the value of ‘srate’) may be important here. (The factor of 2 is not necessary.)
I have no idea what the problem is, however one possibility is that there could be ‘ringing’ due to transients at one or the other end of the filtered signal. One way to eliminate that is to initially create the signal as:
signal_to_filter = [ones(10,1)*signal(1); signal(:); ones(10,1)*signal(end)];
use filtfilt on ‘signal_to_filter’, then remove the first and last 10 elements from the filtered signal.
The filter appears to be stable with this value of ‘srate’.
Analysis —
srate = 500;
[b,a] = ellip(14,5,30,2*(170/srate),'low');
figure
freqz(b,a, 2^14, srate)
EDIT — (15 Jun 2021 at 14:24)
If I reduce the order to 10, then I am not getting NaNs.
The transfer-function implementation of the filter itself does not appear to be unstable, however using a second-order-section implementation would be worth trying —
srate = 2000;
[z,p,k] = ellip(14,5,30,2*(170/srate),'low');
[sos,g] = zp2sos(z,p,k);
figure
freqz(sos, 2^14, srate)
The filtfilt call would then be:
lpfSig = filtfilt(sos,g,Sig);
.

Jan
Jan on 11 Jun 2021
I try to run your code guessing some inputs:
srate = 1000;
[b, a] = ellip(14, 5, 30, 2*(170/srate), 'low');
lpfSig = filtfilt(b, a, rand(1, 1000));
any(isnan(lpfSig))
ans = logical
0
You see, there is no Nan. So it is your turn to determine, where the difference between my guessed inputs and your real inputs is. Based on the given information, I cannot do this for you.
  3 Comments
Jan
Jan on 14 Jun 2021
Even if I chance "srate = 1000" to "srate = 2000", I do not get NANs.
Anjali Agarwal
Anjali Agarwal on 15 Jun 2021
If I reduce the order to 10, then I am not getting NaNs.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!