필터 지우기
필터 지우기

NaN after applying filtfilt

조회 수: 13 (최근 30일)
Giuseppe Naselli
Giuseppe Naselli 2014년 3월 4일
댓글: Yuntao Ji 2020년 5월 17일
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
Yuntao Ji
Yuntao Ji 2020년 5월 17일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jos (10584)
Jos (10584) 2014년 3월 4일
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
Jos (10584)
Jos (10584) 2014년 3월 4일
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 2014년 3월 6일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Digital Filtering에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by