Clear Filters
Clear Filters

Filter a signal by chunks

7 views (last 30 days)
Diego Marvid
Diego Marvid on 26 Apr 2021
Answered: Jan on 26 Apr 2021
I'll need to filter a signal in real time. Waiting for the whole signal to exist and filter is out of the question. The solution is to take small chunks and filter them. So basically the signal is the sum of a great amount of filtered chunks.
If this information is going to be used in real time systems, the phase delay of the filter will impact negatively the performance. Take the example of having a real time camera sending X,Y,Z coordinates of a person to a mechanical tracking system. The coordinates have to be filtered in order to mantain stable mechanical movements. If the delay is too big the mechanical system will be tracking air. This is why I opted for a zero-phase digital filter (filtfilt). However, when I divide the function in multiple chunks the intersection is NOT smooth. This is an example of a signal divided in 2 chunks and applying filtfilt to both of them.
The code for this function look like this:
[b,a] = butter(3,0.04);
p1 = filtfilt(b,a,y(1:100));
p2 = filtfilt(b,a,y(101:end));
yf2 = [p1;p2];
The intersection is at around 3.3-3.4 seconds. It is pretty visible that there are 2 different filtering sections.
This behaviour has even worse results in the velocity:
There's a clear spike at the intersection of both chunks.
I think that filtfilt pre-calculates the optimal initial condition for the filter. So passing the previous filtered chunk is no option with this.
The other option is using normal filter and passing final conditions to initial condition of next stage. The problem is the delay associated and the first initial condition.
The two chunks look smooth now but there is a clear delay and the filtered signal starts from 0!. Is there a way to set the first initial conditions so that the filtered signal starts from where it has to be? (At least the first value of the original function).
The code for this function look like this:
[b,a] = butter(3,0.04);
[p1,z1] = filter(b,a,y(1:100));
[p2,z2] = filter(b,a,y(101:end),z1);
yf2 = [p1;p2];
Is there any way in matlab to correct the initial condition on filtfilt for a signal divided in chunks? Or using a better initial condition with filter and trying to minimize the phase delay?
  1 Comment
Mathieu NOE
Mathieu NOE on 26 Apr 2021
hello Diego
As you say, the filtering method you need should not have too much delay (or phase rotation) if it's supposed to be used in a feedback control loop
maybe you should first define what is the max amount of delay or phase your loop can stand before going unstable
filtfilt is not causal so don't use it for real time expectations
filter can help you , as it supposed to be the "simple" causal application of any FIR or IIR filter
no, signal noise can be simply reduced if your control loop gain is reduced by a low pass filter in the regulator section

Sign in to comment.

Accepted Answer

Jan
Jan on 26 Apr 2021
There is no perfect method to get the initial values of the filter in the general case. One option is to filter the signal backwards and use the final conditions are initial values, but of course this costs processing time.
Is it an option to treat the initial phase, until the filter is stable, as a kind of calibration? I have this every morming, when I stand up and recalibrate, where the center of earth and the strength of the gravity is. So my motoric control is instable in the first 1 or 2 seconds and I avoid fast movements.
After this short delay, using the final conditions of a filter is fine for a smooth filtering. But as soon as you use FILTFILT, filtering backwards will destroy the smoothness. What about killing the delay in a hard way, by just shifting the signal?
By the way, these libraries might be faster for filterting (filter and filtfilt): https://www.mathworks.com/matlabcentral/fileexchange/32261-filterm

More Answers (0)

Community Treasure Hunt

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

Start Hunting!