filtfilt.m fails for input array x with 3 or more dimensions

조회 수: 5 (최근 30일)
Helmut Riedel
Helmut Riedel 2011년 9월 12일
편집: Jan 2016년 9월 28일
Dear all,
filtfilt.m does not work for ndim-arrays anymore, e.g.:
x = randn(1000,3,2); % i.e., random signal with 3x2 channels
[b,a] = butter(2,0.1); % 2nd order Butterworth lowpass filter
In older matlab versions, e.g., R2007b, I could say
fx = filtfilt(b,a,x);
and got the filtered signal fx with the same size as x. In R2011a the same command
fx = filtfilt(b,a,x);
returns the following error:
??? Error using ==> filter
Initial conditions must be a vector of length max(length(a),length(b))-1, or an array with the leading dimension of size max(length(a),length(b))-1 and with remaining dimensions matching those of x.
Error in ==> filtfilt>ffMultiChan at 296
yc3 = filter(b(:,ii),a(:,ii),xt,zo);
Error in ==> filtfilt at 78
y = ffMultiChan(b,a,x,zi,nfact,L);
My impression is that the dimensionality of the initial and final conditions, Zi and Zf is not correctly passed to filter.m.
Of course, I can circumvent the error by filtering data with maximal two dimensions, e.g., by reshaping before and after filtering or as follows:
fx = zeros(size(x)); % allocation
fx(:,:,1) = filtfilt(b,a,x(:,:,1));
fx(:,:,2) = filtfilt(b,a,x(:,:,2));
However, I just would like that filtfilt.m can handle n-dimensional input x as in earlier versions.
Thanks in advance for any help,
Helmut

답변 (2개)

Jan
Jan 2011년 9월 12일
Does it help to specify the dimension to operate on?
You could try to use Fex: FiltFiltM, which is much faster than Matlab's implementation, while it replies identical values. FILTFILT wastes a lot of time with expanding the signal by initial and final phases, while FiltFiltM calculates the edges separately. In addition FILTFILT reverses the expanded signal twice, while FiltFiltM uses a fast C-mex to emulate FILTER with allowing to filter in teh reverse direction. In sonsequence FiltFiltM is 5 (short signal) to 20 (large signal) times faster.

Nitesh Chowdhury
Nitesh Chowdhury 2016년 9월 27일
hi,
I'm new to using MATLAB and i'm using 2014a version, trying to filter a sensor signal which has a lot of noise. The data is in 1*16060double matrix. I have attempted multiple different variations of the command filtfilt but I keep getting errors. Also what should be the values for 'a' and 'b'?
I have already used transpose, converted into a 2*16060double matrix with time in the new row but nothing works. Here are the errors that I have got.
>
> filtfilt(1,10,ax)
Index exceeds matrix dimensions.
Error in filtfilt>ffMultiChan (line 320)
[~,zo] = filter(b(:,ii),a(:,ii),xt,zi(:,ii)*xt(1,:)); % outer product
Error in filtfilt (line 106)
y = ffMultiChan(b,a,x,zi,nfact,L);
>> filtfilt(1000,10000,ax)
Index exceeds matrix dimensions.
Error in filtfilt>ffMultiChan (line 320)
[~,zo] = filter(b(:,ii),a(:,ii),xt,zi(:,ii)*xt(1,:)); % outer product
Error in filtfilt (line 106)
y = ffMultiChan(b,a,x,zi,nfact,L);
>> filtfilt(17000,17000,ax)
Index exceeds matrix dimensions.
Error in filtfilt>ffMultiChan (line 320)
[~,zo] = filter(b(:,ii),a(:,ii),xt,zi(:,ii)*xt(1,:)); % outer product
Error in filtfilt (line 106)
y = ffMultiChan(b,a,x,zi,nfact,L);
>> filtfilt(17000,17000,ax)
Attempted to access xt(1); index out of bounds because numel(xt)=0.
Error in filtfilt>ffOneChan (line 260)
[~,zo] = filter(b(:,ii),a(:,ii), xt, zi(:,ii)*xt(1)); % yc1 not needed
Error in filtfilt (line 103)
y = ffOneChan(b,a,x,zi,nfact,L);
  댓글 수: 1
Jan
Jan 2016년 9월 28일
편집: Jan 2016년 9월 28일
Please post a new question in a new thread and not as an answer of another question. Thanks.
Do you get the same error for this code:
ax = rand(1, 16060);
y = filtfilt(1,10,ax);
?

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

카테고리

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