I have a transfer function that I am trying to use to filter acceleration data.
So far I have been able to use lsim with a sin wave over about 10 seconds and I'm getting the result I expect. However, I cannot work how to get data to the function in real time.
To clarify, every 0.1 seconds I am receiving an acceleration value from an external program. I need to filter the values to remove high frequency variations in the data. I need this to occur for each data point I receive as I then use the current filtered acceleration value into additional processing steps.
How do I use the transfer function in continuously and update the output value each time new data is received?
This is an example of how to do this with filter
:
filter_state = []; % start with empty filter history
while not_stopped()
x = get_one_input_sample();
[y, filter_state] = filter(B, A, x, filter_state);
process_one_output_sample(y);
end;
Note that you need to use the extended form of filter, in which you pass the history of the filter from one iteration to the next using the variable filter_state
. The B
and A
are the coefficients of your discrete filter. If your filter is continuous, you need to transform it to a discrete filter first using c2d
or so. Note that if your filter is very complex, you might need to split up the filtering in several steps (e.g. one second-order-stage per filter) to avoid numerical problems.