我找不到为什么filter
函数似乎输出的内容与smoothdata
函数不同。它们都应该使用移动平均实现5
平滑的windowSize。精通这些函数的人能解释一下发生了什么吗?
这里是文档的>link<,下面的代码从那里改编而来,添加了smoothdata
(对于其他难题,smooth
函数也不同):
% from documentation
t = linspace(-pi,pi,100);
rng default %initialize random number generator
x = sin(t) + 0.25*rand(size(t));
windowSize = 5;
b = (1/windowSize)*ones(1,windowSize);
a = 1;
y = filter(b,a,x);
%this is added for this example, you need >Matlab 2017a to run this
y2=smoothdata(x,'movmean',windowSize)
%y3=smooth(x,100); %bug in the code (obsolete)
y3=smooth(x,windowSize);
%now plot data
figure;
plot(t,x)
hold on
plot(t,y)
plot(t,y2)
plot(t,y3)
legend('Input Data','Filtered Data','smoothdata','smooth')
%show obvious parts of plot
xlim([0 3]);
ylim([0 1.25]);
下面是我得到的输出:
以下是图的第一部分的一些不一致之处:
%this is added for this example, you need >Matlab 2017a to run this
y2 = smoothdata(x,'movmean',[windowSize-1,0]);
y3 = smoothdata(padarray(x,[0 2]),'movmean',[windowSize-1,0]);
%now plot data
figure(1); clf;
plot(t,x)
hold on
plot(t(1:10),y(1:10))
plot(t(1:10),y2(1:10))
plot(t(1:10),y3(1:10))
legend('Input Data','Filtered Data','smoothdata',['padded ' char(10) 'smoothdata'])
%show obvious parts of plot
xlim([-3.1784 -2.3858]);
发布于 2018-06-06 03:29:44
如果是标量,window
parameter for smoothdata
将生成一个以原点为中心的窗口。filter
实现了一个因果过滤器,这意味着它取windowSize
先前样本的平均值。因此,这两个结果之间的差异是windowSize/2
样本的偏移。你可以在你的图中清楚地看到这种转变。使用两个参数来模拟filter
的结果
y2 = smoothdata(x,'movmean',[windowSize-1,0])
smooth
function还实现了移动平均,第二个参数是窗口大小。这里使用的是100
,而不是值为5
的windowSize
。因此,这个结果是超过20倍的平均值。使用正确的窗口大小复制结果:
y3 = smooth(x,windowSize);
我猜想y3
会被调到w.r.t。filter
的结果,就像问题中的y2
一样。
https://stackoverflow.com/questions/50707331
复制相似问题