DifferentiateData - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


SignalProcessing

  

DifferentiateData

  

compute the derivative of a 1-D rtable of data

 

Calling Sequence

Parameters

Options

Description

Examples

Compatibility

Calling Sequence

DifferentiateData( X, n, options )

Parameters

X

-

1-D rtable or list of complexcons data

n

-

(optional) positive integer, specifies the order of differentiation. The default is 1.

Options

• 

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.

Description

• 

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 DnX 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 3n. For the other methods, 2n.

• 

For both method=backward and method=forward, m and n must satisfy nm1, whereas for method=central, they must satisfy nm212. Moreover, d must satisfy dm1.

• 

For the finite difference methods, we use the standard formulas. For method=forward:

DnXi=1hnj=0n−1njnjXi+j

• 

For method=backward:

DnXi=1hnj=0n−1jnjXi+nj

• 

For method=central:

DnXi=12hnj=0n−1jnjXi+2n2j

• 

When method=savitzkygolay, the Savitzky-Golay Filter is used to estimate the derivative. The frame radius r should satisfy d<2r and 2r+1m, and the degree should satisfy nd. 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:

1. 

Compute the FFT Y of X.

2. 

Compute the element-wise product Z of  Y and H, where H is defined below.

3. 

Compute the Inverse FFT DnX 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+12Iπi1Tnim2+12Iπim1Tnotherwise

• 

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.

Examples

withSignalProcessing&colon;

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:

ft2+7t+4

ft2+7t+4

(1)

a0&colon;

b5&colon;

n250&colon;

T,dt,XGenerateSignalf&comma;t=a..b&comma;n&comma;output=times&comma;timestep&comma;signal

• 

Now, differentiate:

DXDifferentiateDataX&comma;1&comma;step=dt&comma;method=forward&comma;extrapolation=polynomial

• 

Since the data does not have any erratic behavior, we expect this approximation to be good:

RelativeRootMeanSquareErrorDX&comma;GenerateSignaldifff&comma;t&comma;t=a..b&comma;n

0.00570236649300809566

(2)
• 

Visually:

dataplotT&comma;X&comma;DX&comma;style=line&comma;color=blue&comma;darkgreen&comma;legend=Signal&comma;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:

f5cost+sin3t

f5cost+sin3t

(3)

a0&colon;

b2π&colon;

n64&colon;

T,dt,XGenerateSignalf&comma;t=a..b&comma;n&comma;includefinishtime=false&comma;output=times&comma;timestep&comma;signal&colon;

• 

Second, compute the first derivative three ways, one directly with the GenerateSignal command (for comparison), and two with the DifferentiateData using different options:

YGenerateSignaldifff&comma;t&comma;t=a..b&comma;n&comma;includefinishtime=false&colon;

Y1DifferentiateDataX&comma;1&comma;step=dt&comma;method=central&comma;extrapolation=periodic&colon;

Y2DifferentiateDataX&comma;1&comma;step=dt&comma;method=spectral&colon;

• 

The central approximation for the first derivative is fairly accurate, and the spectral approximation is exceptionally accurate:

RelativeRootMeanSquareErrorY1&comma;Y

0.00753297720724038257

(4)

RelativeRootMeanSquareErrorY2&comma;Y

4.77375076333675125×10−15

(5)
• 

Third, compute the second derivative, again in three ways, and calculate the accuracy of the approximations:

ZGenerateSignaldifff&comma;`$`t&comma;2&comma;t=a..b&comma;n&comma;includefinishtime=false&colon;

Z1DifferentiateDataX&comma;2&comma;step=dt&comma;method=central&comma;extrapolation=periodic&colon;

Z2DifferentiateDataX&comma;2&comma;step=dt&comma;method=spectral&colon;

• 

As with the first derivative, the approximations for the second derivative are fairly accurate (but less so than for the first derivative):

RelativeRootMeanSquareErrorZ1&comma;Z

0.0250341382145791123

(6)

RelativeRootMeanSquareErrorZ2&comma;Z

6.56329427489268421×10−14

(7)
• 

