# How do I remove initial peaks I get while differentiating a harmonic signal twice using simulink ?

10 views (last 30 days)
Utsav Shah on 20 Jun 2017
Commented: Atakan Zeybek on 17 Aug 2020
Please find the attached document for detailed description.

Swarooph on 22 Jun 2017
Hello Utsav,
Thanks for the detailed explanation! There is a fundamental difference in how you are calculating the derivative in MATLAB and Simulink. The primary difference comes from the fact that, if X is a vector of length m, then Y = diff(X) returns a vector of length m-1. The elements of Y are the differences between adjacent elements of X. So the one less element is attributed to the fact that the first element doesn't have an adjacent one before it. This is why you don't see the initial peaks in MATLAB. You can check this using the following code snippet:
>> sineInput = sin(linspace(0,3*pi,1001));
>> size(sineInput)
ans =
1 1001
>> invdt = 1/0.01;
>> der1 = invdt*diff(sineInput);
>> size(der1)
ans =
1 1000
>> der2 = invdt*diff(der1);
>> size(der2)
ans =
1 999
However in Simulink, length of the signal at input and output of the derivative block should be equal. Because everything is time based. All signals (barring a very few exceptions) will carry a value at all times. This means even for the first instant, the previous value for all signals (initial condition) is assumed to be 0 (mostly, unless otherwise specified). So for e.g. at the first instant:
sineInput = 0.01
derivative1 = (sineInput - sineInput_prev)/dt = (0.01-0)/0.01 = 1
derivative2 = (derivative1 - derivative1_prev)/dt = (1-0)/0.01 = 100 <------ This is the peak that you see
That being said, the following information from the documentation for the derivative block might be useful: Because of the derivative block sensitivities, structure your models to use integrators (such as Integrator blocks) instead of Derivative blocks. Integrator blocks have states that allow solvers to adjust step size and improve accuracy of the simulation. See Circuit Model for an example of choosing the best-form mathematical model to avoid using Derivative blocks in your models.

Vaidyanathan Narayanan on 24 Jun 2017
Hi Swarooph,
Thank you for the detailed explanation.
Actually I was aware about the reasons of peak occurring in simulink and not occurring in matlab.
Can you also suggest a way to include double-differentiation block(or something similar) in simulink and avoid the first two data points(peak values) from going into my subsystem block ?
Atakan Zeybek on 17 Aug 2020
Hi Vaidyanathan,
I am also in trouble with the same issue. Is there anybody have the solution?