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.
2. Plot the exponential signal.
3. Use the whiten function to add white noise to the signal.
4. Plot the original and the noisy signal.
5. Apply the detrend function to the noise-contaminated signal.
6. Plot the original and the detrended functions.
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.
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.
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.