L A B 3 : G E N E R A T I N G S I N U S O I D S NAME: DATE OF EXPERIMENT: DATE REPORT SUBMITTED: 1/7
1 THEORY DIGITAL SIGNAL PROCESSING LABORATORY 1.1 GENERATION OF DISCRETE TIME SINUSOIDAL SIGNALS IN MATLAB In this lab, discrete-time sinusoidal signal has been introduced. A discrete sinusoidal signal can be either sine or cosine time discrete signal and it can be expressed by: X(n) = A*sin(wn+ ), - < n < Where w = 2*pi*f; Therefore X(n) = A*sin(2πfn+ ), - < n < Similarly, discrete-time sinusoidal signal for a cosine function can be expressed as X(n) = A*cos(2*pi*fn+ ) - < n < Where n = integer variable called the sampler no A = Amplitude of the sinusoid w = frequency in radian per sample = Phase in radians As the mamaximum value of the functions SINE & COSINE is unity, the A acts as a scaling factor giving maximum and minimum values of ±A. 1.2 OBJECTIVES To learn the generation of discrete time sinusoidal signals To perform the elementary operations of shifting (advanced and delayed) To learn the techniques of doing scaling(time and amplitude) 2/7
To learn the application of some basic commands of MATLAB for simple digital signal processing problems 1.3 MATLAB SCRIPT n=0:40; f=0.1; phase=0; A=1.5; arg=2*pi*f*n-phase; x=a*cos(arg); clf; stem(n,x); axis([0 40-2 2]); grid; title('sinusoidal Sequence'); xlabel('time index n'); ylabel('amplitude'); axis; 1.4 ANSWERS TO THE QUESTIONS a) What is the period of the sequence? b) What command line controls the frequency in the program? c) What parameter controls the phase of this sequence? d) What parameter controls the amplitude of the sequence? e) What is the frequency of this sequence? 1.5 GENERATING AND SAMPLING CONTINOUS TIME SIGNALS Most signals in a DSP model are discrete-time signals. However, it is common that the discrete-time signal is a sampled version of a continuous-time signal. The following MATLAB m-file illustrates the generation of a continuous-time sinusoidal signal that is the sum of two separate sinusoids. S1 is a 240 Hz sinusoid and S2 is a 360 Hz sinusoid, which are summed to create Xa. Xa is plotted along with its components S1 and S2. Note that the time step of 100 s could easily be made smaller, say 1 s, to more closely approximate a continuous waveform. a) Create a script file on matlab and write to the shown codes and check it. 3/7
t = 0:100E-6:15E-3; S1 = sin(480*pi*t); S2 = 3*sin(720*pi*t); Xa = S1 + S2; plot(t,s1,'*'); hold on; plot(t,s2,'o'); hold on; plot(t,xa); hold off; xlabel('time'); ylabel('amplitude'); title('continuous Time Example'); grid; b) Now let s consider a sampled version of Xa. There are at least two ways to accomplish this in matlab. The first is to create an m-file that generates a discrete version of Xa given the sampling rate. And now Assume a sampling rate of 5 khz, which is much greater than the Nyquist rate of 720 Hz. Then x(n) = Xa(nT) where T = 1/5000 is the discrete version of the Xa. According to given datas that generate a script file and write matlab code where discrete version of Xa. 1.6 GENERATION OF WHITE NOISE WITH GAUSSIAN DISTRIBUTION Generate the white noise with the Gaussian distribution using the following code: x = 5*randn([1,4096]) ; % generate 4096 noise samples a) Make a plot of the white noise. Plot the first 50 samples (discrete-time signal style) using the Matlab function stem(). HINT : subplot(2,1,1); stem(x(1:50)); b) Use fft() to compute DFT coefficients, and plot and examine the magnitude spectrum of the signal x(n) assuming the sampling rate is 8000 Hz. To obtain frequency components: Ak = abs(fft(x))/length(x); To plot the spectrum: fs=8000; k=0:1:length(x)-1; f=k*fs/length(x); %frequency index % convert it to Hz subplot(2,1,2); plot(f,ak); Label and plot your graph. 4/7
What frequency range should you read? What is the frequency resolution? 1.7 GENERATION OF THE SUM OF DIGITAL SINUSOIDS Generate three sinusoids with the following amplitudes and phases x 1 ( t ) 5cos[2 (500) t ] ; x 2 ( t ) 5cos[2 (1200) t 0.25 ] ; x 3 ( t ) 5cos[2 (1800) t 0.5 ] a) Make a plot of all three signals over a range of t that will exhibit approximately 0.01 second. Each signal will be sampled using a sampling rate of 8000 Hz, a step size (sampling period) for time t is 1/8000=0.000125 second HINT : t=0:(1/8000):0.5; figure; subplot(3,1,1);plot(t,x1); axis([0 0.01 6 6]); Verify that the phases of all three signals are correct, and verify that each one has the correct maximum amplitude. Calculated x 1 ( 0) x 1max Measured x 1 ( 0) x 1max Calculated x 2 ( 0) x 2max Measured x 2 ( 0) x 2max Calculated x 3 ( 0) x 3max Measured x 3 ( 0) x 3max b) Create a program to generate the sum of sinusoids by adding three generated sinusoids x( t) x ( t) x ( t) x ( t). 1 2 3 Make a plot of x(t) over the same range of time as used in (1.7). figure; subplot(2,1,1);plot(t,x);axis([0 0.01-6 6]); What is the sampling rate? c) Use fft() to compute DFT coefficients, plot and examine the spectrum of the signal xn ( ) (frequency components). To obtain frequency components: Ak = abs(fft(x))/length(x); 5/7
To plot the spectrum: fs=8000; k=0:1:length(x)-1; %k is the frequency bin (frequency index) f=k*fs/length(x); % convert xx to Hz subplot(2,1,2);plot(f,ak); Label and plot your graph. What frequencies are shown in the signal spectral plot? What is the maximum frequency value that you can see? What is the frequency resolution based on the matlab program? INSTRUCTOR VERIFICATION 1.8 SCRIPT FILES Check the students script file to see if it is correct 1.9 FUNCTION FILES Check the students function file to see if it is correct. 2 LAB WRITEUP Please send me a write-up of your code in pdf format. You can write it as a word file, then save as pdf. Each lab grade consists of two sections. 50% of your grade is for coming to the lab. 50% is for your write-up. If you do not send the write-up within 2 weeks of the lab, you will not be allowed to submit a lab writeup. If your lab writeup is found to be substantially identical to another students using our special forensics team, you and the other student will receive a substantial reduction in your grade. 6/7
At the top of your code you should have your name, student number, lab number, date you performed the lab. In the lab writeup you should answer all questions in the body of the lab report. Please clearly label every question. You will not get credit for sections that are not clearly labeled. You must copy figures from Matlab and paste them into your word file, or save them as jpeg and import them into your word file. All figures must have captions inside which clearly describe what the figure is. Place print-outs of your code in the appendix, clearly mark what section and problem number the code was for. 7/7