Detrending data using MATLAB detrend - how much error?
19 views (last 30 days)
Show older comments
Hey guys,
So my question is pretty conceptual. Let us say I have obtained some data in real time. Lets say I want to detrend that data using MATLAB's built in function.
I know that detrending the data distorts the values of your data set. My question is - will this distortion affect our results very much? For example, if I wanted to use FFT to transform the detrended data in order to obtain the frequency content - just how much will detrending negatively affect my results? How can I quantify the error? Thanks for your time.
0 Comments
Accepted Answer
Dr. Seis
on 15 Oct 2011
I agree with Jan. To be sure, large trends should be removed from data if you are interested in investigating the higher frequency components of your data. How to go about removing this trend is what I had to think about a little more. Currently I use the "detrend" function to remove both constant and linear trends from my seismic data before doing further processing... however, I am starting to rethink the tool I use to do this removal.
Most "real" data can contain constant offsets and, what I believe to be the appearance of, linear trends. In the frequency domain, a constant offset (or the mean of the data) is simply the amplitude at zero frequency. This is easy to remove by either using the detrend(data,'constant') function OR by transforming the data into the frequency domain and changing the zero-frequency amplitude to zero. A linear-looking trend in your data, on the other hand, may actually be a combination of the higher frequency data (you are after) and a very low-frequency sine wave. Since the highest period (or the inverse of your lowest non-zero frequency) you are able to resolve is equal to the length of time of your data, the appearance of a linear trend could mean that your data are essentially superimposed on a sine wave with a period many times greater than that of the highest period you can resolve (which is why at first glance the trend may look linear). You might be wondering.. what does this all mean?
Run the following:
N = 512; % number of samples
dt = 0.1; % time increment (number of seconds per sample)
df = 1 / (N*dt); % frequency increment
Nyq = 1 / (2*dt); % Nyquist frequency
f = -Nyq:df:Nyq-df; % frequency range
t = 0:dt:(N-1)*dt; % time range
a_notrend = 2*rand(1,512)-1; % Make up a timeseries
a_trend = a_notrend + 5*sin(2*pi*df/10*t); % Add a low-frequency sine wave
Next view the data in the time and frequency domain with:
plot(t,a_notrend,'b-',t,a_trend,'r-');
plot(f,abs(fftshift(fft(a_notrend))),'b-',...
f,abs(fftshift(fft(a_trend))),'r-');
Next see what the detrend function does to the data in the time and frequency domain:
plot(t,a_notrend,'b-',t,detrend(a_trend,'linear'),'r-');
plot(f,abs(fftshift(fft(a_notrend))),'b-',...
f,abs(fftshift(fft(detrend(a_trend,'linear')))),'r-');
What is the error (in percent) from using the detrend(data,'linear') function:
plot(f,(abs(fftshift(fft(detrend(a_trend,'linear'))))-...
abs(fftshift(fft(a_notrend))))*100/...
max(abs(fftshift(fft(a_notrend)))),'b-');
You will notice that the lower frequency amplitudes are the only ones that are affected. If in your data (if you were to run a test to see which amplitudes are affected in the frequency domain) these amplitudes were a lot lower than you are interested in, then you might be filtering them out anyway so it might not matter that these amplitudes are a "little" off. However, if these amplitudes correspond to frequencies close to the ones you are interested in, then you might want to consider other detrending options. Since fitting a sine wave can be a pain in the rear, you might be able to try finding the best fit low-order polynomial for example:
a_detrend = a_trend - ...
polyval(polyfit(a_trend,2),t); % fit and remove a 2nd order poly
After I remove any non-constant trend, I generally finish it off by removing the mean from the data, too.
The bottom line is, if you want to preserve your lower frequency amplitudes, then you will need to be a little more careful about how you remove the trend from your data. This means spending some time figuring out the actual trend in your data. If these lower frequencies are going to be filter out anyway, then you probably do not have to worry too much about the detrend tool too much. You will have to run similar tests on your data to find out!
More Answers (1)
Jan
on 13 Oct 2011
The effects depend on the actual calculations. If you meaure the sea level and want to determine the average wave amplitude, the removing of the tides increase the accuracy.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!