This document contains information about MATLAB and its use for digital signal processing (DSP) experiments. It provides an overview of MATLAB, describing it as an interactive environment for technical computing and its key features. It also outlines the contents of a DSP laboratory manual, which includes experiments using MATLAB and a DSP processor to perform tasks like linear and circular convolution, filtering, and Fourier analysis. The document discusses the tools and commands in MATLAB and provides guidance on getting help and running demonstrations.
This document contains information about MATLAB and its use for digital signal processing (DSP) experiments. It provides an overview of MATLAB, describing it as an interactive environment for technical computing and its key features. It also outlines the contents of a DSP laboratory manual, which includes experiments using MATLAB and a DSP processor to perform tasks like linear and circular convolution, filtering, and Fourier analysis. The document discusses the tools and commands in MATLAB and provides guidance on getting help and running demonstrations.
Prepared by Dr H.S.Prashantha Associate Professor Department of Electronics Communication Engineering PES Institute of Technology Bangalore
Dr H.S.Prashantha, Dept of ECE, PESIT
2
DSP LABORATORY MANUAL
CONTENTS DSP using Matlab 1. Verification of sampling theorem. 2. Impulse response of a given system. 3. Linear convolution of two given sequences. 4. Circular convolution of two given sequences. 5. Autocorrelation of a given sequence and verification of its properties. 6. Cross correlation of a given sequence and verification of its properties 7. Solving a given difference equation. 8. Computation of N point DFT of a given sequence and to plot magnitude and phase Spectrum. 9. Linear convolution of two given sequences using DFT and IDFT. 10. Circular convolution of two given sequences using DFT and IDFT 11. Design and implementation of FIR filter to meet given specification. 12. Design and implementation of IIR filter to meet given specification.
DSP Experiments using DSP processor 1. Linear convolution of two given sequences. 2. Circular convolution of two given sequences. 3. Computation of N point DFT of a given sequence. 4. Realization of an FIR filter (any type) to meet given specification. The input can be a signal from function generator/speech signal. 5. Audio specification such as to plot time and frequency display of microphone plus a cosine using DSP. Read a wav file and match with their respective spectrograms. 6. Noise removal: Add noise above 3KHz and then remove; Interference suppression using 400 Hz ton. 7. Impulse response of a given system of a given system of first and second order.
Dr H.S.Prashantha, Dept of ECE, PESIT
3
Cycle I
DSP using Matlab 1. Verification of sampling theorem. 2. Impulse response of a given system. 3. Linear convolution of two given sequences. 4. Circular convolution of two given sequences. 5. Autocorrelation of a given sequence and verification of its properties. 6. Cross correlation of a given sequence and verification of its properties 7. Solving a given difference equation.
DSP Experiments using DSP processor 8. Linear convolution of two given sequences. 9. Circular convolution of two given sequences. 10. Impulse response of a given system of a given system of first and second order.
Cycle II
DSP using Matlab 1. Computation of N point DFT of a given sequence and to plot magnitude and phase Spectrum. 2. Linear convolution of two given sequences using DFT and IDFT. 3. Circular convolution of two given sequences using DFT and IDFT 4. Design and implementation of FIR filter to meet given specification. 5. Design and implementation of IIR filter to meet given specification.
DSP Experiments using DSP processor 1. Computation of N point DFT of a given sequence. 2. Realization of an FIR filter (any type) to meet given specification. The input can be a signal from function generator/speech signal. 3. Audio specification such as to plot time and frequency display of microphone plus a cosine using DSP. Read a wav file and match with their respective spectrograms. 4. Noise removal: Add noise above 3KHz and then remove; Interference suppression using 400 Hz ton.
Dr H.S.Prashantha, Dept of ECE, PESIT
4
Cycle 1
DSP USING MATLAB
MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++ etc. The key features of MATLAB are as follows High-level language for technical computing Development environment for managing code, files, and data Interactive tools for iterative exploration, design, and problem solving Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration 2-D and 3-D graphics functions for visualizing data Tools for building custom graphical user interfaces
MATLAB (MATrix LABoratory) is a special purpose computer program optimized to perform engineering and scientific calculations. The MATLAB program implements the MATLAB language, and provides a very extensive library of predefined functions to make technical programming tasks easier and more efficient. The extremely wide variety of functions makes it much easier to solve technical problems in MATLAB than in other languages such as FORTRAN or C. The advantages of using MATLAB for technical programming are: Ease of use Platform independence Predefined functions Device-independent plotting Graphical User Interface MATLAB Compiler MATLAB is a huge program, with an incredibly rich variety of functions. There are more than 1000 functions in the basic MATLAB. The disadvantages of MATLAB are: It is interpreted language, and therefore can execute more slowly than compiled languages. Cost. (Student version of MATLAB is available for low cost with almost identical feature) full copy of MATLAB is 5 TO 10 times more expensive than a conventional C or FORTRAN compiler. The functional unit of data in any MATLAB program is the array. An array is a collection of data values organized into rows and columns. When MATLAB executes, it can display several types of windows that accept commands or display information. The three most important types of windows are command windows, where commands may be entered: figure windows, which display plots and graphs: and edit/debug windows which permit a user to create and modify MATLAB programs. The major tools within or accessible from the MATLAB desktop are: The command window: a user can enter interactive commands at the command prompt (>>) in the command window and they will be executed on the spot. Instead of typing commands directly in the command window, a series of commands may be placed in to a single file and the entire file may be executed by Dr H.S.Prashantha, Dept of ECE, PESIT
5
typing its name in the command window. Such files are called script files. Script files are also called M-Files because they have file extension of . M. The command history window: the command history window displays a list of the commands that a user has entered in the command window. The launch pad: it is a special tool that collects references to the documentation, demos, and related tools for MATLAB itself and for each toolkit that you own. The edit/debug window: used to create new M-Files, or to modify existing ones. An edit/debug window is created automatically when you create a new M-File or open an existing one. The edit/debug window is essentially a programming text editor, with the MATLAB languages features highlighted in different colors. Comments in an M-File appear in green, variables and numbers appear in black, character strings appear in red, and language keywords appear in blue. Figure windows: used to display MATLAB graphics. A figure can be two or three-dimensional plot of data, an image or a GUI. The MATLAB workspace: it is a collection of all the variables and arrays that can be used by MATLAB when a particular command, M-File, or function is executing. All commands executed in the command window share a common workspace, so they can all share variables. (Clear deletes all variables from the current workspace). The workspace browser: it provides a graphic display of the same information as whos command, and the display is dynamically updated whenever the contents of workspace change. The workspace browser also allows user to change the contents of any variables in the workspace. (A list of the variables and arrays in the current workspace can be generated with the whos command)
Getting help: by selecting the? Help icon bar from the desktop tool bar, or by typing helpdesk or helpwin in the command window. The other way of obtaining help is by using lookfor command. Help command searches for an exact function name match, whereas the lookfor command searches the quick summary information in each function for a match. Lookfor is slower than help but it improves the chances of getting useful information. For new users of MATLAB, a few demonstrations may help to give you a feel for its capabilities. To run MATLABs built in demonstrations, type demo in the command window, or select demos from the launch pad. The contents of the command window can be cleared at any time using the clc command, and the contents of the current figure window can be cleared at any time using the clf command. The variables in the workspace can be cleared with the clear command. It is a good idea to issue the clear command at the start of each new independent calculation. Another important command is the abort command. If an m-file appears to be running for too long, it might contain an infinite loop, and it will never terminate. In this case the user can regain the control by typing control-c (^c) in the command window. When MATLAB detects a ^c, it interrupts the running program and returns a command prompt. After diary command is typed, a copy of all input and most output typed in the command window is echoed in the diary file. The command diary off suspends input in to the diary file, and the command diary on resumes input again. Dr H.S.Prashantha, Dept of ECE, PESIT
6
MATLAB includes a special command (which) to help you to find out just which version of a file is being executed. and where it is located. The format of this command is which functionname, where functionname is the name of the function that you are trying to locate. The path related function include: * addpath Add directory to MATLAB search path * path Display MATLAB search path * path2rc Add current directory to MATLAB search path * rmpath Remove directory from MATLAB search path
The functional unit of data in any MATLAB program is the array. An array is a collection of data values organized into rows and columns, and known by a single name. MATLAB variable is a region of memory containing an array, which is known by a user- specified name. MATLAB variable names must begin with a letter, followed by any combination of letters, numbers, and the underscore( _ ) character. Only the first 31 characters are significant; if more than 31 are used, the remaining characters will be ignored. If two variables are declared with names that only differ in the 32 nd character, MATLAB will treat them as same variable. Spaces cannot be used in MATLAB variable names, underscore letters can be substituted to create meaningful names. It is important to include a data dictionary in the header of any program that you write. A data dictionary lists the definition of each variable used in a program. The definition should include both a description of the contents of the item and the units in which it is measured. MATLAB language is case-sensitive. It is customary to use lower-case letters for ordinary variable names. The most common types of MATLAB variables are double and char. MATLAB is weakly typed language. Variables are not declared in a program before it is used. MATLAB variables are created automatically when they are initialized. There are three common ways to initialize variables in MATLAB: Assign data to the variable in an assignment system. Input data into the variable from the keyboard. Read data from a file. The semicolon at the end of each assignment statement suppresses the automatic echoing of values that normally occurs whenever an expression is evaluated in an assignment statement. Dr H.S.Prashantha, Dept of ECE, PESIT
7
How to invoke MATLAB? *) Double Click on the MATLAB icon on the desktop . *) You will find a Command window where in which you can type the commands and see the output. For example if you type PWD in the command window, it will print current working directory. *)If you want to create a directory type mkdir mydir in the command window, It will create a directory called pes. *) If you want delete a directory type rmdir mydir in the command window. How to open a file in MATLAB? *) Go to File New M-File and click Then type the program in the file and save the file with an extension of .m. While giving file name we should make sure that given file name should not be a command. It is better to the file name as myconvlution .
How to run a MATLAB file? *) Go to Debugrun and click
1. Program to Verify Sampling Theorem
% program for verification of sampling theorem
clc; % clears the command window clear all; % clears the variables declared
t=0:.001:0.1; f1=input ('Enter the input freequency1 = '); f2=input ('Enter the input freequency2 = ');
y=cos(2*pi*f1*t)+cos(2*pi*f2*t); f3=max(f1,f2);
% under sampling fs=f3; %fs = sampling freequency ts=1/fs; tx=0:ts:0.1; m=max(size(tx)); ys=cos(2*f1*pi*tx)+cos(2*pi*f2*tx); figure(1); subplot(3,1,1); plot(t,y); grid on; title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); Dr H.S.Prashantha, Dept of ECE, PESIT
8
subplot(3,1,2); stem(tx,ys); grid on; title('The sinusoidal signal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,3); plot(tx,ys); grid on; title('The recovered sinusidal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts');
% Right sampling fs=2*f3; %fs = sampling freequency ts=1/fs; tx=0:ts:0.1; m=max(size(tx)); ys=cos(2*pi*f1*tx)+cos(2*pi*f2*tx); figure(2); subplot(3,1,1); plot(t,y); grid on; title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,2); stem(tx,ys); grid on; title('The sinusoidal signal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,3); plot(tx,ys); grid on; title('The recovered sinusidal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts');
% over sampling fs=3*f3; %fs = sampling freequency ts=1/fs; tx=0:ts:0.1; m=max(size(tx)); ys=cos(2*pi*f1*tx)+cos(2*pi*f2*tx); figure(3); Dr H.S.Prashantha, Dept of ECE, PESIT
9
subplot(3,1,1); plot(t,y); grid on; title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,2); stem(tx,ys); grid on; title('The sinusoidal signal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,3); plot(tx,ys); grid on; title('The recovered sinusidal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts');
OUTPUT Under sampling
Right sampling 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 -2 0 2 The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t) Time in seconds A m p l i t u d e
i n
v o l t s 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0 1 2 The sinusoidal signal sampled at fs Hz Time in seconds A m p l i t u d e
i n
v o l t s 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0 1 2 The recovered sinusidal sampled at fs Hz Time in seconds A m p l i t u d e
i n
v o l t s Dr H.S.Prashantha, Dept of ECE, PESIT
10
Over sampling
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 -2 0 2 The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t) Time in seconds A m p l i t u d e
i n
v o l t s 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 -2 0 2 The sinusoidal signal sampled at fs Hz Time in seconds A m p l i t u d e
i n
v o l t s 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 -2 0 2 The recovered sinusidal sampled at fs Hz Time in seconds A m p l i t u d e
i n
v o l t s 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 -2 0 2 The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t) Time in seconds A m p l i t u d e
i n
v o l t s 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 -2 0 2 The sinusoidal signal sampled at fs Hz Time in seconds A m p l i t u d e
i n
v o l t s 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 -2 0 2 The recovered sinusidal sampled at fs Hz Time in seconds A m p l i t u d e
i n
v o l t s Dr H.S.Prashantha, Dept of ECE, PESIT
11
2. Program to find the Impulse response of the system
%To find the Impulse response of a given System %a 0 *y(n)-a 1 *y(n-1)+a 2 *y(n-2)=b 0 *x(n)+b1*x(n-1)+b 2 *x(n-2) clc; clear all; b=input('Enter the coefficiants of x(n) in the order x(n),x(n-1)...in the Matrix form = '); a=input('Enter the coefficiants of y(n) in the order y(n),y(n-1)...in the Matrix form = '); n=[-5:20]; x=[(n==0)]; h=filter(b,a,x) figure(1); subplot(2,1,1); stem(n,x); xlabel('n'); ylabel('amplitude'); title('impulse sequence'); subplot(2,1,2); stem(n,h); xlabel('n'); ylabel('amplitude'); title('impulse response');
OUTPUT Enter the coefficiants of x(n) in the order x(n),x(n-1)...in the Matrix form = [0.2066 0.4131 0.2066] Enter the coefficiants of y(n) in the order y(n),y(n-1)...in the Matrix form = [1 -0.3695 0.1968]
-5 0 5 10 15 20 0 0.5 1 n a m p l i t u d e impulse sequence -5 0 5 10 15 20 -0.2 0 0.2 0.4 0.6 n a m p l i t u d e impulse response Dr H.S.Prashantha, Dept of ECE, PESIT
3. Program to perform linear Convolution of two given sequences
%This program gives the convolution of two sequences clc; clear all; close all; x1=input('enter the first sequence'); %input of first sequence x2=input('enter the second sequence'); %input of second sequence n1=length(x1); n2=length(x2); N=n1+n2-1;
4. Program to perform Circular Convolution of two given sequences
%This program calculates the circular convolution clc; clear all; x=input('Input first sequence'); h=input('Input second sequence'); N1=length(x); N2=length(h); N=max(N1,N2); 1 2 3 4 5 6 7 0 2 4 first sequence s i g n a l time 1 2 3 4 5 6 7 0 2 4 second sequence s i g n a l time 1 2 3 4 5 6 7 0 20 40 convolved sequence s i g n a l time Dr H.S.Prashantha, Dept of ECE, PESIT
14
x=[x zeros(1,N-N1)]; h=[h zeros(1,N-N2)];
for m=1:N y(m)=0; for n=1:N i=m-n+1; if(i<=0) i=N+i; end y(m)=y(m)+x(n)*h(i); end end
disp('first sequence'); x disp('second sequence'); h disp('convolved sequence'); y n=0:N-1; subplot(3,1,1); stem(x); title('first sequence'); ylabel('signal'); xlabel('time');
Input first sequence [1 2 3 4] Input second sequence [1 2 3 4]
first sequence Dr H.S.Prashantha, Dept of ECE, PESIT
15
x =
1 2 3 4
second sequence
h =
1 2 3 4
convolved sequence
y =
26 28 26 20
5. Autocorrelation of a given sequence x=input('enter the sequence'); y=xcorr(x,x); figure; subplot(2,1,1); stem(x); ylabel('amplitude'); 1 1.5 2 2.5 3 3.5 4 0 2 4 first sequence s i g n a l time 1 1.5 2 2.5 3 3.5 4 0 2 4 second sequence s i g n a l time 1 1.5 2 2.5 3 3.5 4 0 20 40 convolved sequence s i g n a l time Dr H.S.Prashantha, Dept of ECE, PESIT
16
xlabel('n'); subplot(2,1,2); stem(fliplr(y)); y ylabel('amplitude'); xlabel('n'); disp('the resultant signal is '); fliplr(y);
Input: Enter the sequence[1 2 3 4] The resultant signal is
y = [ 4 11 20 30 20 11 4]
6. Cross correlation of a given sequence clc; close all; clear all; x=input('enter the sequence'); h=input('enter the second sequence'); y=xcorr(x,h); figure; subplot(3,1,1); 1 1.5 2 2.5 3 3.5 4 0 1 2 3 4 a m p l i t u d e n 1 2 3 4 5 6 7 0 10 20 30 a m p l i t u d e n Dr H.S.Prashantha, Dept of ECE, PESIT
17
stem(x); ylabel('amplitude'); xlabel('n'); subplot(3,1,2); stem(h); ylabel('amplitude'); xlabel('n'); subplot(3,1,3); stem(fliplr(y)); y ylabel('amplitude'); xlabel('n'); disp('the resultant signal is '); fliplr(y);
Input: enter the sequence[1 2 3 4] enter the second sequence[4 3 2 1]
1 1.5 2 2.5 3 3.5 4 0 2 4 a m p l i t u d e n 1 1.5 2 2.5 3 3.5 4 0 2 4 a m p l i t u d e n 1 2 3 4 5 6 7 0 10 20 30 a m p l i t u d e n Dr H.S.Prashantha, Dept of ECE, PESIT
18
7. Program to Solve difference equation
%program for solution of difference equation % y(n) -0.5 y(n-1) = x(n) with x(n)=u(n)-u(n-4) b=input('enter the coefficients of x'); a=input('enter the coefficients of y'); n=[-5:50]; x=[(n>=0)]-[(n>4)]; figure(1); subplot(2,1,1); stem(n,x); title('input sequence x(n)'); xlabel('n'); ylabel('x'); subplot(2,1,2); y=filter(b,a,x); stem(n,y); title('output sequence y(n)'); xlabel('n'); ylabel('y');
% y(n) -0.25 y(n-1) -0.125 y(n-2)= x(n) with x(n)=u(n)-u(n-10) and initial condition y(-1)=1, y(-2)=-2
-10 0 10 20 30 40 50 0 0.5 1 input sequence x(n) n x -10 0 10 20 30 40 50 0 0.5 1 1.5 2 output sequence y(n) n y Dr H.S.Prashantha, Dept of ECE, PESIT
19
b=input('enter the coefficients of x'); a=input('enter the coefficients of y'); c=input('enter the initial conditions'); n=[-5:50]; ic=filtic(b,a,c);
-10 0 10 20 30 40 50 0 0.5 1 input sequence x(n) n x -10 0 10 20 30 40 50 0 0.5 1 1.5 2 output sequence y(n) n y Dr H.S.Prashantha, Dept of ECE, PESIT
20
8. Program to Calculate N point DFT and N point IDFT of the given sequence
%to compute dft of the sequence and plot magnitude and phase spectra clc; clear all; close all; % input sequence xn=input('enter the input sequence'); N=input('enter the number of points');
for k=0:1:N-1; for n=0:1:N-1; p=exp(i*2*pi*n*k/N) x2(k+1,n+1)=p; end end 0 5 10 15 20 25 30 35 0 2 4 6 8 fft sequence frequency m a g n i t u d e 0 5 10 15 20 25 30 35 -4 -2 0 2 4 phase of fft sequence frequency p h a s e Dr H.S.Prashantha, Dept of ECE, PESIT
22
xn=(Xk*x2)./N;
9. Linear convolution of two sequences using DFT and IDFT clc; clear all; x=input('Input first sequence'); h=input('Input second sequence'); n1=length(x); n2=length(h); N=n1+n2-1;
subplot(3,1,3); stem(d); title('convolved sequence'); ylabel('signal'); xlabel('time'); Input: x = 1 1 1 1 second sequence h = 1 1 1 1 convolved sequence Dr H.S.Prashantha, Dept of ECE, PESIT
23
d = 1.0000 2.0000 3.0000 4.0000 3.0000 2.0000 1.0000
10. Circular convolution of two sequences using DFT and IDFT clc; clear all; x=input('Input first sequence'); h=input('Input second sequence'); n1=length(x); n2=length(h); N=max(n1,n2);
if N>n1 x1=[x zeros(1,N-n1)]; end if N>n2 x2=[h zeros(1,N-n2)]; end a=fft(x,N); b=fft(h,N); c=a.*b; d=ifft(c,N);
disp('first sequence'); x disp('second sequence'); h disp('convolved sequence'); d n=0:N-1; 1 1.5 2 2.5 3 3.5 4 0 0.5 1 first sequence s i g n a l time 1 1.5 2 2.5 3 3.5 4 0 0.5 1 second sequence s i g n a l time 1 2 3 4 5 6 7 0 2 4 convolved sequence s i g n a l time Dr H.S.Prashantha, Dept of ECE, PESIT
Result: Input first sequence[1 2 3] Input second sequence[1 2 3 4] first sequence x = 1 2 3 second sequence h = 1 2 3 4 convolved sequence d = 18 16 10 16
Dr H.S.Prashantha, Dept of ECE, PESIT
25
11. Design and implementation of FIR filters to meet given specifications clear all; close all; clc; %cutoff frequency of low pass response at 0.25 pi and a minimum stopband %attenuation of 39 dB. transition band is 0.2 pi wc=0.25*pi; tw=0.2*pi;
%normalize the cutoff frequency and transition width fcn=wc/pi; twn=tw/pi;
%the required transition width is 39 dB. Choose Hanning window %order of the filter disp('order of the filter'); N=ceil(4/twn) if(rem(N,2)==0); N=N+1; end;
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3 0 2 4 first sequence s i g n a l time 1 1.5 2 2.5 3 3.5 4 0 2 4 second sequence s i g n a l time 1 1.5 2 2.5 3 3.5 4 0 10 20 convolved sequence s i g n a l time Dr H.S.Prashantha, Dept of ECE, PESIT
26
y=hanning(N) disp('fir filter coefficients');
b=fir1(N-1,fcn,y)
disp('frequency response'); [H,f]=freqz(b,1,256) H_mag=20*log10(abs(H)); plot(f/pi,H_mag); xlabel('frequency in rad/sec'); ylabel('gain in dB');
output:
%passband edge frequency of 2 kHz, a minimum stopband attenuation of 50 dB %transition band is 0.5 kHzat a sampling frequency of 8 kHz.
fs=8000; %normalizing frequency fn=fs/2; %passband and stopband frequencies fp=2000; tw=500; fs=fp+tw; %normalize the cutoff frequency and transition width fcn=(fp+fs)/(2*fn); twn=tw/fs; %the required transition width is 50 dB. Choose Hamming window %order of the filter disp('order of the filter'); N=ceil(4/twn) if(rem(N,2)==0); N=N+1; end; y=hamming(N) 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 -140 -120 -100 -80 -60 -40 -20 0 20 frequency in rad/sec g a i n
i n
d B Dr H.S.Prashantha, Dept of ECE, PESIT
27
disp('fir filter coefficients'); b=fir1(N-1,fcn,y) disp('frequency response'); [H,f]=freqz(b,1,256) H_mag=20*log10(abs(H)); plot(f/pi,H_mag); xlabel('frequency in rad/sec'); ylabel('gain in dB');
Output:
12. Programs to Design IIR filters
% Program to Design the Digital IIR Butterworth Low pass filter using bilinear transformation
clc; clear all;
pb=input('Enter the pass band edge frequency '); sb=input('Enter the stop band edge frequency '); pbr=input('Enter the pass band attenuation '); sbr=input('Enter the stop band attenuation ');
fs= input('Enter the sampling frequency ');
pbrad=pb*2*pi; sbrad=sb*2*pi;
[n,wn]=buttord(pbrad,sbrad,pbr,sbr,'s'); 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 -120 -100 -80 -60 -40 -20 0 20 frequency in rad/sec g a i n
OUTPUT Enter the pass band edge frequency 1500 Enter the stop band edge frequency 2000 Enter the pass band attenuation 0.5 Enter the stop band attenuation 30 Enter the sampling frequency 8000
Dr H.S.Prashantha, Dept of ECE, PESIT
29
% Program to Disign the Digital IIR Butterworth Low pass filter using impulse invariance method
clc; clear all;
pb=input('Enter the pass band edge frequency '); sb=input('Enter the stop band edge frequency ');
pbr=input('Enter the pass band attenuation in dB '); 0 500 1000 1500 2000 2500 3000 3500 4000 -2000 -1000 0 Frequency (Hz) P h a s e
( d e g r e e s ) 0 500 1000 1500 2000 2500 3000 3500 4000 -500 0 500 Frequency (Hz) M a g n i t u d e
( d B ) Butter worth freequency response 0 500 1000 1500 2000 2500 3000 3500 4000 -1000 -500 0 Frequency (Hz) P h a s e
( d e g r e e s ) 0 500 1000 1500 2000 2500 3000 3500 4000 -500 0 500 Frequency (Hz) M a g n i t u d e
( d B ) Chebychev1 Freequency Response 0 500 1000 1500 2000 2500 3000 3500 4000 -500 0 500 Frequency (Hz) P h a s e
( d e g r e e s ) 0 500 1000 1500 2000 2500 3000 3500 4000 -100 -50 0 Frequency (Hz) M a g n i t u d e
( d B ) Chebychev 2 Freequency Response Dr H.S.Prashantha, Dept of ECE, PESIT
30
sbr=input('Enter the stop band attenuation in dB ');
If Code Composer Studio IDE fails to configure your port correctly, perform the following steps:
Test the USB port by running DSK Port test from the start menu
Use StartProgramsTexas InstrumentsCode Composer StudioCode Composer Studio C6713 DSK ToolsC6713 DSK Diagnostic Utilities The below Screen will appear Select StartSelect 6713 DSK Diagnostic Utility Icon from Desktop The Screen Look like as below Select Start Option Utility Program will test the board After testing Diagnostic Status you will get PASS
If the board still fails to detect
Dr H.S.Prashantha, Dept of ECE, PESIT
32
Go to CMOS setup Enable the USB Port Option (The required Device drivers will load along with CCS Installation)
INTRODUCTION TO CODE COMPOSER STUDIO
Code Composer is the DSP industry's first fully integrated development environment (IDE) with DSP-specific functionality. With a familiar environment liked MS-based C++TM, Code Composer lets you edit, build, debug, profile and manage projects from a single unified environment. Other unique features include graphical signal analysis, injection/extraction of data signals via file I/O, multi-processor debugging, automated testing and customization via a C- interpretive scripting language and much more.
CODE COMPOSER FEATURES INCLUDE:
IDE Debug IDE Advanced watch windows Integrated editor File I/O, Probe Points, and graphical algorithm scope probes Advanced graphical signal analysis Interactive profiling Automated testing and customization via scripting Visual project management system Compile in the background while editing and debugging Multi-processor debugging Help on the target DSP Procedure to work on Code Composer Studio
1. To create a New Project Project New (SUM.pjt)
Dr H.S.Prashantha, Dept of ECE, PESIT
33
2. To Create a Source file File New
type the code (Save & give a name to file, Eg: sum.c).
3. To Add Source files to Project Project Add files to Project sum.c
Dr H.S.Prashantha, Dept of ECE, PESIT
34
4. To Add rts6700.lib file & hello.cmd:
Project Add files to Project rts6700.lib Path: c:\CCStudio\c6000\cgtools\lib\rts6700.lib Dr H.S.Prashantha, Dept of ECE, PESIT
35
Note: Select Object & Library in(*.o,*.l) in Type of files
Project Add files to Project hello.cmd Path: c:\ti\tutorial\dsk6713\hello1\hello.cmd Note: Select Linker Command file(*.cmd) in Type of files
5. To Compile: Project Compile File
6. To build or Link: Project build, Which will create the final executable (.out) file.(Eg. sum.out).
7. Procedure to Load and Run program: Load program to DSK: File Load program sum. out
8. To execute project: Debug Run.
TMS320C6713 is a floating point DSP processor.Basically it is a cisc processor.It has following features *) It can execute eight 32-bit instructions/cycle *) Advanced Very Long Instruction Word DSP core. *) It has eight independent functional units.In which there are 2 ALUs of fixed point, 4 ALUs of Floating and fixed point and 2 multipliers of floating and fixed point. *) Two multi channel audio ports *) 32 bit External memory Interface. DSP Starter kit consists of TMS320C6713 ,CPLD,Flash Memory,SDRAM,Codec.
Code Composer Studio Dr H.S.Prashantha, Dept of ECE, PESIT
36
Code Composer is the Integrated Development Environment(IDE) with DSP specific functionality.Basically code composer studio is a cross compiler.That means the programs written in Higher level languages like Care converted into processor specific languages.
Procedure to work with code composer studio Open the code composer studio,make sure DSP Starter Kit(DSK) is turned on. Create a new project as follows Go to Project->new and click on it , give project_name ,Select target as TMS320C67xx. Then save it in C:\CCStudio\MyProjects Then go Project -> Add Files to Project and click on it , then add the run time support library C:\CCStudio\c6000\cgtools\lib\rts6700.lib Then go Project-> Add Files to Project and click on it , then add the linker command file C:\CCStudio\tutorial\dsk6713\hello1\hello.cmd Then compile the program using Project->compile->click Then go to Project -> Rebuild All. Then go to File->Load program -> project_name.out into the DSP Starter kit. How to view graphical output
To view the graphical output go to View Graph Time/Frequency
1 C PROGRAM TO IMPLEMENT LINEAR CONVOLUTION
/* prg to implement linear convolution */ #include<stdio.h>
#define LENGHT1 6 /*Lenght of i/p samples sequence*/ #define LENGHT2 4 /*Lenght of impulse response Co-efficients */
int x[2*LENGHT1-1]={1,2,3,4,5,6,0,0,0,0,0}; /*Input Signal Samples*/ int h[2*LENGHT1-1]={1,2,3,4,0,0,0,0,0,0,0}; /*Impulse Response Co-efficients*/
Open Code Composer Studio, make sure the DSP kit is turned on.
Start a new project using Project-new pull down menu, save it in a separate directory(c:\ti\myprojects) with name lconv.pjt.
Add the source files conv.c to the project using Projectadd files to project pull down menu.
Add the linker command file hello.cmd . (Path: c:\ti\tutorial\dsk6713\hello1\hello.cmd)
Add the run time support library file rts6700.lib (Path: c:\ti\c6000\cgtools\lib\rts6700.lib)
Compile the program using the Project-compile pull down menu or by clicking the shortcut icon on the left side of program window.
Build the program using the Project-Build pull down menu or by clicking the shortcut icon on the left side of program window.
Load the program(lconv.out) in program memory of DSP chip using the File-load program pull down menu.
To View output graphically Select view graph time and frequency. Configure the graphical window as shown below
Dr H.S.Prashantha, Dept of ECE, PESIT
38
Dr H.S.Prashantha, Dept of ECE, PESIT
39
2 program to Implement Circular Convolution #include<stdio.h> int m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30]; void main() { printf(" enter the length of the first sequence\n"); scanf("%d",&m); printf(" enter the length of the second sequence\n"); scanf("%d",&n); printf(" enter the first sequence\n"); for(i=0;i<m;i++) scanf("%d",&x[i]); printf(" enter the second sequence\n"); for(j=0;j<n;j++) scanf("%d",&h[j]); if(m-n!=0) /*If length of both sequences are not equal*/ { if(m>n) /* Pad the smaller sequence with zero*/ { for(i=n;i<m;i++) h[i]=0; n=m; } for(i=m;i<n;i++) x[i]=0; m=n; } y[0]=0; a[0]=h[0]; for(j=1;j<n;j++) /*folding h(n) to h(-n)*/ a[j]=h[n-j]; /*Circular convolution*/ for(i=0;i<n;i++) y[0]+=x[i]*a[i]; for(k=1;k<n;k++) { y[k]=0; /*circular shift*/ for(j=1;j<n;j++) x2[j]=a[j-1]; x2[0]=a[n-1]; for(i=0;i<n;i++) { a[i]=x2[i]; y[k]+=x[i]*x2[i]; } } /*displaying the result*/ printf(" the circular convolution is\n"); for(i=0;i<n;i++) Dr H.S.Prashantha, Dept of ECE, PESIT
40
printf("%d \t",y[i]);
}
PROCEDURE:
Open Code Composer Studio, make sure the DSP kit is turned on.
Start a new project using Project-new pull down menu, save it in a separate directory(c:\ti\myprojects) with name cir conv.pjt.
Add the source files Circular Convolution.C to the project using Projectadd files to project pull down menu.
Add the linker command file hello.cmd . (Path: c:\ti\tutorial\dsk6713\hello1\hello.cmd)
Add the run time support library file rts6700.lib (Path: c:\ti\c6000\cgtools\lib\rts6700.lib)
Compile the program using the Project-compile pull down menu or by clicking the shortcut icon on the left side of program window.
Build the program using the Project-Build pull down menu or by clicking the shortcut icon on the left side of program window.
Load the program(lconv.out) in program memory of DSP chip using the File-load program pull down menu.
DFT USING DSP KIT
#include <stdio.h> #include <math.h> float x[4]={1,3,2,5}; //input only real sequence
main() { float y[16]; //for 8 point DFT to store real & imaginary float w; int n,k,k1,N=8,xlen=4; for(k=0;k<2*N;k=k+2) { y[k]=0; y[k+1]=0; //initialize real & imag parts k1=k/2; //actual k index for(n=0;n<xlen;n++) Dr H.S.Prashantha, Dept of ECE, PESIT
41
{ w=-2*3.14*k1*n/N; //careful about minus sign y[k]=y[k]+x[n]*cos(w); y[k+1]=y[k+1]+x[n]*sin(w); } printf("%f+j%f \n",y[k],y[k+1]); } }//end of main
Procedure for Real time Programs :
1. Connect CRO to the Socket Provided for LINE OUT.
2. Connect a Signal Generator to the LINE IN Socket.
3. Switch on the Signal Generator with a sine wave of frequency 500 Hz. and Vp-p=1.5v
4. Now Switch on the DSK and Bring Up Code Composer Studio on the PC.
5. Create a new project with name codec.pjt.
6. From the File Menu new DSP/BIOS Configuration select dsk6713.cdb and save it as xyz.cdb
Dr H.S.Prashantha, Dept of ECE, PESIT
42
7. Add xyz.cdb to the current project.
8. Add the given codec.c file to the current project which has the main function and calls all the other necessary routines.
9. Add the library file dsk6713bsl.lib to the current project
Path C:\CCStudio\C6000\dsk6713\lib\dsk6713bsl.lib
10. Copy files dsk6713.h and dsk6713_aic23.h from C:\CCStudio\C6000\dsk6713\include and paste it in current project.
11. Build, Load and Run the program.
12. You can notice the input signal of 500 Hz. appearing on the CRO verifying the codec configuration.
13. You can also pass an audio input and hear the output signal through the speakers.
14. You can also vary the sampling frequency using the DSK6713_AIC23_setFreq Function in the codec.c file and repeat the above steps. Dr H.S.Prashantha, Dept of ECE, PESIT
43
5.0 Conclusion: The codec TLV320AIC23 successfully configured using the board support library and verified.
codec.c
#include "xyzcfg.h"
#include "dsk6713.h" #include "dsk6713_aic23.h"
/* Codec configuration settings */ DSK6713_AIC23_Config config = { \ 0x0017, /* 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume */ \ 0x0017, /* 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume */\ 0x00d8, /* 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume */ \ 0x00d8, /* 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume */ \ 0x0011, /* 4 DSK6713_AIC23_ANAPATH Analog audio path control */ \ 0x0000, /* 5 DSK6713_AIC23_DIGPATH Digital audio path control */ \ 0x0000, /* 6 DSK6713_AIC23_POWERDOWN Power down control */ \ 0x0043, /* 7 DSK6713_AIC23_DIGIF Digital audio interface format */ \ 0x0081, /* 8 DSK6713_AIC23_SAMPLERATE Sample rate control */ \ 0x0001 /* 9 DSK6713_AIC23_DIGACT Digital interface activation */ \ };
/* main() - Main code routine, initializes BSL and generates tone */
void main() { DSK6713_AIC23_CodecHandle hCodec; int l_input, r_input,l_output, r_output;
/* Initialize the board support library, must be called first */ DSK6713_init();
/* Start the codec */ hCodec = DSK6713_AIC23_openCodec(0, &config);
while(1) { /* Read a sample to the left channel */ while (!DSK6713_AIC23_read(hCodec, &l_input));
/* Read a sample to the right channel */ Dr H.S.Prashantha, Dept of ECE, PESIT
44
while (!DSK6713_AIC23_read(hCodec, &r_input));
/* Send a sample to the left channel */ while (!DSK6713_AIC23_write(hCodec, l_input));
/* Send a sample to the right channel */ while (!DSK6713_AIC23_write(hCodec, l_input)); }
/* Close the codec */ DSK6713_AIC23_closeCodec(hCodec); }
USING MATLAB TO DETERMINE FILTER COEFFICIENTS : Using FIR1 Function on Matlab B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter coefficients in length N+1 vector B. The cut-off frequency Wn must be between 0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. The filter B is real and has linear phase, i.e., even symmetric coefficients obeying B(k) = B(N+2-k), k = 1,2,...,N+1. If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N bandpass filter with passband W1 < W < W2. B = FIR1(N,Wn,'high') designs a highpass filter. B = FIR1(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2]. 4. IMPLEMENTATION OF AN FIR FILTER : ALGORITHM TO IMPLEMENT : We need to realize an advance FIR filter by implementing its difference equation as per the specifications. A direct form I implementation approach is taken. (The filter coefficients are taken as a i as generated by the Matlab program.)
Switch on the DSP board. Open the Code Composer Studio. Create a new project Project New (File Name. pjt , Eg: FIR.pjt) Initialize on board codec.
Note: Kindly refer the Topic Configuration of 6713 Codec using BSL
Add the given above C source file to the current project (remove codec.c source file from the project if you have already added). Connect the speaker jack to the input of the CRO. Build the program. Load the generated object file(*.out) on to Target board. Run the program Observe the waveform that appears on the CRO screen. Vary the frequency on function generator to see the response of filter.