Problem with filtfilt (i suppose)

16 views (last 30 days)
Corrado Giuliani
Corrado Giuliani on 5 Sep 2011
Hi. I have this problem: i have an ECG signal and i have to filter it since it's noisy and presents baseline wander.
This is my signal before filtering: http://imageshack.us/photo/my-images/695/prefilt.png/
Then i made two filtfilt operations:
function [x]=signalclearing(ecg,sf)
sf=sf/2; % shannon, sf= sampling frequency
wn=40/sf;
[B,A]=BUTTER(2,wn,'low');% 40 Hz filtering for noise reduction
x1=filtfilt(B,A,ecg);
wn=0.5/sf;
[B,A]=BUTTER(2,wn,'high');% 0.5 HZ filtering for baseline wander removal
x=filtfilt(B,A,x1);
end
as you can see there are problems at the end of the signal,and i guess it's filtfilt's fault which doesnt work at the very end or beginning of the signal.
Am i right? or not?
What are the possible solutions?
It's a big problem for my algorythm since it's all based on the max of it and in this case it screws up everything.
Thanks in advance for any help.

Answers (2)

Daniel Shub
Daniel Shub on 5 Sep 2011
I would zero pad your signal on both ends. This might help the transients die down a little. You could also window the resulting waveform to get rid of any transients.
I just looked at the original again. There is a huge DC offset, or at least ecg(1) and ecg(end) are not even close to zero. This causes a huge transient when you apply the filter, which assumes the preceding and following values are zero. I would remove the DC component first by subtracting the mean (or setup your filter state appropriately).
Basically, if you cut a signal out of the middle and filter it, you are not going to get the "right" answer. You need to do something about the edges. If you switch from an IIR filter (butter) to an FIR filter, then you know exactly how long there are filter transients.

Corrado Giuliani
Corrado Giuliani on 5 Sep 2011
OK i already windowed it and it does work, but i feel it's not so good. I can accept this process, but i'll have to publish what i'm doing and i'm tryin' to do everything in the right way instead of using some expedients to make it work. My question is: why does it not work at those parts? Non filtered signal is noisy but it doesnt have that big peak!

Community Treasure Hunt

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

Start Hunting!