Iterative For Loop for calculating multiple passes of a moving average?
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (1)
Jyotish Robin
il 16 Gen 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!
Categorie
Scopri di più su Entering Commands in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
