Iterative For Loop for calculating multiple passes of a moving average?
Show older comments
I am trying to smooth a given data set (y) with multiple passes of a moving average. I have the code to simply do one pass (I am aware of the smooth function)however, this is for an assignment and the instructor wants us to create our own smoothing code.
What I would like to do is supply a vector of different spans, and have the moving average act on the previous moving average. This way I will be able to find a span set that best smoothes the data.
% Creates a moving average with a defined set of spans.
span = [9,11,21];
y_smooth = y;
for i = 1:length(span)
d = [(span(i)-1)/2];
y_smooth = conv(y_smooth,ones(1,d)/d,'same');
end
%
Does this appear to work?
Accepted Answer
More Answers (1)
Jyotish Robin
on 16 Jan 2017
Hi Jacob,
Span parameter usually refers to the number of terms in the moving average, which is the window size If N is the number of neighboring data points on either side of a sample value , then 2N+1 is the span.
If we want to do moving average by convolution having a window size of w, we shall use the following k kernel:
k=[1/w 1/w...1/w 1/w]
Hope it helps!
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!