From: Tom on
Hi

I'm trying to learn Matlab OOP and started out implementing a simple digital-filter.

Now, I have a class that does the filtering and the memory of the filter is contained within the class and is updated whenever a new filter output is computed. The filter object
is called in a for-loop for every sample iteration. I notice that even though I do update the memory of the filter in the class-method the memory (a vector) is reset to zero so this
the code doesn't work.

Can someone please reveal how I ensure that the filter memory update is retained for the next sample iteration?

Thanks
Tom
From: us on
"Tom " <stoltzo(a)gmail.com> wrote in message <hrctqo$fj8$1(a)fred.mathworks.com>...
> Hi
>
> I'm trying to learn Matlab OOP and started out implementing a simple digital-filter.
>
> Now, I have a class that does the filtering and the memory of the filter is contained within the class and is updated whenever a new filter output is computed. The filter object
> is called in a for-loop for every sample iteration. I notice that even though I do update the memory of the filter in the class-method the memory (a vector) is reset to zero so this
> the code doesn't work.
>
> Can someone please reveal how I ensure that the filter memory update is retained for the next sample iteration?
>
> Thanks
> Tom

a - very - small amout of code is needed...

us
From: Tom on
"us " <us(a)neurol.unizh.ch> wrote in message <hrcu7r$bsi$1(a)fred.mathworks.com>...
> "Tom " <stoltzo(a)gmail.com> wrote in message <hrctqo$fj8$1(a)fred.mathworks.com>...
> > Hi
> >
> > I'm trying to learn Matlab OOP and started out implementing a simple digital-filter.
> >
> > Now, I have a class that does the filtering and the memory of the filter is contained within the class and is updated whenever a new filter output is computed. The filter object
> > is called in a for-loop for every sample iteration. I notice that even though I do update the memory of the filter in the class-method the memory (a vector) is reset to zero so this
> > the code doesn't work.
> >
> > Can someone please reveal how I ensure that the filter memory update is retained for the next sample iteration?
> >
> > Thanks
> > Tom
>
> a - very - small amout of code is needed...
>
> us

Hi us

Thanks for your quick reply - here is my code (implementing a digital state variable filter)

Hope you can see what I'm doing wrong....

classdef StateVariableFilterClass

properties
f = [];
fs = [];
q = [];
input = [];
output = [];
mem;
y_lp = [];
y_hp = [];
y_bp = [];
Norder = [];
tmp = [];
xx = 1;
end

methods

% Class constructor
function obj = StateVariableFilterClass(Norder,f,Q,fs)
obj.f = 2*sin(pi*f/fs);
obj.q = 1/Q;
obj.mem = zeros(Norder,2);
obj.Norder = Norder;
end

% State Variable Filter
function y = filterState(obj,x,counter)

% Iterate over biquad sections
for jj=1:obj.Norder
% Lowpass filter output
obj.mem(jj,2) = obj.mem(jj,1)*obj.f+obj.mem(jj,2);
obj.y_lp = obj.mem(jj,2);

% Highpass filter output
obj.y_hp = x-obj.mem(jj,2)-obj.mem(jj,1)*obj.q;

% Bandpass filter output
obj.y_bp = obj.y_hp*obj.f+obj.mem(jj,1);
obj.mem(jj,1) = obj.y_bp;

x = obj.y_hp;
end

y = x;
end
end
end

The class is instantiated and used in the following way:

% Filters
filt = StateVariableFilterClass(Norder,fc,q,fs);

for ii=1:length(x)
y(ii) = filt.filterState(x(ii),ii);
end
From: Tom on
Maybe I just need to use "persistent"?

"us " <us(a)neurol.unizh.ch> wrote in message <hrcu7r$bsi$1(a)fred.mathworks.com>...
> "Tom " <stoltzo(a)gmail.com> wrote in message <hrctqo$fj8$1(a)fred.mathworks.com>...
> > Hi
> >
> > I'm trying to learn Matlab OOP and started out implementing a simple digital-filter.
> >
> > Now, I have a class that does the filtering and the memory of the filter is contained within the class and is updated whenever a new filter output is computed. The filter object
> > is called in a for-loop for every sample iteration. I notice that even though I do update the memory of the filter in the class-method the memory (a vector) is reset to zero so this
> > the code doesn't work.
> >
> > Can someone please reveal how I ensure that the filter memory update is retained for the next sample iteration?
> >
> > Thanks
> > Tom
>
> a - very - small amout of code is needed...
>
> us
From: David Young on
You need to be aware of the difference between a value class and a handle class.

Objects belonging to normal classes (i.e. value classes) obey the same rules as other Matlab objects: that is, they are effectively copied when assigned or passed to a function. This excellent rule means that you never accidentally update the state of an object that you didn't mean to update, and fits in the general philosophy of Matlab as a language. However, if you are used to the behaviour of other object-oriented languages, it can come as a surprise.

What this means is that when you update the properties of an object, you have to assign the updated object to a variable. So your filterState method needs to return obj as a result (say the first), and you need to call it using

[filt, y(ii)] = filt.filterState(x(ii),ii);

or, perhaps more neatly, store y in the object and have another method that just gets the value of y.

There's another way to deal with this: make your class a handle class. This makes its behaviour more like that of Java classes - changes to the object will persist as you expect without an assignment. My own view is that you shouldn't do this unless there is a very convincing reason (e.g. for GUIs or complex data structures that need to be updated), as it breaks the normal Matlab model, but I am sure that many programmers use handle classes for convenience.
 |  Next  |  Last
Pages: 1 2
Prev: Polynomial roots
Next: FFT DNA