目录
detrend函数的功能是去除多项式趋势。
语法
y = detrend(x)
y = detrend(x,n)
y = detrend(x,n,bp)
y = detrend(___,nanflag)
y = detrend(___,Name,Value)
说明
y = detrend(x) 从 x 的数据中去除最佳直线拟合线。
-
如果 x 是向量,则 detrend 从 x 的元素中减去趋势。
-
如果 x 是矩阵,则 detrend 分别对每列进行运算,从对应的列中减去每个趋势。
y = detrend(x,n) 去除 n 次多项式趋势。例如,当 n = 0 时,detrend 从 x 中删除均值。当 n = 1 时,detrend 去除线性趋势,这等效于上述语法。当 n = 2 时,detrend 去除二次趋势。
y = detrend(x,n,bp) 去除由断点 bp 定义段的连续分段趋势。
y = detrend(___,nanflag) 指定在使用上述任一语法时如何处理 NaN 值。例如,detrend(x,'omitnan') 在计算趋势之前删除 NaN 值,而 detrend(x,'includenan') 包括这些值(默认)。
y = detrend(___,Name,Value) 使用一个或多个名称-值对组指定其他参数。例如,detrend(x,1,bp,'Continuous',false) 指定拟合趋势可以有不连续趋势。
示例
连续线性趋势
创建一个数据向量,并去除连续线性趋势。绘制原始数据、去趋势后的数据和线性趋势。
t = 0:20;
x = 3*sin(t) + t;
y = detrend(x);
plot(t,x,t,y,t,x-y,':k')
legend('Input Data','Detrended Data','Trend','Location','northwest')
如图所示:
连续二次趋势
创建一个数据向量,并去除连续二次趋势。绘制原始数据、去趋势后的数据和趋势。
t = 0:20;
x = 20*sin(t) + t.^2;
y = detrend(x,2);
plot(t,x,t,y,t,x-y,':k')
legend('Input Data','Detrended Data','Trend','Location','northwest')
如图所示:
不连续线性趋势
创建一个数据向量,并使用 0 处的断点去除分段线性趋势。指定所得到的输出可以是不连续的。绘制原始数据、去趋势后的数据和趋势。
t = -10:10;
x = t.^3 + 6*t.^2 + 4*t + 3;
bp = 0;
y = detrend(x,1,bp,'SamplePoints',t,'Continuous',false);
plot(t,x,t,y,t,x-y,':k')
legend('Input Data','Detrended Data','Trend','Location','northwest')
如图所示: