Thursday, February 23, 2017

matlab - Why Butterworth filter always starts my signal from from zero mark (amplitude)?


I am trying to filter my force signal using Butterworth low pass filter.enter image description here


so this is the signal, I have to filter (noise frequency is around 33 Hz). So when I use Butterworth filter with following command in MATLAB (with sixth order):


[B,A]=butter(6,5/100,'low');

forceS=filter(B,A,forceS);

the result I get is enter image description here


So the problem is the filtered signal start from zero mark instead of around 1.5. How can i start the filtered signal from the same value as unfiltered signal has?


Thanks



Answer



Filters do have a delay (a lag) since they do not act immediately on your signal. Also all samples before the time 0 are zeros, thus in general you will start from the "zero mark", as you said (just imagine your filter equation with all zeros).


There are ways to make a filter to have a zero lag. It is done by so called zero-phase filtering, also known as forward-backward filtering. The way you do it is by filtering your signal twice - in forward and in backward direction. Obviously this can work only for offline applications. In MATLAB you can do it very easily using the filtfilt function.


Please find below a code corresponding to your case.


fs = 100;

T = 2;
t = 0:1/fs:T;
f = 2;
s = 1.5*cos(2*pi*f*t) + 0.7*sin(2*pi*33*t);

[B,A]=butter(6, 5/fs,'low');
S=filter(B, A, s);
Sff=filtfilt(B, A, s);

plot(t, s)

hold on
plot(t, S)
plot(t, Sff)
grid on
legend({'Original signal', 'Signal filtered using filter', 'Signal filtered using filtfilt'})

That yields what I suspect you want:


enter image description here


No comments:

Post a Comment

periodic trends - Comparing radii in lithium, beryllium, magnesium, aluminium and sodium ions

Apparently the of last four, $\ce{Mg^2+}$ is closest in radius to $\ce{Li+}$. Is this true, and if so, why would a whole larger shell ($\ce{...