You are on page 1of 5

Johannah Mae D.

Abestano 1) Matlab Script File to Produce Fourier Series of Square Wave


clear; clf; m = 4; t = -m*pi:pi/1000:m*pi; A = 1; N = 7; step = 0.2; wo = 1; c0 = A; % clear all variables % clear all figures

ECE 120 Assignment

% declare time values % summation limit (use N odd) % fundamental frequency (rad/s) % dc bias

T0 = 1 * 2 * pi; K =1 * 2; figure(1) % put first two plots on figure 1

% Compute yce, the Fourier Series in complex exponential form yce = c0*ones(size(t)); % initialize yce to c0 for n = -N:2:N, % loop over series index n % cn = 2/(j*n*wo); % Fourier Series Coefficient cn = (A/T0) * sin(pi * (1/T0) * (T0 / K) * n) / (pi * (1/T0) * n); yce = yce + cn*exp(1i*n*wo*t); end % Fourier Series computation

subplot(1,1,1) % plot([-3 -2 -2 -1 -1 0 0 1 1 2 2 3],... % plot original y(t) % [-1 -1 1 1 -1 -1 1 1 -1 -1 1 1], ':'); hold; plot(t,yce - 0.5); set(gca,'XTick',-m*pi:pi/2:pi*m); %set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'}); grid; xlabel('t (seconds)') ylabel('y(t)'); ttle = ['ECE 120: Truncated Exponential Fourier Series with N = ',... num2str(N)]; title(ttle); hold % Draw the amplitude spectrum from exponential Fourier Series figure(2) % put next plots on figure 2 subplot(2,1,1) tau = T0 / K; asd = stem(0,A * tau / T0); set(get(asd,'BaseLine'),'LineStyle',':') set(asd,'MarkerFaceColor','green')

% plot c0 at nwo = 0

hold for n = -N:step:N, % loop over series index n cn = (A/T0) * sin(pi * (1/T0) * (T0 / K) * n) / (pi * (1/T0) * n); %cn = 2/(j*n*wo); % Fourier Series Coefficient r = stem(n*wo,abs(cn)); % plot |cn| vs nwo set(get(r,'BaseLine'),'LineStyle',':') set(r,'MarkerFaceColor','green') end set(gca,'XTick',-N:1:N); xlabel('w (rad/s)'); ylabel('|cn|'); ttle = ['ECE 120: Amplitude Spectrum']; title(ttle); hold; % Draw the phase spectrum from exponential Fourier Series subplot(2,1,2) h = stem(0,angle(c0)*180/pi); % plot angle of c0 at nwo = 0 set(h,'MarkerFaceColor','green'); hold for n = -N:step:N, % loop over odd series index n cn = (A/T0) * sin(pi * (1/T0) * (T0 / K) * n) / (pi * (1/T0) * n); yaxis = angle(cn)*180/pi; if n > 0, yaxis = -yaxis; end; %cn = 2/(j*n*wo); % Fourier Series Coefficient h = stem(n*wo,yaxis); % plot |cn| vs nwo set(get(h,'BaseLine'),'LineStyle',':'); set(h,'MarkerFaceColor','green'); end set(gca,'XTick',-N:1:N); xlabel('w (rad/s)') ylabel('angle(cn) (degrees)') ttle = ['ECE 120: Phase Spectrum with N = ',num2str(N)]; title(ttle); hold

Output Figure 1

Figure 2

2) Fourier Series Coefficients of Cosine Wave


clear; clf; N = 3; wo = 1; c0 = 0; T0 = 2 * pi; f0 = 0.1592; for n = -N:1:N, cn = (2*pi*f0*n*(1/exp(4*pi^2*f0*n*1i) - 1)*1i)/(T0*(2*pi*f0*n 1)*(2*pi*f0*n + 1)); cn = real(cn); display(strcat('N = ', num2str(n), ': cn = ', num2str(cn))); end % clear all variables % clear all figures % summation limit (use N odd) % fundamental frequency (rad/s) % dc bias

Output N N N N N N N =-3: cn =0.00031837 =-2: cn =0.00037729 =-1: cn =0.50007 =0: cn =0 =1: cn =0.50007 =2: cn =0.00037729 =3: cn =0.00031837

You might also like