How to build and use a LowPass (or HighPass) Filter in real-time in a time domain simulation?

I'm new to control aspects and have not worked with filters before. I've been reading up on the documentation provided by Matlab regarding the filters but I'm not sure if I can or know enough to implement a filter to get the desired outcome. The problem is as follows.
I'm simulating motion of a body in time-domain, where I solve ODEs in a for loop at every time step (using rk4 approach). Within the for loop at each time step, I calculate certain coefficients and use them as inputs in solving the ODEs in my rk4 scheme. One of these coefficients (say alpa) turned out to be very oscillatory with more than one frequency. Following is an illustration of that parameter.
I suppose using a filter to get rid of the noise would be the solution here. However, I don't know how to read the value of alpha at each time step, filter the noise and use the filtered alpha as an input to the ODE system. How can I go around this?

 Accepted Answer

The transfer function for the first LPF is represented as follows
Where, is the cut-off frequency, and S is the variable in the Laplace transform. In real-time systems, this needs to be converted to discrete time. The first-order LPF at discrete time can be simply expressed in the following form
where x[n] is the current input value,
y[n] is the current output value,
where y[n-1] is the previous output value,
α is a factor that determines the strength of the filter, and the time constant
τ and the sampling period
T to calculate it.
I think you can continue to get new inputs in this way and filter them by inputting the measured alpha value into the LPF according to the above formula. And if you adjust the cutoff frequency appropriately while simulating, you should be able to see the filtered signal with a slight delay.
I've never used latex before, so my answers may be terrible, sorry.

7 Comments

@영준, Thank you for the explanation. I can understand the concept a bit clearer now, and I've also been reading up since I posted the question (a day before). The LaTeX is just fine and clear.
My one confusion is what happens at the very first time step, because then y(n-1) doesn't exist. Of course, rationally, at the first time step, there's nothing to filter either. But is there a standard way to handle this? Or would one simple start the filtering process when n>1 ?
In such cases, it is better to specify a single initial state. y(n-1) is effectively null, but since it is also interpreted as no motion, it is equivalent to zero and can be handled with an exception at the start of the if function. If you want to get more advanced, you can randomize the initial state of the filter or look up the initial state of the filter.
The n>1 you are considering is also one of the good ones. Although not stated in the question, all filters introduce a delay, keep in mind that the cutoff frequency can introduce a lot of temporal delay to this, so it should be adjusted heuristically
Okay, that makes sense. However, would these introduce a lag or a phase lag to the system since I'm using results from previous iterations?
I do realize the lack of my theoretical knowledge on the matter limits my efforts. Do you also happen to have any recommended basic reading on these?
Another side question (sorry about the endless queries). How would this differ from a HighPass filter?
** EDIT: I learned this. No need of an answer :)
I see, I see, but since we are using the results from the previous iteration, will there be a delay or phase lag in the system? -> Yes
The earliest filters were circuits, but even then LPFs used capacitors, meaning they had a capacitor component.

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Asked:

on 20 Feb 2024

Commented:

on 21 Feb 2024

Community Treasure Hunt

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

Start Hunting!