EE-493 Digital Signal Processing (DSP)_2012

37
PRACTICAL WORK BOOK For Academic Session 2012 Digital Signal Processing (EE-493) For BE (EE ), PE (BO), PE (MD) & BE ( EL) Name: Roll Number: Class: Batch: Semester/Term: Department : Department of Electrical Engineering NED University of Engineering & Technology

Transcript of EE-493 Digital Signal Processing (DSP)_2012

Page 1: EE-493 Digital Signal Processing (DSP)_2012

PRACTICAL WORK BOOKFor Academic Session 2012

Digital Signal Processing (EE-493)For

BE (EE ), PE (BO), PE (MD) & BE ( EL)

Name:Roll Number:Class:Batch: Semester/Term:Department :

Department of Electrical EngineeringNED University of Engineering & Technology

Page 2: EE-493 Digital Signal Processing (DSP)_2012

CONTENTS

Lab. No.

Dated Title of Experiments Page

No Remarks

1 Effects of Sampling in Discrete Time

Signals. 0 1 - 0 3

2 Effects of Quantization in Discrete Time Continuous Valued Signals.

0 4 - 0 6

3 Discrete-Time Convolution. 0 7 - 0 8

4 Discrete - Time Correlation. 0 9 - 1 0

5 Studying Time Frequency Characteristics using Continuous Time Fourier Series

1 1 - 1 2

6 Studying Discrete Fourier Transform using an audio signal example.

1 3 - 1 4

7(a) Relationship between Laplace and CTFT. 1 5 - 1 6

7(b) Relationship between Z transform and DTFT.

1 7 - 1 8

8 Designing FIR and IIR Filters. 1 9 - 2 1

9 Generation of sine waves and plotting with CSS using C-6713 DSK.

2 2 - 2 4

10 Generation of sine waves using interrupts 2 5 - 2 6

11 Computation of DFT using FFT Algorithms in C -6713 DSK.

2 7 - 2 8

12 Designing Digital Filter using C-6713 DSK.

2 9 - 3 0

13 Appendices 3 1 - 3 5

Page 3: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab Session 01 NED University of Engineering and Technology Department of Electrical Engineering

- 1 -

LAB SESSION 01

Effects of Sampling in Discrete Time Signals OBJECTIVE: 1. Simulate and plot two CT signals of 10 Hz and 110 Hz for 0 < t < 0.2 secs. 2. Sample at Fs = 100 Hz and plot them in discrete form. 3. Observe and note the aliasing effects. 4. Explore and learn.

THEORY: This practical covers the topics of lectures 1-6, and are supported by sections 1.3 and 1.4 of your text book (DSP by Proakis, 4th Ed). PROCEDURE: 1. Make a folder at desktop and name it as your current directory within MATLAB. 2. Open M-file editor and type the following code:  clear all; close all; clc; F1 = 10; F2 = 110; Fs = 100; Ts = 1/Fs; t = [0 : 0.0005 : 0.2]; x1t = cos(2*pi*F1*t); x2t = cos(2*pi*F2*t); figure, plot(t,x1t,t,x2t, 'LineWidth',2); xlabel('cont time (sec)'); ylabel('Amp'); xlim([0 0.1]); grid on; legend('10Hz','110Hz'); title('Two CTCV sinusoids plotted'); 3. Save the file as P011.m in your current directory and ‘run’ it, either using F5 key or writing the file name at the command window.

Page 4: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab Session 01 NED University of Engineering and Technology Department of Electrical Engineering

- 2 -

4. Check for the correctness of the time periods of both sinusoids. 5. Now add the following bit of code at the bottom of your P011.m file and save. nTs = [0 : Ts : 0.2]; n = [1 : length(nTs)-1 ]; x1n = cos(2*pi*F1*nTs); x2n = cos(2*pi*F2*nTs); figure, subplot(2,1,1), stem(nTs,x1n,'LineWidth',2); grid on; title('10Hz sampled'); xlabel('discrete time (sec)'); ylabel('Amp'); xlim([0 0.1]); subplot(2,1,2) stem(nTs,x2n,'LineWidth',2); grid on; title('110Hz sampled') xlabel('discrete time (sec)'); ylabel('Amp'); xlim([0 0.1]); 6. Before hitting the ‘run’, just try to understand what the code is doing and try to link it with what we have studied in classes regarding concepts of frequency for DT signals. 7. Now ‘run’ the file and observe both plots. 8. To see what is really happening, type the following code at the bottom of your existing P011.m file and run again.

figure, plot(t,x1t,t,x2t); hold stem(nTs,x1n,'r','LineWidth',2); xlabel('time (sec)'); ylabel('Amp'); xlim([0 0.05]); legend('10Hz','110Hz'); 9. Observe the plots.

Page 5: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab Session 01 NED University of Engineering and Technology Department of Electrical Engineering

- 3 -

RESULT: Explain (write) in your own words the cause and effects of what you just saw. EXERCISE: This needs to be completed before your next practical and saved. Consider the following CT signal: x(t) = sin (2 pi F0 t). The sampled version will be: x(n) = sin (2 pi F0/Fs n), where n is a set of integers and sampling interval Ts=1/Fs. Plot the signal x(n) for n = 0 to 99 for Fs = 5 kHz and F1 = 0.5, 2, 3 and 4.5 kHz. Explain the similarities and differences among various plots.

Page 6: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab Session 02

NED University of Engineering and Technology Department of Electrical Engineering

- 4 -

LAB SESSION 02