Visually:

dataplotT&comma;X&comma;Y&comma;Z&comma;style=line&comma;color=blue&comma;darkgreen&comma;purple&comma;legend=Signal&comma;First derivative&comma;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,VGenerateSignalf&comma;t=a..b&comma;n&comma;includefinishtime=true&comma;output=times&comma;timestep&comma;signal&colon;

W1GenerateSignaldifff&comma;t&comma;t=a..b&comma;n&comma;includefinishtime=true&colon;

W2DifferentiateDataV&comma;1&comma;step=h&comma;method=spectral&colon;

RelativeRootMeanSquareErrorW2&comma;W1

0.106321866987496502

(8)

RelativeRootMeanSquareErrorW220..20&comma;W120..20

0.00768251003253147581

(9)

Example 3

• 

First, generate a signal:

fpiecewiset<πor3π<t&comma;sin4t+5&comma;110sin40t+5

fsin4t+5t<πor3π<tsin40t10+5otherwise

(10)

a0&colon;

b4π&colon;

n214&colon;

X,dtGenerateSignalf&comma;t=a..b&comma;n&comma;includefinishtime=false&comma;output=signal&comma;timestep&colon;

• 

Second, compute the first and second derivatives, both directly and numerically:

D1X1DifferentiateDataX&comma;1&comma;step=dt&comma;method=spectral&colon;

D2X1DifferentiateDataX&comma;2&comma;step=dt&comma;method=spectral&colon;

D1X2GenerateSignaldifff&comma;t&comma;t=a..b&comma;n&comma;includefinishtime=false&colon;

D2X2GenerateSignaldifff&comma;t&comma;t&comma;t=a..b&comma;n&comma;includefinishtime=false&colon;

RelativeRootMeanSquareErrorD1X1&comma;D1X2

3.52534051901222011×10−7

(11)

RelativeRootMeanSquareErrorD2X1&comma;D2X2

0.0000559512708099739738

(12)
• 

Finally, plot the first and second derivatives:

SignalPlotX&comma;title=Plot of Signal&comma;color=blue

SignalPlotD1X1&comma;title=Plot of First Derivative&comma;color=blue

SignalPlotD2X1&comma;title=Plot of Second Derivative&comma;color=blue

Example 4

• 

Complex data can also be differentiated:

T,dt,XGenerateSignalt2expIt&comma;t=0..5&comma;104&comma;output=times&comma;timestep&comma;signal&colon;

DXDifferentiateDataX&comma;1&comma;step=dt&comma;method=forward&comma;extrapolation=spline&colon;

X1,X2ComplexToRealX&colon;

DX1,DX2ComplexToRealDX&colon;

dataplotT&comma;X1&comma;DX1&comma;style=line&comma;title=Signal&comma;legend=Real part&comma;Imaginary part&comma;color=red&comma;blue

dataplotT&comma;X2&comma;DX2&comma;style=line&comma;title=First Derivative&comma;legend=Real part&comma;Imaginary part&comma;color=red&comma;blue

Example 5

• 

The Savitzky-Golay method can also be used to estimate the derivative:

fsint+5expItcos3t+10

fsint+5&ExponentialE;Itcos3t+10

(13)

a0

a0

(14)

b2π

b2π

(15)

n250

n250

(16)

dt,XGenerateSignalf&comma;t=a..b&comma;n&comma;includefinishtime=false&comma;output=timestep&comma;signal&colon;

YDifferentiateDataX&comma;1&comma;step=dt&comma;method=savitzkygolay&comma;frameradius=10&comma;extrapolation=periodic&comma;degree=3&colon;

• 

Comparing with the actual derivative:

ZGenerateSignaldifff&comma;t&comma;t=a..b&comma;n&comma;includefinishtime=false&colon;

RelativeRootMeanSquareErrorY&comma;Z

0.00205478287629022073

(17)

Compatibility

• 

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

SignalProcessing[FFT]

SignalProcessing[GenerateSignal]

SignalProcessing[IntegrateData]

SignalProcessing[SavitzkyGolayFilter]