FiltFilt function initial and final conditions
Mostrar comentarios más antiguos
Hi,
I would like to filter a signal using filtfilt in order to have:
- zero phase - to have the initial and final sample of the filtered data the same as the raw data
Following this link: http://www.mechanicalvibration.com/filtfilt_Casual_versus_non_.html#foot60 the initial and final point are maintained using filtfilt. However when I do this, they are not.
The commands I use are: b = [0.0675, 0.1349, 0.0675]; % Low pass Butterworth filter a = [1.0, -1.1430, 0.4128]; Sig_filtered=filtfilt(b, a, Sig_raw);
Can anyone explain this to me or point me towards a filter function that can do this?
Thank you in advance.
Kr, Brecht
Respuesta aceptada
Más respuestas (3)
Bruno Luong
el 16 de Oct. de 2018
Editada: Bruno Luong
el 16 de Oct. de 2018
Here is a demo of using filtfilt on periodic data
- The first method use filtfilt on one period
- The second method stitches rawdata one period on the left and one on the right, call filtfilt on the three periods, then crop the middle period (Jan's suggestion)
- The third method does like the second but smoothly interpolate in a transition of 0.1 period.
As you can see the first one have a problem of stitching at the boundary, the second and third methods give almost the same result, but I know the third one must have a smooth transition.


% True Periodic signal
x = linspace(0,2*pi,100);
y = sin(x) - 0.5*sin(2*x+1) + 0.6*sin(3*x+2) - 0.3*sin(4*x+4);
% Add noise
sigma = 0.3;
y = y + sigma*randn(size(y));
% Filter design
[b,a] = butter(8,0.1);
% First method
yff = filtfilt(b,a,y);
% Second & Third methods
nperiods = 3;
[xd,yd] = duplicate(x,y,nperiods);
y123 = filtfilt(b,a,yd);
y1 = crop(y123,nperiods,1);
y2 = crop(y123,nperiods,2);
% Second method
yt = y2;
% Third method
w = max(0,interp1(x(end)*[0.9 1],[0,1],x,'linear','extrap'));
yi = (1-w).*y2 + w.*y1;
% Generate graphics
close all
hold on
xp = x/(2*pi);
h(1)=plotdup(xp,y,'color',0.7*[1 1 1]);
h(2)=plotdup(xp,yff,'b');
h(3)=plotdup(xp,yt,'g');
h(4)=plotdup(xp,yi,'r');
xline(1);
xline(2);
legend(h,'noisy data','fiiltfilt','ff-truncated','ff-interpolate')
function [xd,yd] = duplicate(x,y,nperiods)
if nargin < 3
nperiods = 3;
end
x = x(:);
dx = x(end)-x(1);
xd = x(1:end-1) + dx*(0:nperiods-1);
xd = [xd(:)', dx*(nperiods-1)+x(end)];
yd = [repmat(y(1:end-1),1,nperiods), y(end)];
end
function y = crop(yd,nperiods,i)
yend = yd(end);
yd = reshape(yd(1:end-1),[],nperiods);
if i < nperiods
yend = yd(1,i+1);
end
y = [yd(:,i); yend]';
end
function h = plotdup(x,y,varargin)
[xd,yd] = duplicate(x,y);
h = plot(xd,yd,varargin{:});
end
Bruno Luong
el 16 de Oct. de 2018
Editada: Bruno Luong
el 16 de Oct. de 2018
Now a rigorous approach of zero-phase FIR on circular data. One need to build a circular matrix from the coefficients A and B, the similar to filt-filt apply the circular-filter in both directions to undo the phase-shift.


I call it fourth method, a ff-circular. The code is following
% True Periodic signal
x = linspace(0,2*pi,100);
y = sin(x) - 0.5*sin(2*x+1) + 0.6*sin(3*x+2) - 0.3*sin(4*x+4);
% Add noise
sigma = 0.3;
y = y + sigma*randn(size(y));
% Filter design
[b,a] = butter(8,0.1);
% First method
yff = filtfilt(b,a,y);
% Fourth method
n = length(y)-1;
Sa = cmat(a,n);
Sb = cmat(b,n);
yc=Sa\(Sb*y(1:end-1)');
yc=Sa\(Sb*flip(yc));
yc = flip(yc([end 1:end]))';
% Generate graphics
close all
hold on
xp = x/(2*pi);
h(1)=plotdup(xp,y,'color',0.7*[1 1 1]);
h(2)=plotdup(xp,yff,'b');
h(3)=plotdup(xp,yc,'r');
xline(1);
xline(2);
legend(h,'noisy data','fiiltfilt','ff-circular')
function S = cmat(a, n)
A = repmat(flip(a),n);
d = 0:length(a)-1;
S = spdiags(A,d,n,n) + ...
spdiags(A,d-n,n,n);
end
function [xd,yd] = duplicate(x,y,nperiods)
if nargin < 3
nperiods = 3;
end
x = x(:);
dx = x(end)-x(1);
xd = x(1:end-1) + dx*(0:nperiods-1);
xd = [xd(:)', dx*(nperiods-1)+x(end)];
yd = [repmat(y(1:end-1),1,nperiods), y(end)];
end
function h = plotdup(x,y,varargin)
[xd,yd] = duplicate(x,y);
h = plot(xd,yd,varargin{:});
end
Brecht Vermeulen
el 4 de Sept. de 2018
0 votos
4 comentarios
Bruno Luong
el 16 de Oct. de 2018
Editada: Bruno Luong
el 16 de Oct. de 2018
If your data is periodic, than not only you need to match the value of the first and end point but also the derivative of those points.
I'm not sure but to me it looks like one can write down the recursive filter equations in a periodic way and solve it.
I guess it still give a linear relation that can non longer apply recursively but rather can be solved by linear algebra and matrix.
Note: For non-recursive filter (not your case), it boils down to circular convolution.
Jan
el 16 de Oct. de 2018
I expected filtfilt to maintain exactly the initial and
final sample.
This is not the nature of filtfilt, which expands the original signal by a mirrored signal of the initial and final phase to allow a more or less generally matching smoothing in these parts also.
Transitional effects are a problem at filtering, which can be solved heuristically only. Any algorithm must either apply a poorer filtering at the corners due to less available data, or it must guess a "valid" continuation of the signal, to set the internal state of the filter to the matching parameters. Both methods have drawbacks. But if you know, that the signal is periodic, you have all you need to expand the signal: Add the final phase to the start, the start phase to the end, run filter in both directions or try filtfilt. Afterwards crop the added signals. This does not guarantee, that the first and last sample are perfectly identical, but if you have noise in the signal, this would not be realistic at all.
Bruno Luong
el 16 de Oct. de 2018
Editada: Bruno Luong
el 16 de Oct. de 2018
@Jan: A better way than crude cropping is interpolating the filtered signal over overlapping zone (imagine the signal wrapped on the circle) by a weighted of two signals that make a smooth transitions with continuity, the weight selected to can be infinity smooth if such smoothness is desired.
@Bruno: I agree. I'm only not satisfied with such photoshopping of measurement data. Smoothing a signal until it fits the expectations exactly is a construction of a result and not necessarily a measurement. Another problem could be a high frequency signal near to the Nyquist frequency. Then a minimal timeshift can cause a cancellation of the signal. So cropping is less smart, but perhaps this crudeness preserves the nature of the original signal.
As usual in signal processing, filtering is not trivial. You cannot expect that filter or filtfilt remove the noise magically and a deep knowledge of the effects of filtering is required. A pure noise can be filtered, until a clean periodic sine-wave is found as "result", but this has no scientific power anymore.
But maybe the OP is aware of such problems. It was only "heavy filtering" combined with "maintain exactly the initial and final sample" which let me mention the potential overdoing at filtering.
Categorías
Más información sobre Digital Filtering en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!