Symmetric/Centered Moving Average

18 visualizaciones (últimos 30 días)
Fellipi Henrique Barbosa do Carmo
Fellipi Henrique Barbosa do Carmo el 28 de Nov. de 2021
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

Steven Lord
Steven Lord el 28 de Nov. de 2021
Take a look at the movmean function.
  3 comentarios
Steven Lord
Steven Lord el 1 de Dic. de 2021
So near the edges you're making the window narrower on both sides rather than just ignoring the elements outside the array like the 'shrink' value for the 'Endpoints' name-value pair argument does? Is that a technique that's generally used in your field or is it something that's specific to the particular problem you're trying to solve?
Fellipi Henrique Barbosa do Carmo
Fellipi Henrique Barbosa do Carmo el 1 de Dic. de 2021
  • Yes.
  • It's specific to the problem! Were it any other problem I'd just use the values of the original signal on both sides! For example, if my window was 15 and I had 200 points, then I'd use the original values from 1 to 7 and from 194 to 200. But to be honest, we don't do this sort of nonreal time signal processing, where you already have the data and can use 'future data', we rather do real-time DSP.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by