Effacer les filtres
Effacer les filtres

Problem with persistent variables within embedded function

4 vues (au cours des 30 derniers jours)
PEF le 5 Sep 2019
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?

Réponse acceptée

Denis Gurchenkov
Denis Gurchenkov le 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
function obj = MyLowpassFilter(u)
obj.xs = [u 0]';
function y = SecondOrderLowpassFilterTustin(obj, u,fn,dn,Ts)
... all your code here but use obj.xs instead of xs...
function main
persistent filter1, filter2, filter3
if isempty(filter1)
filter1 = MyLowpassFilter(u(1));
filter2 = MyLowpassFilter(u(2));
filter3 = MyLowpassFilter(u(3));
sig1 = filter1.SecondOrderLowpassFilterTustin(u(1),fn,dn,Ts);
sig2 = filter2.SecondOrderLowpassFilterTustin(u(2),fn,dn,Ts);
sig3 = filter3.SecondOrderLowpassFilterTustin(u(3),fn,dn,Ts);
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;
[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);
function [y, xs] = SecondOrderLowpassFilterTustin(u,fn,dn,Ts, xs)
...your code here but remove the initialization of xs...

Plus de réponses (0)


En savoir plus sur Single-Rate Filters dans Help Center et File Exchange



Community Treasure Hunt

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

Start Hunting!

Translated by