Functions > Signal Processing > Time Series Analysis > Example: Linear Detrending
  
Example: Linear Detrending
Use the detrend function to approximate and remove a linear trend from data using a least squares best-fit line.
1. Define an exponential signal.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Plot the exponential signal.
Click to copy this expression
3. Use the whiten function to add white noise to the signal.
Click to copy this expression
4. Plot the original and the noisy signal.
Click to copy this expression
5. Apply the detrend function to the noise-contaminated signal.
Click to copy this expression
6. Plot the original and the detrended functions.
Click to copy this expression
The disappearance of the linear trend can be further demonstrated by looking at the least squares line before and after trend removal.
7. Use the slope and intercept functions to calculate the slope and intercept before and after trend removal.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
This is always the case. The output of detrend is a vector whose least squares line is the horizontal line through the origin, that is, the slope and intercept are both very close to 0.
An effect of detrend is that the mean has been removed from the input data as seen by looking at the DC term before and after trend removal.
8. Use the dft function to calculate the DC (first) term before and after trend removal.
Click to copy this expression
Click to copy this expression
The detrend function performs a simple "preprocessing" task that often precedes a smoothing operation. As mentioned above, detrending is also often a useful first step in spectral estimation.