Frequency Spectrum of Even Square Wave
M-file:
%% Frequency spectrum of Trigonometric Fourier Series for even square wave
figure(); % open a figure to plot frequency spectrum
n = 1:10; % vector of values for n noting omega = n x omega_0
an = 2*(sin(n*pi/2)./(n*pi)); % coefficients of cosines
bn = 0; % coefficients of sines
A = sqrt(an.^2 + bn.^2); % amplitudes
phi = -atan2(bn,an)*180/pi; % phases
%% plot amplitude spectrum
subplot(2,1,1); stem([0, n*pi], [1/2, A], 'filled'); grid;
xlabel('\omega (rad/sec)'); ylabel('A_n'); title('Amplitude Spectrum');
%% plot phase spectrum
subplot(2,1,2); stem([0, n*pi], [0, phi], 'filled'); grid;
xlabel('\omega (rad/sec)'); ylabel('\phi_n'); title('Phase Spectrum');
Plot generated: