LABORATORY - FREQUENCY ANALYSIS OF DISCRETE-TIME SIGNALS INTRODUCTION The objective of this lab is to explore many issues involved in sampling and reconstructing signals, including analysis of the frequency content of sinusoidal signals using the DFT. When the sequence x[n] is of finite duration, the frequency contents that are contained in the sequence can be determined using the Discrete Fourier Transform (DFT). The DFT is an approximation of the Discrete Time Fourier Transform (DTFT), which is computed over an infinite duration. Computation of the DFT using only N data points may exhibit the edge effect when the windowed periodic signal is nothing like the original signal as shown in Fig. 1. (a) Windowed data (b) Fig. 1 Edge effect of the windowed data. (a) original sinusoidal sequence. (b) windowed periodic signal. Extracting a finite length of data can be thought of as multiplying the sequence with a rectangular box of length N which has a sinc-like spectrum as shown in Fig. 2. The sinclike spectrum of the rectangular window makes the resulting spectrum of the windowed sinusoid in Fig. 1 appear like a sinc-function, instead of an impulse as expected. (a) (b) Fig. 2 A rectangular window (a) Time domain (b) Frequency domain SW-1
Fig. 3 Spectrum of windowed sinusoid EXERCISE 1: EFFECT OF THE NUMBER OF POINTS IN THE FFT In this exercise, we will demonstrate the effect of data window and changing the value of the number of points in the FFT, N, on the FFT of sinusoidal signals. 1. Let us start by synthesising a cosine with 5 samples at 1 samples per period. t=linspace(,2,5); T=t(2)-t(1); x=cos(2*pi*t/(1*t)); % vector of time variable % sampling period % the cosine with 5 samples at 1 samples/period The plot of the sequence x[n] from n = to 49 is shown in Fig. 4 stem([:49],x); % plot of the sequence x[n] 1.8.6.4.2 x[n] -.2 -.4 -.6 -.8-1 5 1 15 2 25 3 35 4 45 5 Sample (n) Fig. 4 A cosine with 5 samples at 1 samples per period SW-2
Q1. What is the sampling period of x[n]? Q2. What is the frequency of the cosine x[n] in Hz? Q3. What kind of spectrum do you expect from this signal? 2. The frequency contents of the sequence will be determined using the MATLAB command FFT. The syntax for computing the FFT of a signal is FFT(x,N) where x is the discrete signal x[n] you wish to transform and N is the number of points in the FFT. If N is omitted, the FFT will generates the N-point FFT where N is the length of x. Define 3 different values of N, i.e. N=64, 128, and 256. Compute a set of N- point FFT X(k) and plot the results from k=-n/2. % Define 3 different values of N N1 = 64; N2 = 128; N3 = 256; % Compute the magnitudes of FFT of x[n] for each of 3 values of N X1 = abs(fft(x,n1)); X2 = abs(fft(x,n2)); X3 = abs(fft(x,n3)); % Frequency scaling to convert the discrete frequencies k to the continuous frequency in Hz fs=1/t; % sampling frequency in Hz F1 = [ : N1/2]/N1*fs; F2 = [ : N2/2]/N2*fs; F3 = [ : N3/2]/N3*fs; % plot the transforms figure; subplot(3,1,1) stem(f1,x1(1:n1/2+1),'filled'),title('n = 64'),axis([ 12.3 3]) subplot(3,1,2) stem(f2,x2(1:n2/2+1),'filled'),title('n = 128'),axis([ 12.3 3]) subplot(3,1,3) stem(f3,x3(1:n3/2+1),'filled'),title('n = 256'),axis([ 12.3 3]) xlabel('frequency (Hz)') SW-3
3 N = 64 2 1 2 4 6 8 1 12 3 N = 128 2 1 2 4 6 8 1 12 3 N = 256 2 1 2 4 6 8 1 12 Frequency (Hz) Fig. 5 FFT of a cosine for N=6, 128 and 256 Fig. 5 shows that the transforms all have a sinc-like appearance, which is a result of data window, differing only in the number of samples used to approximate the sinc function. Q4. Record the frequencies at the peaks of all three spectra in Fig. 5. Compare these peak frequencies with the frequency of the cosine x[n] and comment on the results. EXERCISE 2: EFFECT OF DATA LENGTH In this part, we will consider impact of window data length on resolution of FFT as the FFT length kept fixed. We choose to fix N=124 points and vary the number of repetitions of the repetitions of the fundamental period. % Data generation t=linspace(,2,5); T=t(2)-t(1); x1=cos(2*pi*t/(1*t)); % 5 periods x2=repmat(x1,1,2); % 1 periods x3=repmat(x1,1,5); % 25 periods N = 124; % the number of FFT % Compute the magnitudes of FFT of x[n] for each of 3 data lengths X1 = abs(fft(x1,n)); X2 = abs(fft(x2,n)); X3 = abs(fft(x3,n)); SW-4
% Frequency scaling fs=1/t; % sampling frequency in Hz F = [ : N/2]/N*fs; % plot the transforms figure; subplot(3,1,1) plot(f,x1(1:n/2+1)),title('5 periods') subplot(3,1,2) plot(f,x2(1:n/2+1)),title('1 periods') subplot(3,1,3) plot(f,x3(1:n/2+1)),title('25 periods') xlabel('frequency (Hz)') 4 5 periods 2 2 4 6 8 1 12 14 5 1 periods 2 2 4 6 8 1 12 14 25 periods 1 2 4 6 8 1 12 14 Frequency (Hz) Fig. 6 FFT of a cosine of 5, 1 and 25 periods Q5. As x[n] is extended to a large number of periods, how does this affect the obtained spectrum? Find the peak frequencies of spectra obtained from Fig. 6 and compare them with the ones you get from Fig. 5. EXERCISE 3: RECOVERING THE TIME-DOMAIN FUNCTION USING IFFT Given the FFT of a signal, you can recover the time-domain function of the signal by using IFFT. Here is how to do it. 1. Generate the cosine function as in EXERCISE 1: t=linspace(,2,5); % vector of time variable T=t(2)-t(1); % sampling period x=cos(2*pi*t/(1*t)); SW-5
2. Create the DFT of the signal and plot its magnitude and phase. Xk=fft(x); % Discrete Fourier transform of x figure subplot(2,1,1),stem(abs(xk),'.'), title('magnitude of FFT'); subplot(2,1,2),stem(angle(xk),'.'),title('phase of FFT'); 3. Recover the time-domain function by using the inverse DFT. xr = real(ifft(xk)); figure subplot(2,1,1), plot(t,x), title('cosine before FFT'); subplot(2,1,2), plot(t,xr),title('cosine after FFT and IFFT'); SW-6