How to calculate in v(k) - v(k-1) in M file

Hi all!
How to calculate v(k) - v(k-1) in M file. v(k) is not in the table/array, but it is an input signal from Simulink model (e.g. voltage) to Matlab Fnc block.
In Simulink (without using Matlab Fnc blocks) I use Variable Transport Delay. But now I want to write as much code as possible in M file.
Thanks.

Answers (2)

function e=fcn(v)
persistent vp
if isempty(vp)
vp=0;
end
e=v-vp;
vp=v;
Wayne King
Wayne King on 12 Dec 2013
Edited: Wayne King on 12 Dec 2013
you can use the diff() function in MATLAB. That is v(k)-v(k-1)
Also in R2013b if you have DSP System Toolbox, you can use the System object
dsp.FIRFilter
and then use the System block (create Simulink block from the System object)
hfir = dsp.FIRFilter('Numerator',[1 -1]);
x = 1:10;
x = x.^2;
y = step(hfir,x');

Categories

Find more on Modeling in Help Center and File Exchange

Tags

Asked:

on 12 Dec 2013

Edited:

on 12 Dec 2013

Community Treasure Hunt

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

Start Hunting!