close all;
m = input('输入傅里叶展开项数,可输入矩阵');
t = -4:1/100:4;
f = 0.5+0.5*sawtooth(pi*t,0.5);
figure;
plot(t,f) %绘制原函数
axis([-2 2 -0.5 1.5])
grid on;
title('原函数')
figure; grid;
ff = 0.5+0.5*square(pi*t,50)
plot(t,ff) %绘制方波
axis([-2 2 -0.5 1.5])
grid on;
title('方波信号')
l = length(m);
for k = 1:l
sum = 1/2;
for n = 1:m(k)
an = (4/(pi*pi*n*n))*sin(n*pi/2)*sin(n*pi/2)*cos(n*pi*t);
sum = sum+an;
end
figure;
plot(t,sum);
axis([-4 4 -0.5 1.5]);
grid on;
title(strcat('第',m,'次谐波叠加'));
end