Effects of Quantization in Discrete Time Continuous Valued

Signals

OBJECTIVE:

1. Simulate a DTCV sinusoid of 1/50 cycles/sample with length of the signal be 500. 2. Choose the no. of significant digits for round-off and apply to the signal generated above. 3. Compute the error signals and SQNR. 4.

Explore and observe.

THEORY:

In this session, we will generate DTCV sinusoids and quantize them. We shall measure the quantization noise signals and the quality of the quantization process. The error signal is given as: xe(n) = x(n) - xq(n). Where, xq(n) is the quantized signal.

The resolution of the quantizer is given as: max min

1

x x

L, where L is the no. of

quantization levels. The quality of the quantized signal is measured by the signal-to-quantization noise ratio

(SQNR) which is mathematically written as: SQNR = 10e

x

P

P10log Px and Pe are average

powers of the DTCV and quantized signal respectively, and may be written as: 1

2

0

1( )

N

xn

P x nN

and.1

0

2)()(

1 N

nqe nxnx

NP

This practical is supported by section 1.4.3 of your text book (4th Ed).

PROCEDURE:

1. Make a folder at desktop and name it as your current directory within MATLAB. 2. Open M-file editor and write the following code:

clear all; close all; clc;

fd1 = 1/50; n = [0 : 499 ];

q=input('No. of Digits after decimal points to be retained (0-9): ');

x1 = cos(2*pi*fd1*n); Px1 = sum(abs(x1).^2)/length(x1);

x1q = round(x1*10^q)/10^q;

Page 7: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab Session 02

NED University of Engineering and Technology Department of Electrical Engineering

- 5 -

x1e = x1 - x1q; Pe1 = sum(abs(x1e).^2)/length(x1e);

SQNR = 10*log10(Px1/Pe1); disp(['The Signal to Quantization Noise Ratio is: ' num2str(SQNR) '

dB.' ]); figure, subplot(2,1,1); plot(n,x1,n,x1q); xlabel('indices'); ylabel('Amp'); xlim([0 49]); ylim([-1.1 1.1]); legend('DTCV','DTDV');

subplot(2,1,2); plot(n,x1e); xlabel('indices'); ylabel('Error'); xlim([0 49]);

3. Save the file as P021.m in your current directory and run it. 4. Explore and take notes. 5. Now modify the above code as follows and save as another file P022.m.

clear all; close all; clc;

fd1 = 1/50; n = [0 : 499 ]; q = [0 : 10]; % No. of Digits after decimal points to be retained

for num = 1 : length(q)

x1 = cos(2*pi*fd1*n); Px1 = sum(abs(x1).^2)/length(x1); x1q = round(x1*10^q(num))/10^q(num);

x1e = x1 - x1q; Pe1 = sum(abs(x1e).^2)/length(x1e);

SQNR(num) = 10*log10(Px1/Pe1);

end

figure, plot(q,SQNR); xlabel('Significant Digits'); ylabel('SQNR (dB)');

Page 8: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab Session 02 NED University of Engineering and Technology Department of Electrical Engineering

- 6 -

ylabel('SQNR (dB)'); xlim([q(1) q(end)]);

6. Before hitting the ‘run’, just try to understand what the code is doing and try to link it with the previous code. 7. Now ‘run’ the file and observe the results.

 

RESULT:

Explain (write) in your own words the cause and effects of what you just saw. EXERCISE:

 Do Exercise 1.16 of Proakis (4th Ed) in MATLAB and bring it in your next lab.

Page 9: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 03 NED University of Engineering and Technology Department of Electrical Engineering

- 7 -

LAB SESSION 03

Discrete-Time Convolution

OBJECTIVE: 1. We have the impulse response of a system as ( ) {3,2,1, 2,1,0, 4,0,3}h n

↑= − − .

2. For ( ) {1, 2,3, 4,3, 2,1}x n↑

= − − , find y(n).

3. Modify the code such that ( ) {3, 2,1, 2,1,0, 4,0,3}h n↑

= − − - (origin is shifted), and check

the causality property mentioned above (Theory – point 3).

THEORY:

1. Convolution is given as: ( ) ( ) ( ) ( ) ( ) ( ) ( )k k

y n x n h n x k h n k h k x n k∞ ∞

=−∞ =−∞

= ∗ = − = −∑ ∑ . i.e.

one can compute the output y(n) to a certain input x(n) when impulse response h(n) of that system is known. Convolution holds commutative property.

2. The length of the resulting convolution sequence is N+M-1, where, N and M are the lengths of the two convolved signals respectively.

3. In Causal Systems, the output only depends on the past and/or present values of inputs and NOT on future values. This means that the impulse response h(n) of a causal system will always exist only for 0.n ≥

This practical is supported by section 2.3.3 of your text book (4th Ed).

PROCEDURE:

1. Make a folder at desktop and name it as your current directory within MATLAB. 2. Open M-file editor and write the following code:  clear all; close all; clc; h = [3 2 1 -2 1 0 -4 0 3]; % impulse response org_h = 1; % Sample number where origin exists nh = [0 : length(h)-1]- org_h + 1; x = [1 -2 3 -4 3 2 1]; % input sequence org_x = 1; % Sample number where origin exists nx = [0 : length(x)-1]- org_x + 1; y = conv(h,x);

Page 10: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 03 NED University of Engineering and Technology Department of Electrical Engineering

- 8 -

ny = [nh(1)+ nx(1) : nh(end)+nx(end)]; figure, subplot(3,1,1), stem(nh,h); xlabel('Time index n'); ylabel('Amplitude'); xlim([nh(1)-1 nh(end)+1]); title('Impulse Response h(n)'); grid; subplot(3,1,2), stem(nx,x); xlabel('Time index n'); ylabel('Amplitude'); xlim([nx(1)-1 nx(end)+1]); title('Input Signal x(n)'); grid; subplot(3,1,3) stem(ny,y); xlabel('Time index n'); ylabel('Amplitude'); xlim([ny(1)-1 ny(end)+1]); title('Output Obtained by Convolution'); grid;

3. Save the file as P031.m in your current directory and ‘run’ it. 4. Try to learn, explore the code and make notes. 5. Now modify the above code as mentioned in objectives above and check for causality.

RESULT: EXERCISE: 1. What will happen if we input ( ) {0,0,1,0,0}x n

↑= into the above system.

2. Can you prove commutative property of the convolution? 3. Modify the code to prove Associative and Distributive properties of the convolution

Page 11: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 04 NED University of Engineering and Technology Department of Electrical Engineering

- 9 -

LAB SESSION 04

Discrete-Time Correlation

OBJECTIVE: 1. Generate two sinusoids of length 10 and fd = 0.1 with variable phase. 2. Apply correlation and check for certain properties such as magnitude and location of maximum correlation with varying phases. 3. Also, to verify whether the lag zero auto-correlation gives the energy of the signal. 4. Compute correlation using convolution command.

THEORY:

1. Correlation is given as: ( ) ( ) ( ) ( ) ( ).xyn n

r l x n y n l x n l y n∞ ∞

=−∞ =−∞

= − = +∑ ∑ . Where ‘l’ is the

lag. This is called cross-correlation and it gives the magnitude and location of similarity between two signals. The correlation between y(n) and x(n) is not necessarily the same as the correlation between x(n) and y(n). It is given as:

( ) ( ) ( ) ( ) ( ).yxn n

r l y n x n l y n l x n∞ ∞

=−∞ =−∞

= − = +∑ ∑

2. Generally, ( ) ( )xy yxr l r l= − . These two are the same when x(n) and y(n) are the same

signals or when x(n) and y(n) are even symmetric signals.

3. The length of the resulting correlation sequence is N+M-1, where, N and M are the lengths of the two signals

4. Correlation may also be computed using convolution algorithm with a modification that we need to fold one of the signals before applying the convolution. Mathematically,

( ) ( ) ( )xyr n x n y n= ∗ − .

PROCEDURE: 1. Make a folder at desktop and name it as your current directory within MATLAB. 2. Open M-file editor and write the following code: clear all; close all; clc; n = [0:9]; ph1 = 0; ph2 = 0; x = sin(2*pi*0.1*n + ph1); org_x = 1; nx = [0 : length(x)-1]- org_x + 1;

Page 12: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 04 NED University of Engineering and Technology Department of Electrical Engineering

- 10 -

y = sin(2*pi*0.1*n + ph2);

 org_y = 1; ny = [0 : length(y)-1]- org_y + 1; rxy = xcorr(x,y); nr = [nx(1)-ny(end) : nx(end)-ny(1)]; [maxR indR] = max(rxy); disp(['The correlation at lag zero is: ' num2str(rxy(find(nr==0))) '.']); disp(['The maximum correlation is at lag ' num2str(nr(indR)) '.']); figure, subplot(3,1,1), stem(nx,x); xlabel('Time index n'); ylabel('Amplitude'); xlim([nx(1)-1 nx(end)+1]); title('Signal x(n)'); grid; subplot(3,1,2), stem(ny,y); xlabel('Time index n'); ylabel('Amplitude'); xlim([ny(1)-1 ny(end)+1]); title('Signal y(n)'); grid; subplot(3,1,3) stem(nr,rxy); xlabel('Time index n'); ylabel('Amplitude'); xlim([nr(1)-1 nr(end)+1]); title('Cross Correlation'); grid;

3. Save the file as P041.m in your current directory and ‘run’ it. 4. Learn the specific logical bits of the code and make notes. 5. Now modify the phase of the second signal to pi/2 (it will make it cosine) and observe the correlation at lag zero. Modify the phase again to ‘pi’ and observe. 6. Check for auto-correlation (ph1 = ph2) that the lag zero value gives the energy of the signal. 7. Observe that the commutative property does not hold.  

RESULT:

EXERCISE:  Modify the code, such that the correlation is obtained using convolution command.

Page 13: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 05 NED University of Engineering and Technology Department of Electrical Engineering

- 11 -

LAB SESSION 05 Studying Time Frequency Characteristics using Continuous

Time Fourier Series OBJECTIVE: 1. Generate a square wave in time domain with certain time period and pulse width. 2. Apply CTFS equation to compute spectral coefficients. 3. Observe the plot to verify the properties mentioned above. 4. Modify time-periods and pulse widths to assess the corresponding change in frequency domain.

THEORY: In this session, we will explore the relation between time domain properties of the signal (Time period and Pulse Width) and frequency domain properties (Spectral density and Lobe width) – using CTFS equations.

The Analysis equation for CTFS is given as: /2 2

/2

1 ( ) OTp j F t

K Tpc x t e dt

Tpπ−

−= ∫ ; where Fo is the

Fundamental Frequency and is the inverse of the Time period of the signal. 1. The spectral spacing of the spectrum is equal to the inverse of the time period of the

signal.

2. The Lobe width of the spectrum is equal to the inverse of the pulse width of the signal.

PROCEDURE: 1. Make a folder at desktop and name it as your current directory within MATLAB. 2. Open M-file editor and write the following code:  clear all; clc; close all; Ts = 0.001; %sampling period - so small to approximate cont time t = [-10:Ts:9.999 ]; % Time Vector - again approx. continuous Tp = 1; % Time Period of the signal tau = 0.5; % Pulse Width - Duty Cycle x = (1+square(t*2*pi/Tp,tau*100))/2; % Generating Square wave figure, plot(t,x); xlabel('secs');

Page 14: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 05 NED University of Engineering and Technology Department of Electrical Engineering

- 12 -

ylim([-1.2 1.2]); pause; % Press any key Fo = 1/Tp; % Fundamental Frequency % Extracting a portion of the signal equal to its period. I_period = find(round(t*1000)/1000 == -Tp/2) : find(round(t*1000)/1000 == Tp/2); xport = x( I_period ); figure, plot(t(I_period),xport); xlabel('secs') ylim([-1.2 1.2]) pause % Press any key % Computing CTFS Coefficients for k = 1:20

B = exp(-j*2*pi*(k-1)*Fo.*[-Tp/2:Ts:Tp/2]); C(k) = sum(xport.*B)/(length(xport)*Tp);

end kFo = Fo*[0:k-1]; % Frequency Scale in Hz. figure, stem(kFo,abs(C)); title('CTFT Coefficients'); xlabel('Hz');

 

RESULT:

Explain (write) in your own words the cause and effects of what you just saw. EXERCISE:

Now make the following modifications one by one and observe the effects.

a. Change ‘Tp’ to 2 secs and then to 0.5 secs. b. Change ‘tau’ to 0.2. c. Change ‘tau’ to 0.001 and see the spectra. d. Change ‘tau’ to 1 and observe the spectra.

Make notes about all observations and learn to play around with the code.

Note: Do not change the sampling time, as this is an artificially made continuous-time signal. Also, do not increase Tp than 10 secs and reduce below 0.05 secs.

Page 15: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 06 NED University of Engineering and Technology Department of Electrical Engineering

- 13 -

LAB SESSION 06 Studying Discrete Fourier Transform using an audio signal

OBJECTIVE: 1. Load an audio file ‘noisy.wav’ into Matlab. 2. There is a tone added to the speech in this file. The objective is to find the frequency of this tone. 3. Computing the DFT of this signal; 4. Generating frequency vector in Hz. 5. Displaying the DFT and observing the frequency of the added tone. THEORY: • DF analysis equation:

• DFT synthesis eq:

• Frequency Resolution is given by .

• Sample frequencies are given by – 1.

 

PROCEDURE: 1. Make a folder at desktop and name it as your current directory within MATLAB. 2. Copy the audio file ‘noisy.wav’ into your current directory. 3. Open M file editor and write the following code:  clear all; clc; close all; [y,Fs,bits] = wavread('noisy.wav'); Ts = 1/Fs; n = [0:length(y)-1]; t = n.*Ts; k = n; Df = Fs./length(y); F = k.*Df; Y = fft(y); magY = abs(Y); sound(y,Fs); figure, subplot(2,1,1); plot(F,magY); grid on; xlim([0 Fs/2]); xlabel('Frequency (Hz)'); ylabel('DFT Magnitude'); title('Discrete Fourier Transform'); subplot(2,1,2); plot(F,magY); grid on;

Page 16: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 06

NED University of Engineering and Technology Department of Electrical Engineering

- 14 -

xlim([0 2000]); xlabel('Frequency (Hz)'); ylabel('DFT Magnitude'); title('Discrete Fourier Transform');

4. Save the file as P081.m in your current directory and run it.

RESULT:

Explore and take notes.

EXERCISE

Try and remove the tone spikes from the spectra of the noisy signal. Take Inverse DFT (IFFT) of the modified spectra. Listen to this new time-domain signal and see if the tone noise is removed.

Note: This is actually a very crude of way of filtering which is covered later.

Page 17: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 07 (a) NED University of Engineering and Technology Department of Electrical Engineering

- 15 -

LAB SESSION 07(a)

Relationship between Laplace and CTFT

OBJECTIVE: 1. Generate pole zero constellation in s plane.

2. Plot corresponding Frequency (Bode magnitude) response. 3. Plot impulse response and determine that the system is FIR or IIR.

4. Modify location of poles in s plane to observe the corresponding change in frequency and impulse response.

THEORY: The Laplace Transform of a general continuous time signal x (t) is defined as;

X(S) = ∫∞

∞−

)(tx e-st dt.

Where the complex variable s=δ + j w, with δ and w the real and imaginary parts. CTFT is a subset of Laplace when δ =0. Since ‘δ’ information is not present in CTFT, therefore information about stability can only be obtained from Laplace. If pole lies on L.H.S of s-plane, system is stable. If pole lies on R.H.S of s-plane, system is unstable. If pole lies on y(jw)-axis, system is marginally stable or oscillatory. If system has FIR, it is stable. If system is IIR, it can be stable or unstable. PROCEDURE: 1. Make a folder at desktop and name it as your current directory within MATLAB. 2. Open M-file editor and write the following code:

clear all; close all; clc; Num = poly([(0-(i*(pi/2))),(0+(i*(pi/2)))]); Zeros=roots(Num) Den = poly([-1,-1]); poles=roots(Den) sys=tf(Num,Den) figure; subplot(3,1,1); pzmap(sys); xlim([-2 2]); ylim([-4 4]);

Page 18: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 07 (a) NED University of Engineering and Technology Department of Electrical Engineering

- 16 -

subplot(3,1,2); [mag phase w]=bode(sys); mag=squeeze(mag); plot(w,mag); subplot(3,1,3); impulse(sys); H=dfilt.df1(Num,Den); A=isfir(H)

3. Save the file as P091.m in your current directory and ‘run’ it.

RESULT: 1. Learn the specific logical bits of the code and make notes. 2. Observe the plots. 3. Now, explain (write) in your own words the cause and effects of what you just saw.

         

EXERCISE: Change the location of poles from L.H.S of s-plane to y axis first, and then to R.H.S of s-plane and observe the effects.

 

Page 19: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 07 (b) NED University of Engineering and Technology Department of Electrical Engineering

- 17 -

LAB SESSION 07(b)

Relationship between z transform and DTFT

OBJECTIVE: 1. Generate pole zero constellation in z plane.

2. Plot corresponding Frequency (Bode magnitude) response. 3. Plot impulse response and determine that the system is FIR or IIR. 4. Modify location of poles in z plane to observe the corresponding change in frequency and impulse response.  

THEORY: The z - Transform of a general discrete time signal x(n) is defined as;

X (z) = ∑∞=

−∞=

n

n

nx )( z-n

Where the complex variable z=r w∠ , with r the radius and w the angle. DTFT is a subset of z transform when r =1. Since ‘r’ information is not present in DTFT, therefore information about stability in discrete time can only be obtained from z transform. If pole lies inside the unit circle, system is stable. If pole lies outside the unit circle, system is unstable. If pole lies at the unit circle, system is marginally stable or oscillatory. If system has FIR, it is stable. If system is IIR, it can be stable or unstable. PROCEDURE: 1. Make a folder at desktop and name it as your current directory within MATLAB. 2. Open M-file editor and write the following code:

clear all; close all; clc; Num = poly([(0-(i*(pi/2))),(0+(i*(pi/2)))]); Den = poly([-1,-1]); Num1 = poly([j,-j]); Den1 = poly([exp(-1),exp(-1)]); sys1=tf(Num1,Den1,1) figure; subplot(3,1,1); pzmap(sys1); xlim([-2 2]); ylim([-4 4]); subplot(3,1,2); [mag phase w]=bode(sys1);

Page 20: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 07 (b) NED University of Engineering and Technology Department of Electrical Engineering

- 18 -

mag=squeeze(mag); plot(w,mag); xlim([0 100]) subplot(3,1,3); impulse(sys1); H=dfilt.df1(Num,Den); A=isfir(H) figure; pzmap(sys1) grid on;

3. Save the file as P010.m in your current directory and ‘run’ it.

RESULT: 1. Learn the specific logical bits of the code and make notes. 2. Observe the plots. 3. Now, explain (write) in your own words the cause and effects of what you just saw.

 

EXERCISE: Change the location of poles from inside the unit circle to outside and at the unit circle and observe the effects.

  

Page 21: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 08 NED University of Engineering and Technology Department of Electrical Engineering

- 19 -

LAB SESSION 08

Designing FIR and IIR Filters

OBJECTIVE:

1. Create a signal vector containing two frequencies as: i) 100 Hz. and ii) 150 Hz., with Fs = 1000 Hz.

2. Design two bandpass FIR filters with 64 coefficients and with passbands as i) 125 to 175 Hz. and ii) 75 to 125 Hz.

