# Discrete algorithm for low pass filter

I am working on a position controller for a marine vessel. I have an measurement signal containing the y-position of the vessel that consists of both low frequency (<.1 rad/s) and high frequency (>.6 rad/s) motions. I am currently using a state observer that seperates that combined motion in two seperate states (a LF and WF states). The gains are calculated using the discrete time kalman filter algorithm.

At every timestep I recieve a new incoming measurement and my state observer corrects the predicted state with the measurement.

I want to compare the performance of this approach with using a low pass filter on the measurement signal instead of using an kalman filter. The theory seems to be that a Kalman filter has a much lower phase lag, but I am seeing some amplifications in the magnitude bode plot of my outgoing LF signal compared to the incoming measurements and the filter thus amplifies motions that are between the LF and the WF frequencies...

I need some advice on how to implement a nth order low pass filter in such a discrete time system. Is there an algorithm to filter the incoming measurements at every time step, or do i need to take the entire time trace of the measurement and filter that signal. I am using matlab.

Any tips are more than welcome, Scott

You can probably use Matlab's filter command. If you call it like this:

[Y,Zf] = filter(B,A,X,Zi);


the variable Zf contains the state of the filter after filtering, and Zi contains the filter's state before filtering. You could proceed roughly like this (in pseudocode):

Acquire several samples and store them in vector Samples
[Y,Z] = filter(B,1,Samples); % assuming FIR filter stored in B
while 1
acquire new sample and store it in Sample
[Y,Z] = filter(B,1,Sample,Z);  % Y contains the updated filter output
end


To design the filter, I'd start by looking at the fir1 command.

• Thanks, so I will need a couple of samples to filter on right? What is a good way to determine how many samples (and thus phase lag?) I will need to get good filter results? – Scott Oct 6 '14 at 14:54
• You should be able to filter one sample at a time. However, for FIR filters, a high filter order implies a filter with many coefficients, which in turn implies that the effect of particular sample propagates slowly through the filter. Infinite impulse response (IIR) filters require less coefficients to achieve a given order, so they may be a better option for you. – MBaz Oct 6 '14 at 16:27