detrend - Remove polynomial trend - MATLAB (original) (raw)

Syntax

Description

D = detrend([A](#d126e406195)) removes the best straight-fit line from the data in A and returns the remaining data.

You can use detrend functionality interactively by adding theFind and Remove Trends task to a live script.

example

D = detrend([A](#d126e406195),[n](#mw%5F70f4f3ea-945e-4305-9511-0fae3694ba6b)) removes the nth-degree polynomial trend. For example, when n = 0, detrend removes the mean value from A. When n = 1, detrend removes the linear trend, which is equivalent to the previous syntax. When n = 2, detrend removes the quadratic trend.

example

D = detrend([A](#d126e406195),[n](#mw%5F70f4f3ea-945e-4305-9511-0fae3694ba6b),[bp](#mw%5F28f12450-18a0-4c71-b476-c2d29b588e1c)) removes a continuous, piecewise trend with segments defined by the breakpoints specified in the vector bp.

example

D = detrend(___,[nanflag](#mw%5F680464dd-e0b7-4afd-a928-160d0a1e5401)) specifies whether to include or omit NaN values in A for any of the previous syntaxes. For example, detrend(A,"omitnan") ignoresNaN values when computing the trend. By default,detrend includes NaN values.

D = detrend(___,[Name,Value](#namevaluepairarguments)) specifies additional parameters using one or more name-value arguments. For example,detrend(A,1,bp,"Continuous",false) specifies that the fitted trend can have discontinuities.

example

Examples

collapse all

Create a vector of data, and remove the continuous linear trend. Plot the original data, the detrended data, and the linear trend.

t = 0:20; A = 3*sin(t) + t; D = detrend(A);

plot(t,A) hold on plot(t,D) plot(t,A-D,":k") legend("Input Data","Detrended Data","Trend")

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent Input Data, Detrended Data, Trend.

Create a table of data, and remove the continuous quadratic trend from a specified variable in the table. Plot the original data, the detrended data, and the trend.

t = (-4:4)'; trend = (t.^2 + 4*t + 3); sig = [0 1 -2 1 0 1 -2 1 0]'; x = sig + trend; T = table(t,trend,sig,x); T = detrend(T,2,"DataVariables","x","SamplePoints","t","ReplaceValues",false)

T=9×5 table t trend sig x x_detrended __ _____ ___ __ ___________

-4      3       0      3     -0.12121  
-3      0       1      1       0.9697  
-2     -1      -2     -3      -1.9654  
-1      0       1      1       1.0736  
 0      3       0      3      0.08658  
 1      8       1      9       1.0736  
 2     15      -2     13      -1.9654  
 3     24       1     25       0.9697  
 4     35       0     35     -0.12121  

plot(T,"t","x") hold on plot(T,"t","x_detrended") plot(T.t,T.x-T.x_detrended,":k") legend("Input Data","Detrended Data","Trend","Location","northwest")

Figure contains an axes object. The axes object with xlabel t contains 3 objects of type line. These objects represent Input Data, Detrended Data, Trend.

Create a vector of data, and remove the piecewise linear trend using a breakpoint at 0. Specify that the resulting output can be discontinuous. Plot the original data, the detrended data, and the trend.

t = -10:10; A = t.^3 + 6t.^2 + 4t + 3; bp = 0; D = detrend(A,1,bp,"SamplePoints",t,"Continuous",false);

plot(t,A) hold on plot(t,D) plot(t,A-D,":k") legend("Input Data","Detrended Data","Trend","Location","northwest")

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent Input Data, Detrended Data, Trend.

Input Arguments

collapse all

Input data, specified as a vector, matrix, multidimensional array, table, or timetable.

Data Types: double | single
Complex Number Support: Yes

Polynomial degree, specified as a nonnegative integer scalar, or as"constant" (equivalent to 0) or"linear" (equivalent to 1).

Breakpoints to define piecewise segments of the data, specified as a vector containing one of the following:

Breakpoints are useful when you want to compute separate trends for different segments of the data.

Data Types: double | single | datetime | duration | logical

Missing value condition, specified as one of these values:

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: D = detrend(A,SamplePoints=1:10:1000)

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: D = detrend(A,"SamplePoints",1:10:1000)

Continuity constraint, specified as one of these values:

Sample points, specified as a vector of sample point values or one of the options in the following table when the input data is a table. The sample points represent the_x_-axis locations of the data, and must be sorted and contain unique elements. Sample points do not need to be uniformly sampled. The vector[1 2 3 ...] is the default.

When the input data is a table, you can specify the sample points as a table variable using one of these options:

Indexing Scheme Examples
Variable name: A string scalar or character vector "A" or 'A' — A variable namedA
Variable index: An index number that refers to the location of a variable in the tableA logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 orfalse values 3 — The third variable from the table[false false true] — The third variable
Function handle: A function handle that takes a table variable as input and returns a logical scalar @isnumeric — One variable containing numeric values
Variable type: A vartype subscript that selects one variable of a specified type vartype("numeric") — One variable containing numeric values

Note

This name-value argument is not supported when the input data is atimetable. Timetables use the vector of row times as the sample points. To use different sample points, you must edit the timetable so that the row times contain the desired sample points.

Example: detrend(A,"SamplePoints",0:0.1:10)

Data Types: double | single | datetime | duration

Table or timetable variables to operate on, specified as one of the options in this table. The DataVariables value indicates for which variables of the input table or timetable to remove polynomial trends.

Other variables in the table or timetable not specified byDataVariables pass through to the output without being detrended.

Indexing Scheme Values to Specify Examples
Variable name A string scalar or character vectorA string array or cell array of character vectorsA pattern object "A" or 'A' — A variable named A["A" "B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index An index number that refers to the location of a variable in the tableA vector of numbersA logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 (false) values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Function handle A function handle that takes a table variable as input and returns a logical scalar @isnumeric — All the variables containing numeric values
Variable type A vartype subscript that selects variables of a specified type vartype("numeric") — All the variables containing numeric values

For vector, matrix, or multidimensional array input data,DataVariables is not supported.

Example: detrend(A,"DataVariables",["Var1" "Var2" "Var4"])

Replace values indicator, specified as one of these values whenA is a table or timetable:

For vector, matrix, or multidimensional array input data,ReplaceValues is not supported.

Example: detrend(A,"ReplaceValues",false)

Tips

Alternative Functionality

Live Editor Task

You can use detrend functionality interactively by adding theFind and Remove Trends task to a live script.

Find and Remove Trends task in the Live Editor

Extended Capabilities

expand all

Usage notes and limitations:

The detrend function supports GPU array input with these usage notes and limitations:

Version History

Introduced before R2006a

expand all

Include or omit missing values in the input array when computing the trend by using the"includemissing" or "omitmissing" options. These options have the same behavior as the "includenan" and"omitnan" options, respectively.

When detrending table or timetable data, you can: