Problem with persistent variables within embedded function

1 view (last 30 days)
PEF
PEF on 5 Sep 2019
Answered: Denis Gurchenkov on 13 Sep 2019
Hi,
I am currently implementing a second order lowpass filter as an embedded function and can be found as attachment. I tested it against a native simulink block employng the same state-space representation and the results are exactly, so the implementation itself should be correct.
My issue is the following: let's say that I have three signals that I intend to filter, hence I would be calling the following snippet:
sig1 = SecondOrderLowpassFilterTustin(u(1),fn,dn,Ts);
sig2 = SecondOrderLowpassFilterTustin(u(2),fn,dn,Ts);
sig3 = SecondOrderLowpassFilterTustin(u(3),fn,dn,Ts);
Unfortunately, this does not work as expected, and I assume that happen because of the persistent xs variable within the function. On the other hand, if let's say only u(1) was to be filtered, sig1 would be representing the proper results.
How can I make the function safe, so that the persistent variable are reset every time I call the function?

Accepted Answer

Denis Gurchenkov
Denis Gurchenkov on 13 Sep 2019
The way I understand your quesiton is this: each of those three calls to SecondOrderLowpassFilterTustin should have its own persistent state. That is, each time
sig1 = SecondOrderLowpassFilterTustin(u(1),fn,dn,Ts);
is invoked, it should use the 'xs' from the prevoius call on this line, but not from the other two calls.
If that is your intent, then one way to solve the issue would be to implement a class that contains 'xs' as a property, and make SecondOrderLowpassFilterTustin() to be a method of that class.
classdef MyLowpassFilter < handle
properties
xs
end
methods
function obj = MyLowpassFilter(u)
obj.xs = [u 0]';
end
function y = SecondOrderLowpassFilterTustin(obj, u,fn,dn,Ts)
... all your code here but use obj.xs instead of xs...
end
end
end
function main
persistent filter1, filter2, filter3
if isempty(filter1)
filter1 = MyLowpassFilter(u(1));
filter2 = MyLowpassFilter(u(2));
filter3 = MyLowpassFilter(u(3));
end
sig1 = filter1.SecondOrderLowpassFilterTustin(u(1),fn,dn,Ts);
sig2 = filter2.SecondOrderLowpassFilterTustin(u(2),fn,dn,Ts);
sig3 = filter3.SecondOrderLowpassFilterTustin(u(3),fn,dn,Ts);
end
Another way would be to just move the persistent state to the caller:
function main(u)
persistent xs0, xs1, xs2
if isempty(xs0)
xs0 = [u 0]';
xs1 = xs0;
xs2 = xs0;
end
[sig1, xs0] = SecondOrderLowpassFilterTustin(u(1),fn,dn,Ts, xs0);
[sig2, xs1] = SecondOrderLowpassFilterTustin(u(2),fn,dn,Ts, xs1);
[sig3, xs2] = SecondOrderLowpassFilterTustin(u(3),fn,dn,Ts, xs2);
end
function [y, xs] = SecondOrderLowpassFilterTustin(u,fn,dn,Ts, xs)
...your code here but remove the initialization of xs...
end

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by