University of Central Oklahoma: ENGR 3324L Lab 8: Sampling and the Discrete Fourier Transform (DFT) Lab objective: 1234- Learn the sampling theorem and the reconstruction of the signal from the samples. Learn the DFT of a sampled signal and some filtering application of the DFT Learn about aliasing and how to minimize error due to aliasing. The relationship between the length of the sampling duration in the time domain and the spectral resolution (frequency sampling interval). Prelab: A continuous signal x(t) defined as x(t) = cos(2π2t - π/4) + 2cos(2π4t) + 3 0 < t < 1 second Answer the following: a) What is the Nyquist sampling rate? b) What is the fundamental frequency ωo? c) According to the sampling theorem if the above signal was sampled at a rate of 6 samples/sec, then the constructed signal from the samples will be xs(t) = cos(2π2t- π/4) + 2cos(2πfat) + 3 What is the apparent frequency fa? d) What is the fundamental frequency of the signal x1(t) shown below? x1(t) = cos(2π2.5t - π/4) + 2cos(2π4t) + 3 e) If the signal x1(t) was sampled at a rate of 10 samples/sec for 1 second, what is the spectrum resolution (frequency sampling interval) of the signal spectrum? Will the discrete spectrum X(rωo) of the sampled signal xn show the 2.5Hz? If not then explain why and propose a solution for this problem? Introduction In most practical applications signals exist in the continuous form. However, with digital technology advancement most signal processing systems are digital. So, in order for the digital systems to process continuous signals we need to convert the continuous signals into discrete signals. In order to recover the continuous signal from the samples, the number of samples per seconds must be equal or greater than twice the bandwidth of the continuous signal (BW includes the highest frequency present in the continuous signal). If the sampling rate is below the Nyquist rate (2B) then the recovered continuous signal from the samples will not be the same as the original signal because aliasing will make high frequency components to appear as low frequency components. Many signal processors operate on the frequency domain of the signal such as compression. Discrete Fourier Transform (DFT), which is the same as the Fast Fourier Transform (FFT), is a technique that allows us to determine the frequency components of a continuous signal from its samples. For the DFT to represent the fundamental frequency and all harmonic frequencies of a continuous signal then the spectrum resolution (frequency sampling interval) must equal the fundamental frequency. For example if the spectrum resolution is 1 Hz then we will be missing frequency 1.5 Hz in the continuous signal if it was present. To be able to capture the 1.5Hz then the fundamental frequency should be 0.5 Hz. Fundamental frequency could be considered as the spectrum resolution of the signal spectrum. In this lab you will investigate the effect of sampling rate and the spectrum resolution on the ability to reconstruct the continuous signal from its samples. In addition you will build a digital filter to eliminate specific frequency component from a continuous signal. Lab Procedures: Task 1: Sampling and Desampling (Interpolation using the sinc function) In this task you will sample the signal x(t) at a rate higher than the Nyquist rate and then reconstruct the continuous signal x(t) from its samples xn. The continuous signal x(t) is shown below. x(t) = cos(2πf1t - π/4) + 2cos(2πf2t) + 3 f1 = 2 , f2 = 4, 0 < t< 1 second 1- Computer is a digital machine so all signals processed in the computer are discrete signals. However a discrete signal of a large number of samples (sampling rate>50*Bandwidth) can be considered as a true representation of the continuous signal x(t). You can verify this by plotting the sampled signal against t. >> To=1; >> t=0:0.01: To; %100 samples per second >>x=cos(2*pi*2*t-pi/4)+2*cos(2*pi*4*t)+3; >>plot(t, x); 2- What is the Bandwidth, B, of the signal x(t)? Sample the signal using Nyquist rate to obtain xn. >> To=1; >> fs= ? ; % replace the question mark by the Nyquist rate >> ts=0:1/fs:To; >> xn=cos(2*pi*2*ts-pi/4)+2*cos(2*pi*4*ts)+3; Plot the sampled signal, xn, against the sampling time using Matlab stem function. >> stem(ts, xn); Use Matlab plot function to plot the sampled signal, xn, against the sampling time. Does the plot look like the original signal? Explain your answer. >>plot(ts, xn); 3- According to the sampling theorem we should be able to reconstruct the original signal x(t) from the sampled signal, xn, if the sampling rate equal or higher than the Nyquist rate. N 1 x s (t ) x(nT ) sinc (t nT ) = x(t) T n 0 Where T is the sampling interval and N is the number of samples. The Matlab M-file sincInterpolation(xn, fs)shown at the end of the lab will construct the continuous signal, x(t) from the sampled signal, xn. fs is the sampling rate. >> [xs, sigMatr] = sincInterpolation(xn, fs); Are the plots of x(t) of part 1 and xs(t) of part 3 the same? How many samples in the estimated signal xs(t)? From the code can you explain what the variable sigMatr is? If not then try to plot the fifth row >>plot(sigMatr(5,:));. Are you amazed of the accuracy of estimating all the points of xs(t) from the small samples in xn? If you are not amazed then you are missing big concepts!!! Task 2: Discrete Fourier Transform What is the fundamental frequency of the signal x(t) shown below? x(t) = cos(2π2t- π/4) + 2cos(2π4t) + 3 1- Sample the signal x(t) at a rate of fs = 10 samples/sec which is above the Nyquist rate to obtain xn. >> To=1; >> fs=10; >> ts=0:1/fs:( To -1/fs); >> xn=cos(2*pi*2*ts-pi/4)+2*cos(2*pi*4*ts)+3; 2- Use the fft and the fftshift Matlab functions to find the DFT coefficients of the discrete signal xn. >>Fx=fftshift((fft(xn))); Plot the magnitude and the phase of the discrete spectrum X(rωo). >>stem([-fs/2: 1/To: fs/2-1/To],abs(Fx)); >>figure >>stem([-fs/2: 1/To: fs/2-1/To],angle(Fx).*(abs(Fx)>0.5)); What is the spectrum resolution ωo (frequency sampling interval) of the discrete spectrum X(rωo)? Can you verify the frequency components and their corresponding phases that are present in the signal x(t) from X(rωo)? Does the spectrum magnitude and phase agree with theory? Carry on the calculation to find the DC spectrum X(0) from the samples xn. 3- Recover the samples xn from its discrete spectrum X(rωo) using the Matlab built-in function ifft. >>xr=ifft(ifftshift(Fx)); Do the recovered samples xr agree with the original samples xn? Carry on the calculation to find the first sample x0 from the spectrum X(rωo). Task 3: Spectrum resolution and the time duration of the sampled signal. Change the frequency of the first cosine term to 2.5 Hz instead of 2 Hz. x(t) = cos(2π2.5t- π/4) + 2cos(2π4t) + 3 What is the fundamental frequency fo of the above signal? 1- Repeat steps 1 to 3 of task 2. Make sure to change the frequency from 2Hz to 2.5Hz in your code. 2- Why does the spectrum X(rωo) of the sampled signal xn show frequency components that are not present in the continuous signal x(t), and does not show the 2.5Hz frequency which is present in the x(t)? 3- If We sample the signal x(t) for two seconds, what is the spectrum resolution (frequency sampling interval) of the spectrum of the sampled signal, xn? 4- Repeat steps 1 to 3 of task 3. Make sure to change the frequency from 2Hz to 2.5Hz and To from 1 second to 2 seconds in your code. Does the spectrum of the 2 second sampled signal, xn, show the 2.5Hz component? Explain your answer. function [xs, sigMatr]=sincInterpolation1(x, fs) %x = samples of the continuous signal %fs= the sampling rate (frequency) T = 1/fs; %sampling interval [xn, No]=size(x); %No is the number of samples To = T*(No-1); %To Time width of the signal Nc = 50*No; %Nc is the number of samples of the constructed signals t=0:1/Nc:To; %samples of the time axis at which the signal will be estimated sigMatr=zeros(No, Nc+1); for n=0:No-1 Argu = (pi/T)*(t-n*T); for j=1:Nc if (Argu(j)~=0) sigMatr(n+1, j:j) = x(n+1)*sin(Argu(j))./Argu(j); else sigMatr(n+1, j:j) = x(n+1); end end end xs = sum(sigMatr); plot(t, xs) axis([0 To min(xs)-0.1 max(xs)+0.1]); Work Sheet Task 1: Sampling and Desampling (Interpolation using the sinc function) 1- plot the sampled signal against t. 2- What is the Bandwidth, B, of the signal x(t)? Plot the sampled signal, xn, against the sampling time using Matlab stem function. Plot the sampled signal, xn, against the sampling time using Matlab plot function. Does the plot look like the original signal? Explain your answer. 3- Show the plot of xs(t) produced by the sincInterpolation(xn, fs) Are the plots of x(t) of part 1 and xs(t) of part 3 the same? How many samples in the estimated signal xs(t)? From the code can you explain what the variable sigMatr is? Task 2: Discrete Fourier Transform What is the fundamental frequency of the signal x(t) shown below? 2- Plot the magnitude and the phase of the discrete spectrum X(rωo). What is the spectrum resolution ωo Can you verify the frequency components and their corresponding phases that are present in the signal x(t) from X(rωo)? Does the spectrum magnitude and phase agree with theory? Carry on the calculation to find the DC spectrum X(0) from the samples xn. 3- Do the recovered samples xr agree with the original samples xn? Carry on the calculation to find the first sample x0 from the spectrum X(rωo). Task 3: Spectrum resolution and the time duration of the sampled signal. What is the fundamental frequency fo of the above signal? 1a- Plot the magnitude and the phase of the discrete spectrum X(rωo). What is the spectrum resolution ωo Can you verify the frequency components and their corresponding phases that are present in the signal x(t) from X(rωo)? Does the spectrum magnitude and phase agree with theory? b- Do the recovered samples xr agree with the original samples xn? 2- Why does the spectrum X(rωo) of the sampled signal xn show frequency components that are not present in the continuous signal x(t), and does not show the 2.5Hz frequency which is present in the x(t)? 3- If We sample the signal x(t) for two seconds, what is the spectrum resolution (frequency sampling interval) of the spectrum of the sampled signal, xn? 4a- Plot the magnitude and the phase of the discrete spectrum X(rωo). What is the spectrum resolution ωo Can you verify the frequency components and their corresponding phases that are present in the signal x(t) from X(rωo)? Does the spectrum magnitude and phase agree with theory? b- Do the recovered samples xr agree with the original samples xn? Does the spectrum of the 2 second sampled signal, xn, show the 2.5Hz component? Explain your answer.