Lab5 (Signal & System) Instructor: Anan Osothsilp Date: 20 Feb 07 Due Date 09 March 07
Anan OsothsilpPage 1 Lab5 Date: 20 Feb 07 Objective: Learn how to use for loop operation for signal manipulation Learn how to write Matlab code to generate waveform for Fourier series Learn how to write Matlab code to generate signal spectrum
Anan OsothsilpPage 2 Lab Instruction: Follow the video tutorial and do Lab exercises Lab5 Date: 20 Feb 07
Anan OsothsilpPage 3 Lab5 Date: 20 Feb 07 For loop programming in Matlab Usage: - perform repeated task such as printing and calculation - iterate or loop through particular signal sample Looping - Display value y = [ ]; for i = 1: 7 disp(y(i)); end Format for variable = start : stop //code end Iterating to find something -Find maximum y = [ ]; temp = 0; for i = 1: 7 if (temp) < y(i) temp = y(i); end
Anan OsothsilpPage 4 Lab5 Date: 20 Feb 07 Linear combination using for loop Fourier series using for loop with plot on the same figure - If “ hold on;” command is called, then - subsequence call for plot command will create plot in the same figure x = [ ] y = 0; for n = 2:10 y = y+ x(n)^2; end figure(1); x = [ ]; for i = 1:length(x) y = i*x; plot(x,y); hold on; pause(2); end
Anan OsothsilpPage 5 Lab5 Date: 20 Feb 07 Linear combination using for loop N = 10 Wo = 2*pi; for n = -N:1:N, Dn = 2/(j*n*Wo); % Fourier Series Coefficient y = y + real(Dn*exp(j*n*Wo*t)); end % Fourier Series computation end
Anan OsothsilpPage 6 Lab5 Date: 20 Feb 07 Fourier series of square wave Without for loop’s code t4=[-1.3:.001:2.3]; x4=round(mod(t4,1)); axis([ ]); a0=0.5*ones(size(t4)); a1=-2/pi*sin(2*pi*t4); a3=-2/3/pi*sin(6*pi*t4); a5=-2/5/pi*sin(10*pi*t4); plot(t4,a0,'g',t4,a0+a1,'r',t4,a0+a1+a3,'m',t4,a0+a1+a3+a5,'k',t4,x4,'b','Linewidth',2) xlabel('Time (sec)') ylabel('Signal amplitude (units)') title('Periodic square wave signal') legend('one term','two terms','three terms','four terms')
Anan OsothsilpPage 7 Lab5 Date: 20 Feb 07 Fourier series of square wave With for loop’s code t=[-1.3:.001:2.3]; x=round(mod(t,1)); %this command generate square wave axis([ ]); %this command set figure axis hold on; plot(t,x,'b','LineWidth',2); %blue color with linewidth =2 a0=0.5*ones(size(t4)); %DC component plot(t,a0,'k','LineWidth',2); %black color with linewidth =2 hold on; sum = a0; color = ['r','g','m']; i =1; for n = [1 3 5 ]; %alternative for n = 1:2:5 sum =sum+ (-2/(n*pi))*(sin(2*pi*n*t)); plot(t,sum,'LineWidth',2); %color according to color(i) hold on; pause(2); %delay to show our graph i = i+1; 'inc color indec'; end xlabel('Time (sec)') ylabel('Signal amplitude (units)') title('Periodic square wave signal') legend('x(t) with bias','n =1','n=2','n=3','n=4') grid on;
Anan OsothsilpPage 8 Lab5 Date: 20 Feb 07 Excerise1 -Using for loop technique, write Matlab code Fourier series of N = 100; -Plot Fourier summation For the following x(t) t=[-1.3:.001:2.3]; x=round(mod(t,1)); %this command generate square wave axis([ ]); %this command set figure axis
Anan OsothsilpPage 9 Lab5 Date: 20 Feb 07 Frequency Spectrum of Fourier series Matlab code x = [ ]; y = 2*x; stem(x,y); N = 10; Wo = pi; for n = -N:1:N, Dn = 2/(j*n*Wo); stem(n*Wo,abs(Dn)) hold on; pause(1); end Generate magnitude spectrum of signal
Anan OsothsilpPage 10 Lab5 Date: 20 Feb 07 N = 10; Wo = pi; for n = -N:1:N, Dn = 2/(j*n*Wo); stem(n*Wo,angle(Dn)*180/pi) hold on; end Generate phase spectrum of signal
Anan OsothsilpPage 11 Lab5 Date: 20 Feb 07 Exercise 2: Find Fourier series and plot its signal spectrum