3. Use both filters on the created signal and observe their outputs. 4. Plot frequency responses and pole-zero constellations of both filters and note

observations. 5. Repeat steps 2 – 4 for IIR filters designed using Butterworth IIR filters.

THEORY:

1. FIR filters are Finite Impulse Response filters with no feedback, whereas IIR contains feedback.

2. Transfer function of FIR filter does not contain any non-trivial poles. Their frequency response is solely dependent on zero locations. IIR filters contain poles as well as zeros.

3. As there are no poles, FIR filters cannot become unstable; however, IIR filters can become unstable if any pole lies outside the unit circle in z-plane.

4. More number of coefficients is needed to design the desired filter in FIR than IIR.

PROCEDURE:

1. Write the following code for FIR filters: close all; clear all; clc; % Frequencies in Hz. F1 = 100; F2 = 150; % Sampling Frequency in samples/sec. Fs = 1000; t = [0 : 1/Fs : 1]; % Time Vector F = Fs*[0:length(t)-1]/length(t); % Frequency Vector x = exp(j*2*pi*F1*t)+2*exp(j*2*pi*F2*t); % Signal Vector bh = fir1( 64 , [125 175]/500); % filter coeffs. for bandpassing F1 bl = fir1( 64 , [75 125]/500); % filter coeffs. for bandpassing F2

