LTPDA Toolbox | contents | ![]() ![]() |
The method ao/detrend offers the possibility to remove polynomial trends from a data series.
The method can be configured with the following parameter:
Key | Description |
---|---|
N |
The order of the polynomial to fit and remove. For orders below 10, a very fast C-code algorithm is used. For higher orders, the MATLAB functions polyfit and polyval are used to construct the polynomial which is then subtracted from the data. |
In this example we will construct a time-series that consists of noise plus a known quadratic trend. We will then remove that trend using detrend and compare the detrended time-series with the original noise.
First let's create the time-series series consisting of the noise plus trend.
% Construct noise data stream pl = plist('tsfcn','5+randn(size(t))','fs',10,'nsecs',10,'yunits','V'); x = ao(pl); % Construct a quadratic data series pl_trend = plist('tsfcn','t.^2','fs', 10,'nsecs',10); trend = ao(pl_trend); % Add them together fcn_trend = x+trend;
The offset of 5 is added to the noise to ensure the data series doesn't come close to zero; we want to divide by it later in the example. |
Next we will detrend the data and compare the result to the noise data x we made above.
pl_detr = plist('N',2); detr = detrend(fcn_trend,pl_detr); iplot(x,fcn_trend,detr, plist('LineStyles', {'', '', '--'}));
In the plist we specified 'LineStyles' as empty strings. These just serve as place holders and can be interpreted as "just to the default". If you want a data-series plotted with no line, then specify 'none', for example, {'none', '-', '--'}. |
From this plot, it is not very easy to see how well our detrending worked. Let's form the fractional difference of the original x data and the detrended data and plot that instead.
detr = detr+5; diff = 100.*(x-detr)./x; iplot(diff);
The result is shown below. We added the value 5 to the detrended time-series just to ensure that we don't divide by any values close to zero.
Try increasing the length of the data series to say, 1000 or 10000 seconds, to see how the detrending improves.
Below is an example pipeline to perform the steps we did above:
This introduces a new concept to the pipelines, namely, the use of constant blocks. Constant blocks are executed before the rest of the pipeline and the values are placed in the MATLAB workspace. This means that all parameter lists on the pipeline can refer to these constants. For example, the pipeline above declares two constants: 'fs' and 'nsecs'. The two ao blocks refer to these. Below is the parameter list for the first ao block, noise.
If you want to change the length of this simulation, then you just need to change the value in the constant block, nsecs.
To add constant blocks to your pipeline, right-click on the canvas and select "Additional Blocks->Constant" from the context menu. You can also add an annotation from the same context menu. The above pipeline shows one annotation. To edit the text on an annotation, double-click it. Right-clicking on an annotation gives a context menu that allows you to configure its appearance.
![]() |
Interpolation of a time-series AO | Whitening noise | ![]() |
©LTP Team