23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5 Types of Functions<br />

% FILTFCN. Each time you call FILTFCN with a new filter<br />

% input value, it computes the corresponding new filter<br />

% output value, updating its internal state vector at the<br />

% same time.<br />

%<br />

% [FILTFCN, STATEFCN] = MAKEFILTER(B, A) also returns a<br />

% function (in the form of a function handle, STATEFCN)<br />

% that can return the filter's internal state. The internal<br />

% state vector is in the form of a transposed direct form<br />

% II delay line.<br />

% Initialize state vector. To keep this example a bit<br />

% simpler, assume that a and b have the same length.<br />

% Also assume that a(1) is 1.<br />

v = zeros(size(a));<br />

filtfcn = @iirFilter;<br />

statefcn = @getState;<br />

function yn = iirFilter(xn)<br />

% Update the state vector<br />

v(1) = v(2) + b(1) * xn;<br />

v(2:end-1) = v(3:end) + b(2:end-1) * xn - a(2:end-1) * v(1);<br />

v(end) = b(end) * xn - a(end) * v(1);<br />

% Output is the first element of the state vector.<br />

yn = v(1);<br />

end<br />

function vOut = getState<br />

vOut = v;<br />

end<br />

end<br />

This sample session shows how makeFilter works. Make a filter that has<br />

a decaying exponential impulse response and then call it a few times in<br />

succession to see the output values change:<br />

[filt1, state1] = makeFilter([1 0], [1 -.5]);<br />

5-30

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!