MATLAB Answers

NaN after applying filtfilt

96 views (last 30 days)
Giuseppe Naselli
Giuseppe Naselli on 4 Mar 2014
Commented: Yuntao Ji on 17 May 2020
Hi All,
I am having some problem in using a filter.
I designed a specific filter using the following commands
fs = 200; %(Hz) sampling frequency
fpass = [0.45 40] ; % Passband edges (Hz)
fstop = [0.4 45]; % Stopband Egdge (Hz)
Wp = fpass *(pi/fs); % Normalized Passband Edges
Ws = fstop *(pi/fs); % Normalized Stopband Edges
Rp = 0.1; % Passband Ripple (dB)
Rs = 20; % Stopband Attenuation (dB)
% Define the necessary filter order
[N, Wn] = cheb1ord(Wp, Ws, Rp, Rs, 's');
[B, A] = cheby1(N, Rp, Wn, 's');
[H, w] = freqs(B, A);
% Plot the responce of the filter (to check it is doing what I want)
subplot(2,1,1)
semilogx(w*fs/pi, 20*log10(abs(H)) );
ylabel('Amplitude (dB)', 'FontSize',16)
title('Filter Responce','FontSize',16)
subplot(2,1,2)
semilogx(w*fs/pi, 180/pi*phase(H));
ylabel('Phase (deg)','FontSize',16)
xlabel('Frequency (Hz)','FontSize',16)
Now, looking at the filter, I decide that the filter is what I desire and to avoid the phase shift I want to use the filtfilt command (I know this will double the filter order, right?, but I took this in account in designing my filter specs), therefore I do
Data_filt = filtfilt(B,A,Data);
The problem is that Data_filt is a vector with all NaN
I have seen few threads on problems that are similar to this, but I could not figure out a definitive solution.
Can someone help with this?
Many thanks in advance
Regards,
G

  1 Comment

Yuntao Ji
Yuntao Ji on 17 May 2020
Hi Giuseppe, I guess you have at least a nan or a inf in your array. You can use isnan and isinf to locate it/them.

Sign in to comment.

Answers (1)

Jos (10584)
Jos (10584) on 4 Mar 2014
This means that Data itself has at least one NaN in it. You could remove it, or replace it using, for instance, interp1 . There may be better alternatives ...
Data = [1 3 7 NaN 9 4 5]
tf = isnan(Data)
ix = 1:numel(Data)
Data(tf) = interp1(ix(~tf),Data(~tf),ix(tf))
This this not replace NaNs at the beginning or end of Data, but these can be safely removed.

  3 Comments

Giuseppe Naselli
Giuseppe Naselli on 4 Mar 2014
Hi Jos, thanks for your answer
I just checked Data and there is no NaN element in it
Any other suggestion?
G
Jos (10584)
Jos (10584) on 4 Mar 2014
Are you sure? Then either filter coefficients B or A should have a NaN
filtfilt(NaN,1:2,1:5)
ans = NaN NaN NaN NaN NaN
Giuseppe Naselli
Giuseppe Naselli on 6 Mar 2014
Hi Jos,
I wrote a simpler code for a simpler filter. Let's suppose I want to filter CPL (data are attached) with an analog 5th order Chebychev type I filter.
My code is the following
fs = 200; %(Hz) sampling frequency
R = 0.01; % dB in the pass-band ripple
fc = 40; % cut-off frequency (Hz)
[z,p,k] = cheby1(5,0.01,2*pi*40,'low','s'); % create the filter
[sos,g] = zp2sos(z,p,k);
CPL_filt = filtfilt(sos,g,CPL); % apply the filter with zero phase-shift
The problem is that CPL_filt has all NaN
Any idea? Can the problem be my Matlab? if so, is it possible to run a check?
G

Sign in to comment.