please interpret this result from the filter function
1 次查看(过去 30 天)
显示 更早的评论
Please help me interpret this result from matlab. The input uses a filter function.
data = [1:0.2:4]';
windowSize = 5;
filter(ones(1,windowSize)/windowSize,1,data)
ans =
0.2000
0.4400
0.7200
1.0400
1.4000
1.6000
1.8000
2.0000
2.2000
2.4000
2.6000
2.8000
3.0000
3.2000
3.4000
3.6000
So the data spans from 1 to 4 with increments of 0.2. Windowsize of 5, I assume, is the length of the moving average filter used. This means it will take an average of 5 consecutive inputs (1 to 1.8, 1.2 to 2.0, and so on).
I don't understand the filter function itself. Is this a moving average filter? Why is the output such? Thanks
0 个评论
采纳的回答
Jan
2018-2-8
编辑:Jan
2018-2-8
You can use an M-version of filter and the debugger to step through the code line by line. This should reveal exactly, how the outputs are created.
function [Y, z] = myFilter(b, a, X, z)
% Author: Jan Simon, Heidelberg, (C) 2018, License: CC BY-SA 3.0
% Direct form II transposed, see: doc filter => More About
n = length(a);
z(n) = 0; % Creates zeros if input z is omitted
b = b / a(1); % normalize parameters
a = a / a(1);
Y = zeros(size(X));
for m = 1:length(Y)
Y(m) = b(1) * X(m) + z(1);
for i = 2:n
z(i - 1) = b(i) * X(m) + z(i) - a(i) * Y(m);
end
end
z = z(1:n - 1);
The the inputs B=ones(1,windowSize)/windowSize and A=1, each element is replaced by the average of windowSize elements. Your data start with 1.0, but the output with 0.2. This happens, because the initial values of the filter status has the default value 0. Therefore there is a transitional effect at the beginning, the filter has to settle at first. You can reduce these effects by setting a better initial state as 4th input Z to the filter command.
See also the faster movmean function introduced in R2016a.
PS. We had some discussions concerning the Copyright (C) note. This does not restrict the usage of this piece of code. According to the terms of use of the forum, all codes are published under the "CC BY-SA 3.0" license. Then "(C)" means, that I have written this code by my own and not copied from anywhere else. It produces exactly the same results as Matlab's filter, so it is worth to mention, that I did not reverse-engineer the original code, because this would be illegal.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Bartlett 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!