filtered output signal gives high values at the ends when using lowpass function

After filtering a data signal using the lowpass function in Maltab, the ends of the output signal gives very high values like in the figure below. How should I deal with this?
Light blue is the original data and light green is after the filtering using lowpass function. This also happens when using filter function.

 Accepted Answer

Filter transients such as those can occasionally occur with any filtering algorithm (and both hardware and digital filters), especially if the signal has abrupt ‘step’ transitions at the ends.
The easiest way to deal with it is to first subtract the mean of the signal from the rest of the signal, filter the signal, then add back the mean to the filtered signal. Another way to deal with it is to pad it at both ends with a vector of ones that is equivalent to the mean of the signal, then use indexing to elimiinate those elements from the ends of the filtered signal. Subtracting the mean and adding it back later is easier.

4 Comments

Is it also a ok to subtract the 1st value of the signal from the whole signal, then filter and add the 1st value back?
because I don't have any experience in the 2nd method you said, I'd like to do the easy way. However, I see this samething happening at the end of the signal(even when I substracted the 1st value from the signal and then filtered), when I used lowpass function. Is it better to use mean in such a case?
I would subtract the mean of the signal from the entire signal (this is easily done in MATLAB), then add the single mean value back to the filtered signal (also easily done in MATLAB).
An example to illustrate the approach:
s = randi([100, 200], 1, 10) % Create Signal
s_mean = mean(s)
s_submean = s - s_mean
s_submean = filtfilt([1 1 1], 3, s_submean) % Simple Moving-Average Filter For Illustration
s_addmean = s_submean + s_mean
The last step re-creates the filtered vector using the original mean value.
Subtracting only the first value may not be as robust as subtracting the mean.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!