how to differentiation code,?
Mostra commenti meno recenti
i want to wirte x*(1+s) into matlab function block in c code
wher x is input signal and s - is s laplace i.e. use for differentation in matlab
please help
Risposte (1)
Benjamin Thompson
il 1 Feb 2022
Modificato: Benjamin Thompson
il 1 Feb 2022
0 voti
If you have x as sampled signal in time, differentiation is easy: (x(t2) - x(t1)) / (t2 - t1)
7 Commenti
RJS
il 1 Feb 2022
Benjamin Thompson
il 1 Feb 2022
Modificato: Benjamin Thompson
il 1 Feb 2022
Simulink has blocks for differentiating signals. If you are using a continuous time signal, use the Derivative block in the Continuous library. If you are using discrete time signals, use the Discrete Derivative block. You could probably use a MATLAB Function Block, but you need to store the previous value of the input signal which takes some work, and you need access to the value of the time step--see the help article "Define MATLAB Function Block Data" for more information on providing data to this type of block in Simulink.
Walter Roberson
il 1 Feb 2022
Modificato: Walter Roberson
il 2 Feb 2022
Simulink has transfer function blocks
RJS
il 2 Feb 2022
Walter Roberson
il 2 Feb 2022
Simulink Coder can generate code for a Transfer Function block.
Have you considered building the C code as a Level 2 S-Function instead of calling it from a MATLAB Function Block ?
Benjamin Thompson
il 2 Feb 2022
LaPlace notation is used for convenience to work with differential equations algebraically, for analysis or simplification, solving for parameters, etc. If you want a time based numerical solution to a differential or difference equation, you must convert to the time representation. In the case of differentiation that is fairly trivial.
Walter Roberson
il 3 Feb 2022
For any given numeric k, you can construct the numeric tf(), and use c2d() to find the equivalent discrete transfer function, the coefficients of which can be used to filter()
I just spent a while tracing through c2d, as I expected that there might be a relatively simple conversion for this case of 1/(k*s+1) .
Unfortunately, the internal construction of ss form involves a 2^round(log2(sqrt(1/k))) to construct a scaling factor, which makes the conversion a bit more complicated.
I kept looking through anyhow, thinking that at least in theory that might not be necessary, that you might be able to proceed with state space a=-1/k, b = 1, c = 1/k, d = 0, e = [] .
Sadly, inside the routine to discretize from zero order hold, there is a call to the undocumented mex routine matscale
I see hints that maybe you could use
Phi = exp(-Ts/k)
Gamma = k - k*exp(-Ts/k)
and
x[n+1] = Phi * x[n] + Gamma * u[n]
but I am not sure how that would translate into filter() or conv()
Categorie
Scopri di più su Signal Generation in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!