y[n] = 1/M Σx[n − k]
lower limit k= 0 upper limit M −1
Write a function that performs the moving average for any non-zero and positive order M. Make sure to add M-1 zeros at the beginning of the data so that your output data will be the same length as your input data.
I have this so far:
function [movaverage] = movaverage(M)
M=zeros(1,length(M));
for i=1:length(t)-M
y1(i)=(1/M)*sum(i:i+M-1);
end
y=[zeros(1,M) y1];
figure
subplot(211)
plot(t,x,'b')
ylabel('Signal')
axis([0 3 0 30])
subplot(212)
plot(t,y,'r')
ylabel('Moving avg')
xlabel('Time (s)')
axis([0 3 0 inf])
9 Comments
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1049586
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1049586
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1049866
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1049866
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1049946
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1049946
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1050561
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1050561
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1050716
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1050716
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1051131
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1051131
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1051251
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1051251
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1051616
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1051616
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1117380
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/609896-calculate-moving-average-manually#comment_1117380
Sign in to comment.