Symmetric/Centered Moving Average
Mostrar comentarios más antiguos
Hi, I am trying to get a report done, but I am having some trouble when it comes to building a symmetric moving average. This may not be a big issue, but I am a beginner in MATLAB, so....
I spent the whole day trying the filter and conv commands that I saw in various answers, but no success; nothing matches the way the graphs should look like;
I already generated the data vectors. All of lenght N = 200 + 1 (200 samples, but since it starts from 0, ...)
I have the data and the corresponding time axis - for example yr is the signal and xr is its time axis - 0 to 0.02s spaced at 0.0001s
The requirements say - the user inputs one of the 3 signals he wants to 'move-average' and an odd integer between 3 and 21 for the number of points;
Let M be number of points and dev the number of points on each side of the center
Since it gets tricky when the index is less than the dev and greater than N - dev, I thought of assigning these entries directly. They would get exactly the signal's values
y = zeros(1,201);
M = 11;
dev = (M-1)/2;
N = 200;
%this would fill y from 1 to 5
for i=1:dev
y(i) = yr(i); %yr is 1x201
end
%this would fill y from 196 to 201
for i = N-dev+1:N+1
y(i) = yr(i);
end
%Now, how do I fill the middle area, from, say, 6 to 195?
%My problem is because depending on M, the terms on each side of the center
%are different; for example, if M is 11, I'd have say, y(6) = 5 entries +
%yr(6) + 5 entries, but if M is, say, 9, the I'd have y(5) = 4 entries +
%yr(5) + 4 entries and so on;
%That's it;
%Can you please tell me how to set up the for loops to create this moving
%averaged centered around a given point?
%The signals are all 1x201; that's 200 samples; sampled at fs = 10kHz;
%To have something to try on, you can use a test signal like:
N = 200;
fs = 1/10000;
A = 0.5;
x = 0 : dt : N/fs;
B = -A;
rng(0);
y = A + (B-A).*rand(size(x));
% Now, y is a 1x201 signal and we want to move-average it, and the number of points is 11;
%What do the loops look like?
%Thank you so much!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!