Use Matlab to detrend the data (detrend)

Use Matlab to detrend the data (detrend)

introduce

Detrending (detrend) processing can eliminate the influence of the offset generated by the sensor when acquiring data on the later calculation. Removing trends from data allows the analysis to focus on fluctuations in the data trends themselves. However, the significance of detrending depends on the purpose of one's research.

method

Data detrending is to subtract an optimal (least squares) fitting line, plane or surface from the data, so that the mean value of the detrended data is zero.

example

As an example, use the example that comes with Matlab to demonstrate the detrending process.

Example showing how to remove a linear trend from daily closing stock prices to emphasize overall increasing price volatility. If the data does have a trend, force its trend to zero and reduce the overall variation. This example simulates stock price fluctuations using distributions adopted from library functions.

Matlab complete code source

clc
clear all
close all

%创建一个模拟数据集并计算其平均值。 sdata表示股票的每日价格变化。
t = 0:300;
dailyFluct = gallery('normaldata',size(t),2);
sdata = cumsum(dailyFluct) + 20 + t/100;
%计算均值
mean(sdata)

figure
plot(t,sdata);
legend('Original Data','Location','northwest');
xlabel('Time (days)');
ylabel('Stock Price (dollars)');

%计算去趋势数据,并且从原始数据中移除
detrend_sdata = detrend(sdata);
trend = sdata - detrend_sdata;
mean(detrend_sdata)

hold on
plot(t,trend,':r')
plot(t,detrend_sdata,'m')
plot(t,zeros(size(t)),':k')
legend('Original Data','Trend','Detrended Data',...
       'Mean of Detrended Data','Location','northwest')
xlabel('Time (days)');
ylabel('Stock Price (dollars)');

Write picture description here

おすすめ

転載: blog.csdn.net/wokaowokaowokao12345/article/details/60138308