Remove polynomial trend
collapse all in page
Syntax
D = detrend(A)
D = detrend(A,n)
D = detrend(A,n,bp)
D = detrend(___,nanflag)
D = detrend(___,Name,Value)
Description
example
D = detrend(A)
removes the best straight-fit line from the data in A
and returns the remaining data.
If
A
is a vector, thendetrend
subtracts the trend from the elements ofA
.If
A
is a matrix, thendetrend
operates on each column separately, subtracting each trend from the corresponding column ofA
.If
A
is a multidimensional array, thendetrend
operates column-wise across all dimensions, subtracting each trend from the corresponding column ofA
.If
A
is a table or timetable with numeric variables of typesingle
ordouble
, thendetrend
operates on each variable ofA
separately, subtracting each trend from the corresponding variable ofA
.
You can use detrend
functionality interactively by adding the Find and Remove Trends task to a live script.
example
D = detrend(A,n)
removes the n
th-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,n,bp)
removes a continuous, piecewise trend with segments defined by the breakpoints specified in the vector bp
.
D = detrend(___,nanflag)
specifies whether to include or omit NaN
values in A
for any of the previous syntaxes. For example, detrend(A,"omitnan")
ignores NaN
values when computing the trend. By default, detrend
includes NaN
values.
example
D = detrend(___,Name,Value)
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.
Examples
collapse all
Continuous Linear Trend
Open Live Script
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 onplot(t,D)plot(t,A-D,":k")legend("Input Data","Detrended Data","Trend")
Continuous Quadratic Trend
Open Live Script
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 onplot(T,"t","x_detrended")plot(T.t,T.x-T.x_detrended,":k")legend("Input Data","Detrended Data","Trend","Location","northwest")
Discontinuous Linear Trend
Open Live Script
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 + 6*t.^2 + 4*t + 3;bp = 0;D = detrend(A,1,bp,"SamplePoints",t,"Continuous",false);plot(t,A)hold onplot(t,D)plot(t,A-D,":k")legend("Input Data","Detrended Data","Trend","Location","northwest")
Input Arguments
collapse all
A
— Input data
vector | matrix | multidimensional array | table | timetable
Input data, specified as a vector, matrix, multidimensional array, table, or timetable.
If
A
is a vector, thendetrend
subtracts the trend from the elements ofA
.If
A
is a matrix, thendetrend
operates on each column separately, subtracting each trend from the corresponding column ofA
.If
A
is a multidimensional array, thendetrend
operates column-wise across all dimensions, subtracting each trend from the corresponding column ofA
.If
A
is a table or timetable with numeric variables of typesingle
ordouble
, thendetrend
operates on each variable ofA
separately, subtracting each trend from the corresponding variable ofA
.
Data Types: double
| single
Complex Number Support: Yes
n
— Polynomial degree
nonnegative integer scalar | "constant"
| "linear"
Polynomial degree, specified as a nonnegative integer scalar, or as "constant"
(equivalent to 0
) or "linear"
(equivalent to 1
).
bp
— Breakpoints
vector
Breakpoints to define piecewise segments of the data, specified as a vector containing one of the following:
Sample point values indicating the location of the breakpoints. Sample point values are contained either in the default sample points vector
[1 2 3 ...]
or specified by theSamplePoints
name-value argument.Logical values where logical
1
(true
) indicates a breakpoint in the corresponding element of the input data. Ifbp
contains logical values, it must be the same length as the sample points.
Breakpoints are useful when you want to compute separate trends for different segments of the data.
Data Types: double
| single
| datetime
| duration
| logical
nanflag
— Missing value condition
"includemissing"
(default) | "includenan"
| "omitmissing"
| "omitnan"
Missing value condition, specified as one of these values:
"includemissing"
or"includenan"
— IncludeNaN
values inA
when computing the trend. If any element in the operating dimension isNaN
, then the corresponding elements inD
areNaN
."includemissing"
and"includenan"
have the same behavior."omitmissing"
or"omitnan"
— IgnoreNaN
values inA
when computing the trend. If all elements in the operating dimension areNaN
, then the corresponding elements inD
areNaN
."omitmissing"
and"omitnan"
have the same behavior.
Name-Value Arguments
Specify optional pairs of arguments as Name1=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)
Continuous
— Continuity constraint
true
(default) | false
Continuity constraint, specified as one of these values:
true
— The fitted trend must be continuous everywhere.false
— The fitted trend can contain discontinuities.
SamplePoints
— Sample points
vector | table variable name | scalar | function handle | table vartype
subscript
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:
|
|
Variable index:
|
|
Function handle:
|
|
Variable type:
|
|
Note
This name-value argument is not supported when the input data is a timetable
. 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
DataVariables
— Table or timetable variables to operate on
table variable name | timetable variable name | scalar | vector | cell array | pattern | function handle | table vartype
subscript
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 by DataVariables
pass through to the output without being detrended.
Indexing Scheme | Examples |
---|---|
Variable names:
|
|
Variable index:
|
|
Function handle:
|
|
Variable type:
|
|
For vector, matrix, or multidimensional array input data, DataVariables
is not supported.
Example: detrend(A,"DataVariables",["Var1" "Var2" "Var4"])
ReplaceValues
— Replace values indicator
true
or 1
(default) | false
or 0
Replace values indicator, specified as one of these values when A
is a table or timetable:
true
or1
— Replace input table variables with table variables containing detrended data.false
or0
— Append input table variables with table variables containing detrended data.
For vector, matrix, or multidimensional array input data, ReplaceValues
is not supported.
Example: detrend(A,"ReplaceValues",false)
Tips
The
detrend
function subtracts the mean or a best-fit line (in the least-squares sense) from your data. If your data is tabular or contains several data columns or is a table or timetable,detrend
treats each data column separately.Removing a trend from the data enables you to focus your analysis on the fluctuations in the data about the trend. A linear trend typically indicates a systematic increase or decrease in the data. A systematic shift can result from sensor drift, for example. While trends can be meaningful, some types of analyses yield better insight once you remove trends.
Whether it makes sense to remove trend effects in the data often depends on the objectives of your analysis.
Alternative Functionality
Live Editor Task
You can use detrend
functionality interactively by adding the Find and Remove Trends task to a live script.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If the input argument
bp
is supplied but not logical, the argument must be sorted in ascending order.If the input argument
bp
is supplied but variable-sizing disabled, the argument must contain integers in the interval[1,m-2]
. In this case,m
is the number of elements in a column of the input argumentA
or the number of elements inA
whenA
is a row vector (m = length(A)
).If the
SamplePoints
name-value argument is specified, variable-sizing is required.If polynomial degree
n
is neither a constant nor a logical, variable-sizing is required.Code generation uses a different method than MATLAB® to detect nonunique or ill-conditioned problems to issue warnings. The code generation warnings do not always match the MATLAB warnings.
The function
detrend
does not userand
in code generation.Code generation for detrending table or timetable variables is not supported.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
GPU arrays as table or timetable variables are not supported.
Version History
Introduced before R2006a
expand all
R2023a: Specify missing value condition
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.
R2022b: Detrend tabular data
When detrending table or timetable data, you can:
Specify tabular variables to detrend by using the
DataVariables
name-value argument.Append or replace tabular variables with variables containing detrended data by using the
ReplaceValues
name-value argument.Specify the sample points as a table variable by using the
SamplePoints
name-value argument.SamplePoints
is not supported when the input data is a timetable.
See Also
Functions
- trenddecomp | mean | ischange | polyfit | polyval
Live Editor Tasks
- Find and Remove Trends
Topics
- Remove Linear Trends from Timetable Data
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office