Moving average without using movmean() or conv()

Write a function that performs the moving average of any 1-dimensional input data 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. You must not use built in functions like movmean() or conv().
y[n] = 1/M Σx[n − k]
lower limit k= 0 upper limit M −1
I have this so far:
function
t1=0:0.001:1;t2=1.001:0.001:2;t3=2.001:0.001:3;
t=[t1 t2 t3];
x1=10*ones(1,length(t1));
x2=15*ones(1,length(t2));
x3=20*ones(1,length(t3));
x=[x1 x2 x3];
M=20;
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])

11 件のコメント

Ameer Hamza
Ameer Hamza 2020 年 10 月 10 日
Since this is a homework question, show us what have you already tried? Read here: https://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Image Analyst
Image Analyst 2020 年 10 月 10 日
You need to give your function a name. You can't just have "function" on a line all by itself.
Image Analyst
Image Analyst 2020 年 10 月 10 日
OK, you fixed that. So now, what are M and t?
Image Analyst
Image Analyst 2020 年 10 月 11 日
No, that's worse. You pass in M but then you just go on to overwrite whatever was passed in with a different M. And you never said what M and t were like I directly asked.
FS
FS 2020 年 10 月 11 日
Thank you for your help. I understand how to calculate for a single value of M but I don't know how to convert this into a function that accepts any value of M.
FS
FS 2020 年 10 月 11 日
編集済み: FS 2020 年 10 月 11 日
I managed to convert it to a function now, but I think I am missing something in the for loop.
M is the window size.
Image Analyst
Image Analyst 2020 年 10 月 11 日
You need an outer loop over n but not like you have. You need to use M, not k
for n=1:length(x)-(2*M+1) % M is the window width
Then the inner loop needs to be over k where k goes from 0 to M-1, just like your instructions said. Then you need to sum y and divide by M, again, like the instructions said, not like what you did which was to divide by (2*k+1). If you sum a sequence of M elements in y, then divide by M, you get the average value in that segment of y.
FS
FS 2020 年 10 月 11 日
Thank you!
Rik
Rik 2020 年 11 月 7 日
If you want to avoid being caught cheating, you should not post your question to a website that is being cached by Google.
Moving average without using movmean() or conv()
Write a function that performs the moving average of any 1-dimensional input data 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. You must not use built in functions like movmean() or conv().
y[n] = 1/M Σx[n − k]
lower limit k= 0 upper limit M −1
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])
Rena Berman
Rena Berman 2021 年 5 月 7 日

(Answers Dev) Restored edit

Rena Berman
Rena Berman 2021 年 5 月 7 日
(Answers Dev) Restored edit

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2020 年 10 月 10 日

0 投票

I'm attaching a fully manual 2-D convolution. It should be easy for you to adapt it to 1-D

1 件のコメント

Image Analyst
Image Analyst 2020 年 10 月 20 日
I see you're editing the original question to follow my advice in my comment above:
"
You need an outer loop over n but not like you have. You need to use M, not k
for n=1:length(x)-(2*M+1) % M is the window width
Then the inner loop needs to be over k where k goes from 0 to M-1, just like your instructions said. Then you need to sum y and divide by M, again, like the instructions said, not like what you did which was to divide by (2*k+1). If you sum a sequence of M elements in y, then divide by M, you get the average value in that segment of y.
"
So, did that help you? Are we done yet and you can Accept this answer? Or do you still have questions?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

FS
2020 年 10 月 10 日

コメント済み:

2021 年 5 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by