t = 0:0.02:5; %データの作成
x = 2*sin(2*pi*1*t);
noise = randn(1, length(t));
x1 = x + noise;
y3 = x*0; %これより3点MA
for k=2: length(x)-1
y3(k) = (x(k-1) + x(k) + x(k+1))/3;
end
y3(1) = (x(1)+x(2))/2;
y3(end) = (x(end-1) + x(end))/2;
y5 = x*0; %これより5点MA
for k=3: length(x)-2
y5(k) = (x(k-2)+x(k-1)+x(k)+x(k+1)+x(k+2))/5;
end
y5(1) = (x(1) + x(2) + x(3))/3;
y5(2) = (x(1) + x(2) + x(3) + x(4))/4;
y5(end-1) = (x(end-3) + x(end-2) + x(end-1) + x(end))/4;
y5(end) = (x(end-2) + x(end-1) + x(end))/3;
y11 = x*0; %これより11点MA
N = length(x);
for k=6: N-5
y11(k) = mean(x(k-5:k+5));
end
y11(1) = mean(x(1:6));
y11(2) = mean(x(1:7));
y11(3) = mean(x(1:8));
y11(4) = mean(x(1:9));
y11(5) = mean(x(1:10));
y11(N-4) = mean(x(N-5:N));
y11(N-3) = mean(x(N-4:N));
y11(N-2) = mean(x(N-3:N));
y11(N-1) = mean(x(N-2:N));
y11(N) = mean(x(N-1:N));
y10 = movmean(x,10); %これより10点MA
y07 = movmean(x1,7); %3つ目の課題
y0703 = movmean(y07,3);
y03 = movmean(x1,3);
y0307 = movmean(y03,7);
figure (1)%グラフの描画
plot(t, x, 'bo-', t, y3, 'r', t, y5, 'g', t, y11, 'y');
legend('x(k): signal', 'y3(k): after 3MA', 'y5(k): after 5MA', 'y11(k): after 11MA')
title('3点5点11点MA'); % タイトルを記入
figure (2)
plot(t, x, 'bo-', t, y3, 'r', t, y5, 'g', t, y11, 'y');
legend('x(k): signal', 'y3(k): after 3MA', 'y5(k): after 5MA', 'y11(k): after 11MA')
title('3点5点11点MA');
xlim([-0.147 0.859])
ylim([1.249 2.196])
figure (3)
plot(t,x, 'b', t, y10, 'r', t, y11, 'g')
legend('x(k): signal', 'y10(k): after 10MA', 'y11(k): after 11MA')
title('10点11点MA');
xlim("auto")
ylim("auto")
figure (4)
plot(t,x1, 'b', t, y0307, 'r', t, y0703, 'g')
legend('x1(k): signal+noise', 'y0307(k): after 3MA7MA', 'y0703(k): after 7MA3MA')
figure (4)
plot(t,x1, 'b', t, y0307, 'r', t, y0703, 'g')
legend('x1(k): signal+noise', 'y0307(k): after 3MA7MA', 'y0703(k): after 7MA3MA')
title('')
xlim([0.364 1.188])
ylim([-2.34 -0.17])