Page 22: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 08 NED University of Engineering and Technology Department of Electrical Engineering

- 20 -

[hh,wh]=freqz(bh,1,length(t),'whole'); % Frequency response for filter 1 [hl,wl]=freqz(bl,1,length(t),'whole'); % Frequency response for filter 2 % Filter operation - see filtfilt in help to learn what it does yh = filtfilt(bh,1,x); yl = filtfilt(bl,1,x); % Plotting figure, subplot(5,1,1),plot(F,abs(fft(x)));xlim([0 Fs/2]); title('FFT of orgianl signal'); subplot(5,1,2),plot(F,abs(hh));xlim([0 Fs/2]); title('Frequency response of Filter One'); subplot(5,1,3),plot(F,abs(fft(yh)));xlim([0 Fs/2]); title('FFT of filtered signal from filter one'); subplot(5,1,4),plot(F,abs(hl));xlim([0 Fs/2]); title('Frequency response of Filter Two'); subplot(5,1,5),plot(F,abs(fft(yl)));xlim([0 Fs/2]); title('FFT of filtered signal from filter two'); xlabel('Hz.') % Pole Zero Constellations [bh,ah] = eqtflength(bh,1); [zh,ph,kh] = tf2zp(bh,ah); [bl,al] = eqtflength(bl,1); [zl,pl,kl] = tf2zp(bl,al); figure, subplot(1,2,1),zplane(bh,ah);xlim([-1.5 1.5]);ylim([-1.5 1.5]); title('Filter One'); subplot(1,2,2),zplane(bl,al);xlim([-1.5 1.5]);ylim([-1.5 1.5]); title('Filter Two');

 2. Note the observations from frequency response and pole-zero constellation plots. 3. Now, write the code in another program for IIR filters:

 close all; clear all; clc; % Frequencies in Hz. F1 = 100; F2 = 150; % Sampling Frequency in samples/sec. Fs = 1000; t = [0 : 1/Fs : 1]; % Time Vector F = Fs*[0:length(t)-1]/length(t); % Frequency Vector x = exp(j*2*pi*F1*t)+2*exp(j*2*pi*F2*t); % Signal Vector

