求周期三角信号的傅里叶系数,并绘制频谱图,只要求写出matlab代码就行
时间: 2024-06-04 22:10:24 浏览: 113
好的,以下是Matlab代码:
syms t n
T = 2*pi;
f = 1/T;
w0 = 2*pi/T;
x = (t<=T/2) - (t>T/2);
a0 = simplify(1/T * int(x,t,0,T));
an = simplify(2/T * int(x*cos(n*w0*t),t,0,T));
bn = simplify(2/T * int(x*sin(n*w0*t),t,0,T));
disp(['a0 = ', char(a0)])
disp(['an = ', char(an)])
disp(['bn = ', char(bn)])
N = 50; % Fourier级数展开项数
A = double(subs(a0));
for i=1:N
A = A + double(subs(an*cos(i*w0*t) + bn*sin(i*w0*t), i));
end
w = [-N*w0:w0:N*w0];
X = zeros(size(w));
for k = -N:N
X(k+N+1) = sum(double(subs(x*exp(-1j*k*w0*t),t,0:T/1000:T)));
end
figure
subplot(2,1,1)
plot(w/(2*pi),abs(X))
title('Magnitude Spectrum')
xlabel('Frequency (Hz)')
ylabel('|X(f)|')
subplot(2,1,2)
plot(w/(2*pi),angle(X))
title('Phase Spectrum')
xlabel('Frequency (Hz)')
ylabel('∠X(f)')
阅读全文