Digital filter with lowpass, then filtfilt: different output
20 views (last 30 days)
Show older comments
Hello world!
Here I am again dealing with digital filters; I am using the latest MATLAB version (R2020a) with DSP system toolbox.
I am manipulating accelerometers data that need to be filtered by a lowpass filter: satisfactory results have been achieved with function lowpass. Since next acquistions have to pass through the very same filter, I extracted the filter built in lowpass writing
[dataflt, Hd] = lowpass(data, cut_off, fs, 'ImpulseResponse', 'fir');
Subsequent steps would be to apply Hd via filtfilt function to all the other acquisitions: as stated in past MathWorks questions, lowpass inherently uses filtfilt function. In order to double-check this statement, I filtered the data used in lowpass with filter Hd writing
dataflt = filtfilt(Hd,data);
I plotted the two outputs to compare them (in the time domain, figure below): it can be concluded that discrepancies arise at the beginning and at the end of the time history.
Why does such a difference exist? Am I neglecting anything fundamental? Is there any way to solve this problem?
I would prefer to use filtfilt (or similar) instead of lowpass because it seems to me the faster way to filter data, given that the filter is precalculated: is this statement true?
Thank you in advance for your wothwhile help.
Riccardo
0 Comments
Accepted Answer
Star Strider
on 13 May 2020
Occasionally, filter transients can appear, such as those you are seeing. The way I deal with those (to my satisfaction at least) is to add a vector of ones multiplied by the initial value of the vector to the beginning (and similarly at the end if necessary) to the vector. I then remove these elements from the filtered signal. The length of the added elements can vary, I usually use 10 samples. Experiment to get the result you want.
4 Comments
More Answers (1)
Paul
on 8 Jun 2024
lowpass calls filtfilt (or executes code equivalent to filtfilt) only if the underlying filter is IIR. If it's FIR, as in the question by @Riccardo Sorvillo, then lowpass "compensates for the delay introduced by the filter" by using the known delay of a FIR filter that depends on its order.
Example:
rng(100);
fs = 1e3;
t = 0:1/fs:1;
x = [1 2]*sin(2*pi*[50 250]'.*t) + randn(size(t))/10;
% lowpass() with IIR filter, same result as filtfilt()
[ylp,d] = lowpass(x,200,fs,'ImpulseResponse','iir','Steepness',0.5);
yff = filtfilt(d,x);
figure
plot(t,ylp-yff)
% lowpass() with FIR filter, different result than filtfilt()
[ylp,d] = lowpass(x,150,fs);
yff = filtfilt(d,x);
figure
plot(t,ylp-yff),grid
0 Comments
See Also
Categories
Find more on Digital Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!