Page 23: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 08 NED University of Engineering and Technology Department of Electrical Engineering

- 21 -

[bh,ah] = butter(6,[125 175]/500);% filter coeffs. for bandpassing F1 [bl,al] = butter(6,[75 125]/500);% filter coeffs. for bandpassing F2 [hh,wh]=freqz(bh,ah,length(t),'whole'); % Frequency response for filter 1 [hl,wl]=freqz(bl,al,length(t),'whole'); % Frequency response for filter 2 % Filter operation - see filtfilt in help to learn what it does yh = filtfilt(bh,ah,x); yl = filtfilt(bl,al,x); % Plotting figure, subplot(5,1,1),plot(F,abs(fft(x)));xlim([0 Fs/2]); title('FFT of orgianl signal'); subplot(5,1,2),plot(F,abs(hh));xlim([0 Fs/2]); title('Frequency response of Filter One'); subplot(5,1,3),plot(F,abs(fft(yh)));xlim([0 Fs/2]); title('FFT of filtered signal from filter one'); subplot(5,1,4),plot(F,abs(hl));xlim([0 Fs/2]); title('Frequency response of Filter Two'); subplot(5,1,5),plot(F,abs(fft(yl)));xlim([0 Fs/2]); title('FFT of filtered signal from filter two'); xlabel('Hz.') % Pole Zero Constellations [bh,ah] = eqtflength(bh,ah); [zh,ph,kh] = tf2zp(bh,ah); [bl,al] = eqtflength(bl,al); [zl,pl,kl] = tf2zp(bl,al); figure, subplot(1,2,1),zplane(bh,ah);xlim([-1.5 1.5]);ylim([-1.5 1.5]); title('Filter One'); subplot(1,2,2),zplane(bl,al);xlim([-1.5 1.5]);ylim([-1.5 1.5]); title('Filter Two');  

