NaN after applying filtfilt
11 views (last 30 days)
Show older comments
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
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.
Answers (1)
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
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
See Also
Categories
Find more on Digital Filtering in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!