Problem with persistent variables within embedded function

1 回表示 (過去 30 日間)
PEF
PEF 2019 年 9 月 5 日
回答済み: Denis Gurchenkov 2019 年 9 月 13 日
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?

採用された回答

Denis Gurchenkov
Denis Gurchenkov 2019 年 9 月 13 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by