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&plus;7t&plus;4

ft2+7t+4

(1)

a0&colon;

b5&colon;

n250&colon;

T&comma;dt&comma;XGenerateSignalf&comma;t&equals;a..b&comma;n&comma;&apos;output&apos;&equals;&apos;times&apos;&comma;&apos;timestep&apos;&comma;&apos;signal&apos;

• 

Now, differentiate:

DXDifferentiateDataX&comma;1&comma;&apos;step&apos;&equals;dt&comma;&apos;method&apos;&equals;&apos;forward&apos;&comma;&apos;extrapolation&apos;&equals;&apos;polynomial&apos;

• 

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

RelativeRootMeanSquareErrorDX&comma;GenerateSignaltf&comma;t&equals;a..b&comma;n

0.00570236649300122009

(2)
• 

Visually:

dataplotT&comma;X&comma;DX&comma;&apos;style&apos;&equals;&apos;line&apos;&comma;&apos;color&apos;&equals;&apos;blue&apos;&comma;&apos;darkgreen&apos;&comma;&apos;legend&apos;&equals;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&plus;sin3t

f5cost+sin3t

(3)

a0&colon;

b2Pi&colon;

n64&colon;

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

• 

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

YGenerateSignaltf&comma;t&equals;a..b&comma;n&comma;&apos;includefinishtime&apos;&equals;&apos;false&apos;&colon;

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

Y2DifferentiateDataX&comma;1&comma;&apos;step&apos;&equals;dt&comma;&apos;method&apos;&equals;&apos;spectral&apos;&colon;

• 

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

RelativeRootMeanSquareErrorY1&comma;Y

0.00753297720724115192

(4)

RelativeRootMeanSquareErrorY2&comma;Y

4.82327325373904705×10−15

(5)
• 

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

ZGenerateSignal2t2f&comma;t&equals;a..b&comma;n&comma;&apos;includefinishtime&apos;&equals;&apos;false&apos;&colon;

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

Z2DifferentiateDataX&comma;2&comma;&apos;step&apos;&equals;dt&comma;&apos;method&apos;&equals;&apos;spectral&apos;&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.0250341382145814959

(6)

RelativeRootMeanSquareErrorZ2&comma;Z

7.35080030302369947×10−14

(7)
• 

Visually:

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

W1GenerateSignaltf&comma;t&equals;a..b&comma;n&comma;&apos;includefinishtime&apos;&equals;&apos;true&apos;&colon;

W2DifferentiateDataV&comma;1&comma;&apos;step&apos;&equals;h&comma;&apos;method&apos;&equals;&apos;spectral&apos;&colon;

RelativeRootMeanSquareErrorW2&comma;W1

0.106321866987499819

(8)

RelativeRootMeanSquareErrorW220..20&comma;W120..20

0.00768251003253226511

(9)

Example 3

• 

First, generate a signal:

fpiecewiset<Pior3Pi<t&comma;sin4t&plus;5&comma;1sin40t10&plus;5

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

(10)

a0&colon;

b4Pi&colon;

n214&colon;

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

• 

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

D1X1DifferentiateDataX&comma;1&comma;&apos;step&apos;&equals;dt&comma;&apos;method&apos;&equals;&apos;spectral&apos;&colon;

D2X1DifferentiateDataX&comma;2&comma;&apos;step&apos;&equals;dt&comma;&apos;method&apos;&equals;&apos;spectral&apos;&colon;

D1X2GenerateSignaltf&comma;t&equals;a..b&comma;n&comma;&apos;includefinishtime&apos;&equals;&apos;false&apos;&colon;

D2X2GenerateSignal2t2f&comma;t&equals;a..b&comma;n&comma;&apos;includefinishtime&apos;&equals;&apos;false&apos;&colon;

RelativeRootMeanSquareErrorD1X1&comma;D1X2

3.52534042480501931×10−7

(11)

RelativeRootMeanSquareErrorD2X1&comma;D2X2

0.0000559512699727594801

(12)
• 

Finally, plot the first and second derivatives:

SignalPlotX&comma;&apos;title&apos;&equals;Plot of Signal&comma;&apos;color&apos;&equals;&apos;blue&apos;

SignalPlotD1X1&comma;&apos;title&apos;&equals;Plot of First Derivative&comma;&apos;color&apos;&equals;&apos;blue&apos;

SignalPlotD2X1&comma;&apos;title&apos;&equals;Plot of Second Derivative&comma;&apos;color&apos;&equals;&apos;blue&apos;

Example 4

• 

Complex data can also be differentiated:

T&comma;dt&comma;XGenerateSignalt2&ExponentialE;It&comma;t&equals;0..5&comma;104&comma;&apos;output&apos;&equals;&apos;times&apos;&comma;&apos;timestep&apos;&comma;&apos;signal&apos;&colon;

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

X1&comma;X2ComplexToRealX&colon;

DX1&comma;DX2ComplexToRealDX&colon;

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

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

Example 5

• 

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

fsint&plus;5&ExponentialE;Itcos3t&plus;10

fsint+5&ExponentialE;Itcos3t+10

(13)

a0

a0

(14)

b2Pi

b2π

(15)

n250

n250

(16)

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

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

• 

Comparing with the actual derivative:

ZGenerateSignaltf&comma;t&equals;a..b&comma;n&comma;&apos;includefinishtime&apos;&equals;&apos;false&apos;&colon;

RelativeRootMeanSquareErrorY&comma;Z

0.00205478287629157685

(17)

Compatibility

• 

Since internal commands are now compiled in external code, the compiled option is now deprecated. For backwards compatibility, the command can still be passed, but it will be ignored.

• 

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 and compiled options were 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]