4. Note the observations from frequency response, pole-zero constellation. 5. Compare plots across FIR and IIR.

Page 24: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 09 NED University of Engineering and Technology Department of Electrical Engineering

- 22 -

LAB SESSION 09

Generation of sine waves using lookup table using C-6713 DSK

OBJECTIVE: To generate a sinusoid and plotting it with Code Composer Studio (sine8_buf). REQUIREMENTS:

C6713 DSK(DSP Starter Kit) and its support tools Oscilloscope Head phones/ Speaker

THEORY: This exercise generates a sinusoid with eight points. More important, it illustrates CCS capabilities for plotting in both time and frequency domains. The program used in this exercise creates a buffer to store the output data in memory. Buffer: Buffer is an array in which data to be written or to be read from the disk is placed. Putting the contents of a file in a buffer has certain advantages; we can perform various operations on the contents of buffer without having to access the file again. The size of the buffer is important for efficient operation. Depending on the operating system, buffers of certain sizes are handled more efficiently than others. In this program, an output buffer is created to capture a total of 256 sine data values. Interrupt: An interrupt can be issued internally or externally. An interrupt stops the current CPU process so that it can perform a required task initiated by the interrupt. The source of the interrupt can be an ADC, timer etc. On an interrupt; the conditions of the current process must be saved so that they can be restored after the interrupt task is performed. PROCEDURE:

1. Connect DSK C6713 board to the USB port of PC and give +5V supply to the board.

2. Perform the diagnostic and quick tests of DSK (refer to Appendix A). 3. Connect the DSK by selecting Debug→ Connect, if it has not already been

connected.

Page 25: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 09 NED University of Engineering and Technology Department of Electrical Engineering

- 23 -

4. Create New Project (refer to Appendix B).5.Add files to the project (refer to Appendix C).

6. Bulid the program (refer to Appendix D). 7. Select File→ Load Program in order to load sine_buf.out on the DSK. 8. Connect a speaker to the LINE OUT connector on the DSK 9. Select Debug→ Run. OBSERVATION: A tone of 1 kHz can be heard on speaker or can be viewed on an oscilloscope. EXERCISE: Plotting with Code Composer Studio (CCS):

