From kozick@charcoal.eg.bucknell.edu Wed Feb 12 14:22 EST 1997 Date: Wed, 12 Feb 1997 14:22:45 -0500 From: Kozick Rich To: ligong@bucknell.edu, suqi@bucknell.edu, yuesun@bucknell.edu, fhaosng@bucknell.edu, piatt@bucknell.edu, tassone@bucknell.edu, grace@bucknell.edu, nclark@bucknell.edu, cdavies@bucknell.edu, morelli@bucknell.edu, dmorelli@sunlink.net, daviesfive@aol.com, kozick@bucknell.edu Subject: hw3.m fix Hi, Something did not seem right in class today ... Remember that the zero crossings for the analog spectrum and the FFT should have lined-up for T=0.01, according to our "sinc" plots on the board? Well, they do, but my program hw3.m has an error. I have attached the corrected program below, and the updated copy is also on the Web page. The problem is that I was not actually sampling every T seconds. The error is rather subtle. If T = 0.01, I should only get 10 samples of the pulse, not 11. It is easy to get 11 samples if you sample at times 0, 0.01, ..., 0.1. But, in a real system with a pulse of length 0.1 seconds, we would not get "1" samples at BOTH t=0 and t=0.1. So, please run the program hw3.m again for various cases. Now, the above comments are true only when 0.1 is an integer multiple of T. For example, try T=0.015. Then the zero crossings do not line up, nor should they. Make sure you understand this point, and include an explanation with your homework that is due on Friday. Draw plots of aliasing with the sinc functions to understand. Thanks. Rich =================== hw3.m =========== % Program for EE 329, Homework 3 % Using the FFT to plot the frequency spectrum of a rectangular pulse. % Rich Kozick, February 1997 T = 0.01; % Sample spacing N = 100; % Number of samples Fs = 1/T; % Sampling rate f = (0:N-1)'/N*Fs; % FFT frequencies in hertz fa = linspace(0, Fs, 301); % Frequencies for plotting analog spectrum % Generate sampled signal x and the sampling times t = (T:T:0.1)'; x = ones(length(t), 1); N1 = length(x); if (N1 > N) fprintf('ERROR: Samples do not span the pulse duration\n'); return; end N2 = N - N1; t = [t; t(N1)+(T:T:(N2*T))']; x = [x; zeros(N2,1)]; % Plot sampled time signal figure(1) plot(t, x, '-', t, x, 'o') xlabel('t (sec)') ylabel('x(t)') s = sprintf('T = %f sec, N = %d pts, Fs = %f Hz', T, N, Fs); title(s) % Plot FFT absX = abs(fft(x)); absX = absX/max(absX)*0.1; % Normalize maximum amplitude to 0.1 figure(2) plot(f, absX, ':', f, absX, 'x') xlabel('f (Hz)') ylabel('|X(k)|') title('FFT MAGNITUDE') % Plot analog signal spectrum absXa = 0.1 * abs(sinc(fa/10)); figure(3) plot(fa, absXa); xlabel('f (Hz)') ylabel('|Xa(f)|') title('ANALOG SIGNAL MAGNITUDE SPECTRUM') % Plot analog spectrum and FFT on same axes figure(4) plot(f, absX, ':', fa, absXa, '-'); legend('FFT', 'ANALOG FT') xlabel('f (Hz)') ylabel('Magnitude') title('FFT AND ANALOG SIGNAL SPECTRUM')