120 likes | 221 Views
Date: 20 Feb 07. Lab5 (Signal & System) Instructor: Anan Osothsilp. Due Date 09 March 07. 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
E N D
Date: 20 Feb 07 Lab5 (Signal & System) Instructor: Anan Osothsilp Due Date 09 March 07
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 Osothsilp Page 1 Lab5
Date: 20 Feb 07 Lab Instruction: Follow the video tutorial and do Lab exercises Anan Osothsilp Page 2 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 Format for variable = start : stop //code end Looping - Display value y = [0 1 2 3 4 5 6]; for i = 1: 7 disp(y(i)); end Iterating to find something -Find maximum y = [0 1 2 3 4 5 6]; temp = 0; for i = 1: 7 if (temp) < y(i) temp = y(i); end end Anan Osothsilp Page 3 Lab5
Date: 20 Feb 07 Linear combination using for loop x = [-1 0 1 2 3 4 5 6 7 8 ] y = 0; for n = 2:10 y = y+ x(n)^2; end • 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 figure(1); x = [1 2 3 4 5]; for i = 1:length(x) y = i*x; plot(x,y); hold on; pause(2); end Anan Osothsilp Page 4 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 Osothsilp Page 5 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([-2 3 -0.3 1.3]); 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 Osothsilp Page 6 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([-2 3 -0.3 1.3]); %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 Osothsilp Page 7 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([-2 3 -0.3 1.3]); %this command set figure axis Anan Osothsilp Page 8 Lab5
Date: 20 Feb 07 Frequency Spectrum of Fourier series Matlab code x = [1 2 3 4 5]; 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 Osothsilp Page 9 Lab5
Date: 20 Feb 07 Generate phase spectrum of signal N = 10; Wo = pi; for n = -N:1:N, Dn = 2/(j*n*Wo); stem(n*Wo,angle(Dn)*180/pi) hold on; end Anan Osothsilp Page 10 Lab5
Date: 20 Feb 07 Exercise 2: Find Fourier series and plot its signal spectrum Anan Osothsilp Page 11 Lab5