To plot the current output data stored in the buffer using CCS, perform following steps: 1. Select View→ Graph→ Time/Frequency. Change the Graph Property Dialog so

that the options in Figure 2.1 are selected for a time-domain plot. The other options can be left as default. Press OK and observe the wave pattern in time-domain within CCS.

2. Now change CCS Graph Property Dialog for a frequency-domain plot according to Figure 2.2. Press OK and observe the FFT magnitude plot. The spike at 1000Hz represents the frequency of the sinusoid generated.

Display Type Single Time Graph Title Graphical Display Start Address out_buffer Acquisition Buffer Size 256 Index Increment 1 Display Data Size 64 DSP Data Type 16-bit signed integer Q-value 0 Sampling Rate (Hz) 8000 Plot Data From Left to Right Left-shifted Data Display Yes Autoscale On DC Value 0Axes Display On Time Display Unit S Status Bar Display On

Figure 2.1

Page 26: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 09 NED University of Engineering and Technology Department of Electrical Engineering

- 24 -

Display Type FFT Magnitude Graph Title Graphical Display Signal Type Real Start Address out_buffer Acquisition Buffer Size 256 Index Increment 1FFT Frame Size 256 FFT Order 8 FFT Windowing Function Rectangle Display Peak and Hold Off DSP Data Type 16-bit signed integer Q-value 0 Sampling Rate (Hz) 8000 Plot Data From Left to Right Left-shifted Data Display Yes Autoscale On

Figure 2.2

Page 27: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 10 NED University of Engineering and Technology Department of Electrical Engineering

- 25 -

LAB SESSION 10

Generation of sine waves using interrupts

OBJECTIVE: To illustrate a Loop Program using interrupt (loop_intr). REQUIREMENTS:

C6713 DSK(DSP Starter Kit) and its support tools Oscilloscope Function Generator Head phones/ Speaker

THEORY: This lab session illustrates input and output with the codec. This program example is very important since it can be used as a base program to build on. For example, to implement a digital filter, one would need to insert the appropriate algorithm between the input and output functions. An interrupt occurs every sample period Ts=1/Fs at which time an input sample value is read from the codec’s ADC and then sent as output to the codec’s DAC. Interrupt: An interrupt can be issued internally or externally. An interrupt stops the current CPU process so that it can perform a required task initiated by the interrupt. The source of the interrupt can be an ADC, timer etc. On an interrupt; the conditions of the current process must be saved so that they can be restored after the interrupt task is performed. Loop: We frequently need to perform an action over and over, often with variations in the details each time. The mechanism that meets this need is the “loop”. In programming, it is often the case that you want to do something a fixed number of times. The loop is ideally suited for such cases. There are three major loop structures in ‘C’: the for loop, the while loop and the do while loop. The program in this exercise uses while loop PROCEDURE:

1. Connect DSK C6713 board to the USB port of PC and give +5V supply to the board.

2. Perform the diagnostic and quick tests of DSK (refer to Appendix A).

Page 28: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 10 NED University of Engineering and Technology Department of Electrical Engineering

- 26 -

3. Connect the DSK by selecting Debug→ Connect, if it has not already been connected.

4. Create New Project (refer to Appendix B). 5. Add files to the project (refer to Appendix C). 6. Build the program (refer to Appendix D). 7. Select File→ Load Program in order to load loop_intr.out on the DSK. 8. Input a sinusoidal waveform to the LINE IN connector on the DSK with an

amplitude of approximately 2V p-p and a frequency between approximately 1and 3 kHz.

9. Select Debug→ Run. OBSERVATION: A tone of same input frequency, but attenuated to approximately 0.8V p-p, can be heard on speaker or can be viewed on an oscilloscope connected to the LINE OUT connector of the DSK. EXERCISE:

Increase the amplitude of the input sinusoidal waveform beyond 6V p-p and observe that the output signal becomes distorted. This is because; the maximum level of the input signal to the codec is 6 Vp-p.

Page 29: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 11 NED University of Engineering and Technology Department of Electrical Engineering

- 27 -

LAB SESSION 11 Computation of DFT using FFT Algorithms in C -6713 DSK OBJECTIVE: To compute DFT of a sequence of real Numbers with Output from the CCS Window (DFT). REQUIREMENT:

1. C6713 DSK(DSP Starter Kit) and its support tools 2. Oscilloscope 3. Function Generator

THEORY: The DFT converts a time - domain sequence into an equivalent frequency – domain sequence. The inverse DFT performs the reverse operation and converts a frequency - domain sequence into an equivalent time - domain sequence. The FFT is a very efficient algorithm technique based on the DFT but with fewer computations required. The FFT is one of the most commonly used operations in digital signal processing to provide a frequency spectrum analysis. PROCEDURE:

1. Connect DSK C6713 board to the USB port of PC and give +5V supply to the board. 2. Perform the diagnostic and quick tests of DSK (refer to Appendix A). 3. Connect the DSK by selecting Debug→ Connect, if it has not already been connected. 4. Create New Project (refer to Appendix B). 5. Add files to the project (refer to Appendix C). 6. Build this project as DFT (refer to Appendix D). The input x(n) is a cosine with N=8 data

points. To test the results, load the program. 7. Select view, Watch Window and insert the two expressions j and out(right click on the

Watch window). Click on +out to expand and view out[0] and out[1], which represent the real and imaginary components respectively.

8. Place a break point at the bracket “}” that follows the DFT function call.

