t = 0:0.02:5; %データの作成
s = 2*sin(2*pi*1*t);
x = s + randn(1, length(t));
y = x*0; %これより3点MA
for k=2: length(x)-1
y(k) = (x(k-1) + x(k) + x(k+1))/3;
end
y(1) = (x(1)+x(2))/2;
y(end) = (x(end-1) + x(end))/2;
figure (2) %グラフの描画
plot(t, x, 'b', t, y, 'r', t, s, 'k');
legend('x(k): before MA', 'y(k): after MA', 's(k): signal')
title('3点MA'); % タイトルを記入
t = 0:0.02:5; %データの作成
s = 2*sin(2*pi*1*t);
x = s + randn(1, length(t));
y = x*0; %これより5点MA
for k=3: length(x)-2
y(k) = (x(k-2)+x(k-1)+x(k)+x(k+1)+x(k+2))/5;
end
y(1) = (x(1) + x(2) + x(3))/3;
y(2) = (x(1) + x(2) + x(3) + x(4))/4;
y(end-1) = (x(end-3) + x(end-2) + x(end-1) + x(end))/4;
y(end) = (x(end-2) + x(end-1) + x(end))/3;
figure (2) %グラフの描画
plot(t, x, 'b', t, y, 'r', t, s, 'k');
legend('x(k): before MA', 'y(k): after MA', 's(k): signal')
title('5点MA'); % タイトルを記入