SignalProcessing
DifferentiateData
compute the derivative of a 1-D rtable of data
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
DifferentiateData( X, n, options )
X
-
1-D rtable or list of complexcons data
n
(optional) positive integer, specifies the order of differentiation. The default is 1.
degree: Positive integer, specifies the degree of the polynomial or spline used to extrapolate points for the finite difference and Savitzky-Golay methods. The default is 3.
frameradius: Positive integer, specifies the radius of the frame used for the Savitzky-Golay method. The default is 5.
extrapolation: One of periodic, polynomial, and spline, specifies how the data in X is extrapolated. The default is polynomial.
splinepoints: Positive integer other than 1 or infinity, specifies the maximum number of data points used to perform spline extrapolation. The default is 10.
method: One of backward, central, forward, savitzkygolay, and spectral. The default is central.
step: Positive numeric value, specifies the time step between successive data points. The default is 1.0.
The DifferentiateData(X,n) command computes the derivative of order n for data container X. It is assumed that the data in X originated from a signal sampled uniformly with the time between samples being step.
For the discussion below, assume X is indexed from 1 to m with m being the size of X, and let n be the order of differentiation, T be the size of the time interval over which the signal is sampled, d=degree, and h=step. Moreover, we denote by Dn⁡X the derivative of order n for X.
The three finite difference methods, namely backward, central, and forward, and the savitzkygolay method all use extrapolation on the left and right ends to lengthen X so that the derivative of order n is of the same size as X. This extrapolation can be performed in three ways:
extrapolation=periodic: The data is assumed to be periodic, so that Xi+m=Xi for each integer i.
extrapolation=polynomial: Using the CurveFitting[PolynomialInterpolation] command, interpolating polynomials of degree (at most) d are used to extend X on the left and right.
extrapolation=spline: Using the CurveFitting[ArrayInterpolation] command, splines of degree (at most) d are used to extend X on the left and right.
For method=central, the size of the signal must satisfy 3≤n. For the other methods, 2≤n.
For both method=backward and method=forward, m and n must satisfy n≤m−1, whereas for method=central, they must satisfy n≤m2−12. Moreover, d must satisfy d≤m−1.
For the finite difference methods, we use the standard formulas. For method=forward:
Dn⁡Xi=1hn⁢∑j=0n⁡−1n−j⁢nj⁢Xi+j
For method=backward:
Dn⁡Xi=1hn⁢∑j=0n⁡−1j⁢nj⁢Xi+n−j
For method=central:
Dn⁡Xi=12⁢hn⁢∑j=0n⁡−1j⁢nj⁢Xi+2⁢n−2⁢j
When method=savitzkygolay, the Savitzky-Golay Filter is used to estimate the derivative. The frame radius r should satisfy d<2⁢r and 2⁢r+1≤m, and the degree should satisfy n≤d. Note that the option degree serves the additional role as the degree of the fitting polynomials used in the Savitzky-Golay smoothing.
When method=spectral, the derivative property of the Fourier Transform is used to find the derivative of order n for X:
Compute the FFT Y of X.
Compute the element-wise product Z of Y and H, where H is defined below.
Compute the Inverse FFT Dn⁡X of Z, which is the derivative of order n of X.
The elements of H are given by the following:
Hi=0m::evenandn::oddandi=m2+12⁢I⁢π⁢i−1Tni≤m2+12⁢I⁢π⁢i−m−1Tnotherwise
The spectral method works best when the data represents periodic data, particularly when it is a finite linear combination of sinusoidal terms having frequency no larger than the sample rate. Note: If the original signal is periodic with period T, the final data point should correspond to the final sample before T, since the sample at time T would be the same as time 0. See below for an example.
The DifferentiateData command is most accurate when the signal is not too noisy, the size of the signal is large, and the order of differentiation is small.
Maple will attempt to coerce the provided data container X to a 1-D Vector of either float[8] or complex[8] datatype for the Savitzky-Golay method and any of the finite difference methods, and complex[8] for the spectral differentiation method. An error will be thrown if this is not possible. For this reason, it is most efficient for the passed input to use the appropriate datatype (float[8] or complex[8]).
The data container X must have no indexing function, and use Fortran ordering and rectangular storage.
The DifferentiateData command is not thread safe.
with⁡SignalProcessing:
Example 1
For our first example, we will differentiate data, obtained (for the convenience of comparison) from a polynomial source. First, generate the times and signal Vectors, and note the time step, all using the GenerateSignal command:
f≔−t2+7⁢t+4
a≔0:
b≔5:
n≔250:
T,dt,X≔GenerateSignal⁡f,t=a..b,n,output=times,timestep,signal
Now, differentiate:
DX≔DifferentiateData⁡X,1,step=dt,method=forward,extrapolation=polynomial
Since the data does not have any erratic behavior, we expect this approximation to be good:
RelativeRootMeanSquareError⁡DX,GenerateSignal⁡diff⁡f,t,t=a..b,n
0.00570236649300809566
Visually:
dataplot⁡T,X,DX,style=line,color=blue,darkgreen,legend=Signal,First derivative
Example 2
In this example, we consider real-valued, periodic data. First, generate a signal, along with the times Vector and time step:
f≔5⁢cos⁡t+sin⁡3⁢t
b≔2⁢π:
n≔64:
T,dt,X≔GenerateSignal⁡f,t=a..b,n,includefinishtime=false,output=times,timestep,signal:
Second, compute the first derivative three ways, one directly with the GenerateSignal command (for comparison), and two with the DifferentiateData using different options:
Y≔GenerateSignal⁡diff⁡f,t,t=a..b,n,includefinishtime=false:
Y1≔DifferentiateData⁡X,1,step=dt,method=central,extrapolation=periodic:
Y2≔DifferentiateData⁡X,1,step=dt,method=spectral:
The central approximation for the first derivative is fairly accurate, and the spectral approximation is exceptionally accurate:
RelativeRootMeanSquareError⁡Y1,Y
0.00753297720724038257
RelativeRootMeanSquareError⁡Y2,Y
4.77375076333675125×10−15
Third, compute the second derivative, again in three ways, and calculate the accuracy of the approximations:
Z≔GenerateSignal⁡diff⁡f,`$`⁡t,2,t=a..b,n,includefinishtime=false:
Z1≔DifferentiateData⁡X,2,step=dt,method=central,extrapolation=periodic:
Z2≔DifferentiateData⁡X,2,step=dt,method=spectral:
As with the first derivative, the approximations for the second derivative are fairly accurate (but less so than for the first derivative):
RelativeRootMeanSquareError⁡Z1,Z
0.0250341382145791123
RelativeRootMeanSquareError⁡Z2,Z
6.56329427489268421×10−14
dataplot⁡T,X,Y,Z,style=line,color=blue,darkgreen,purple,legend=Signal,First derivative,Second derivative
When we constructed the data, we omitted the point at t=2⁢π since it corresponds to the point at t=0. If the final point is included, the spectral approximation for the derivative is greatly affected at the endpoints:
U,h,V≔GenerateSignal⁡f,t=a..b,n,includefinishtime=true,output=times,timestep,signal:
W1≔GenerateSignal⁡diff⁡f,t,t=a..b,n,includefinishtime=true:
W2≔DifferentiateData⁡V,1,step=h,method=spectral:
RelativeRootMeanSquareError⁡W2,W1
0.106321866987496502
RelativeRootMeanSquareError⁡W220..−20,W120..−20
0.00768251003253147581
Example 3
First, generate a signal:
f≔piecewise⁡t<πor3⁢π<t,sin⁡4⁢t+5,110⁢sin⁡40⁢t+5
f≔sin⁡4⁢t+5t<πor3⁢π<tsin⁡40⁢t10+5otherwise
b≔4⁢π:
n≔214:
X,dt≔GenerateSignal⁡f,t=a..b,n,includefinishtime=false,output=signal,timestep:
Second, compute the first and second derivatives, both directly and numerically:
D1X1≔DifferentiateData⁡X,1,step=dt,method=spectral:
D2X1≔DifferentiateData⁡X,2,step=dt,method=spectral:
D1X2≔GenerateSignal⁡diff⁡f,t,t=a..b,n,includefinishtime=false:
D2X2≔GenerateSignal⁡diff⁡f,t,t,t=a..b,n,includefinishtime=false:
RelativeRootMeanSquareError⁡D1X1,D1X2
3.52534051901222011×10−7
RelativeRootMeanSquareError⁡D2X1,D2X2
0.0000559512708099739738
Finally, plot the first and second derivatives:
SignalPlot⁡X,title=Plot of Signal,color=blue
SignalPlot⁡D1X1,title=Plot of First Derivative,color=blue
SignalPlot⁡D2X1,title=Plot of Second Derivative,color=blue
Example 4
Complex data can also be differentiated:
T,dt,X≔GenerateSignal⁡t2⁢exp⁡−I⁢t,t=0..5,104,output=times,timestep,signal:
DX≔DifferentiateData⁡X,1,step=dt,method=forward,extrapolation=spline:
X1,X2≔ComplexToReal⁡X:
DX1,DX2≔ComplexToReal⁡DX:
dataplot⁡T,X1,DX1,style=line,title=Signal,legend=Real part,Imaginary part,color=red,blue
dataplot⁡T,X2,DX2,style=line,title=First Derivative,legend=Real part,Imaginary part,color=red,blue
Example 5
The Savitzky-Golay method can also be used to estimate the derivative:
f≔sin⁡t+5⁢exp⁡I⁢t⁢cos⁡3⁢t+10
f≔sin⁡t+5⁢ⅇI⁢t⁢cos⁡3⁢t+10
a≔0
b≔2⁢π
n≔250
dt,X≔GenerateSignal⁡f,t=a..b,n,includefinishtime=false,output=timestep,signal:
Y≔DifferentiateData⁡X,1,step=dt,method=savitzkygolay,frameradius=10,extrapolation=periodic,degree=3:
Comparing with the actual derivative:
Z≔GenerateSignal⁡diff⁡f,t,t=a..b,n,includefinishtime=false:
RelativeRootMeanSquareError⁡Y,Z
0.00205478287629022073
Starting with Maple 2023, external C code is used for the auxiliary procedures that were formerly implemented in Maple and could be compiled by passing the compiled option. The compiled option is now deprecated, but it is still accepted for backwards compatibility.
The SignalProcessing:-DifferentiateData command was introduced in Maple 2022.
For more information on Maple 2022 changes, see Updates in Maple 2022.
The SignalProcessing:-DifferentiateData command was updated in Maple 2023.
The frameradius option was introduced in Maple 2023.
The method option was updated in Maple 2023.
For more information on Maple 2023 changes, see Updates in Maple 2023.
See Also
CurveFitting[ArrayInterpolation]
CurveFitting[PolynomialInterpolation]
SignalProcessing[FFT]
SignalProcessing[GenerateSignal]
SignalProcessing[IntegrateData]
SignalProcessing[SavitzkyGolayFilter]
Download Help Document