Page 30: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 11 NED University of Engineering and Technology Department of Electrical Engineering

- 28 -

OBSERVATION:

1. Select Debug, Animate(Animation speed can be controlled through j=1 and at j=7, while small otherwise. Since x(n) is a one-cycle sequence, m=1. Since the number of points is N=8, a “spike” occurs at j=m=1 used to verify these results.

2. Note that the data values in the table are rounded (yielding a spike with a maximum value of 3996 in lieu of 4000). Since it is a cosine, the imaginary component out[1] is zero(small). In a real time implementation, with Fs=8KHz, the frequency generated would be at f=Fs(number of cycles)/N=1KHz.

EXERCISE: Use the two-cycle sine data table (in the program) with 20 points as input x (n). Within the program, change N to 20, comment the table that corresponds to the cosine (first input), and instead use the sine table values. Rebuild and animate again. Verify a large negative value at j=N-m=18 (10,232). For a real time implementation, the magnitude of X (k), k=0,1,…. can be found. With Fs=8 KHz, the frequency generated would corresponds to f=800 Hz.

Page 31: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 12 NED University of Engineering and Technology Department of Electrical Engineering

- 29 -

LAB SESSION 12

Designing FIR Filter using C-6713 DSK

OBJECTIVE:

FIR Filter implementation: Bandstop centered at 2700Hz (FIR).

REQUIREMENTS: 1. C6713 DSK(DSP Starter Kit) and its support tools 2. Oscilloscope 3. Function Generator THEORY: A digital filter operates on discrete time signals ad can be implemented with a digital signal processor.. This involves the use of an ADC to capture an external input signal, processing the input samples, and sending the resulting output through a DAC. Digital filters are more reliable, accurate and less sensitive to temperature and aging. Stringent magnitude and phase characteristics can be achieved with a digital filter. Also filer characteristics such as centre frequency, bandwidth ad filter type can readily be modified can readily be modified transform is utilized for the analysis of discrete time signals, similar to the Laplace transform for continuous time signals. PROCEDURE:

1. Connect DSK C6713 board to the USB port of PC and give +5V supply to the board. 2. Perform the diagnostic and quick tests of DSK (refer to Appendix A). 3. Connect the DSK by selecting Debug→ Connect, if it has not already been connected. 4. Create New Project (refer to Appendix B). 5. Add files to the project (refer to Appendix C). 6. Build the program (refer to Appendix D). 7. Select File→ Load Program in order to load FIR.out on the DSK 8. Input a sinusoidal signal and vary the input frequency slightly below and above 2700 Hz.

Page 32: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Lab session 12 NED University of Engineering and Technology Department of Electrical Engineering

- 30 -

OBSERVATION: Verify that the output of bandstop filter is a minimum at 2700Hz. EXERCISE: Within CCS, edit the program FIR.c to include the coefficient file bp1750.cof in lieu of bs2700.cof. The file bp1750.cof represent an FIR bandpass filter centered at 1750 Hz. The coefficients can be obtained from FDA tool of MATLAB.

Page 33: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Appendices NED University of Engineering and Technology Department of Electrical Engineering

- 31 -

APPENDICES

1. In debug options select connect. A message will appear that the target is now connected. 2. Creating new project.

To create the project file sine8_buff, select Project, New. Type sine8_buf for the project name, as shown in figure 2. This project file is saved in the folder sine8_buf (within C:\CCStudio_v3.1\MyProjects). In target field select TMS320C67XX. Click Finish.

Page 34: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Appendices NED University of Engineering and Technology Department of Electrical Engineering

- 32 -

3. Adding files to the project. Go to Project, add files to project and do the following:

(a) Look in CCStudio_v3.1. Select folder C6000, change the file of type to All Files and add the following files:(i) Select cgtools, lib, rts6700 and click open.(ii)Select csl, lib, csl6713 and click open.(iii)Select dsk6713, lib, dsk6713bsl and click open.

(b)Look in Desktop, select folder “CCS C6713 Digital Signal codes”.

Page 35: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Appendices NED University of Engineering and Technology Department of Electrical Engineering

- 33 -

(i) Select folder ‘Support’ and add the following files (i)C6713dsk (ii)C6713dskinit[Type C file not ASM or H file].(iii)Select Vectors_intr or Vectors_poll according to the requirement of the code. In our program its Vectors_intr. (ii) Select sine8_buf folder and then select sine8_buf type C file. 4. Building Program.

Building the program: (a) Go to project and then build options.

(i) In compiler options Basic settings, remove the location from dollar sign to DEBUG and then in quotation marks write the location of sine8_buf i.e C:\CCStudio_v3.1\MyProjects\sine8_buf.

Page 36: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Appendices NED University of Engineering and Technology Department of Electrical Engineering

- 34 -

(ii) In Compiler option Advanced settings change the memory mmodel field to Far{--mem_model:data=far}.

(iii) In Compiler option Preprocessor settings: (a) In Pre-Define Symbol {-d}, write CHIP_6713. (b) In Include Search Path{-i}, write C:\CCStudio_v3.1\C6000\dsk6713\include.

Page 37: EE-493 Digital Signal Processing (DSP)_2012

Digital Signal Processing Appendices NED University of Engineering and Technology Department of Electrical Engineering

- 35 -

(iv) In linker Option delete top field from –m to before -0. Delete –w as well.

(b)Press OK at the end. (c)Press Build Clean in project. (d)Finally press Build to build the program. To open the code go to File, Open, Look in sine8_buf and open sine8_buf C type file.