Você está na página 1de 73

TMS320C5416 DSK

USER MANUAL

Cranes Software International Limited


(TI-Division)
#5, Airport Road, Domlur Layout, Bangalore 560 071.
Phone: 91-80-25352636/37/25353038/4532/25354496 Fax : 25356299
tisupport@cranessoftware.com

Cranes Software Int. Ltd.

TI-DIVISION

Copyright 2008 Cranes Software International Limited. All rights reserved.


Cranes Software believes the information in this publication is accurate as of its publication date.
The information is subject to change without notice. THE INFORMATION IN THIS
PUBLICATION IS PROVIDED "AS IS." CRANES SOFTWARE MAKES NO
REPRESENTATIONS OR WARRANTIES OF ANY KIND WITH RESPECT TO THE
INFORMATION IN THIS PUBLICATION, AND SPECIFICALLY DISCLAIMS IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Use, copying, and distribution of any part in this publication requires an applicable
Software license.

Cranes Software Int. Ltd.

TI-DIVISION

TABLE OF CONTENTS

1. INSTALLATION PROCEDURE. 4
2. DSK FEAUTURES. 8
3. INTRODUCTON TO CODE COMPOSER STUDIO13
4. PROCEDURE TO WORK ON CCS....14
5. EXPERIMENTS USING DSK
a.

SINE WAVE GENERATION

b.

SOLUTION OF DIFFERENCE EQUATIONS

c.

TO VERIFY LINEAR CONVOLUTION

d.

TO VERIFY CIRCULAR CONVOLUTION

e.

PROCEDURE TO WORK IN REALTIME

f.

TO DESIGN FIR FILTER (LOW PASS/HIGH PASS) USING WINDOWING


TECHNIQUE.
a.

USING RECTANGULAR WINDOW

b.

USING TRIANGULAR WINDOW

c.

USING KAISER WINDOW

g.

TO DESIGN IIR FILTER (LP/HP).

h.

NOISE CANCELLATION USING ADAPTIVE FILTERS.

i.

TO FIND THE FFT OF GIVEN 1-D SIGNAL AND PLOT

j.

TO COMPUTE POWER DENSITY SPECTRUM OF A SEQUENCE

k.

MINI PROJECT

Cranes Software Int. Ltd.

TI-DIVISION

INSTALLATION PROCEDURE
SYSTEM REQUIREMENTS

Minimum

233MHz or Higher PentiumCompatible CPU


600MB of free hard disk space
128MB of RAM or more
SVGA (800 x 600 ) display
Internet Explorer (4.0 or later) or
Netscape Navigator (4.7 or later)
Local CD-ROM drive

Recommended

500MHz or Higher Pentium


Compatible CPU

128MB RAM

16bit Color

Supported Operating Systems

Windows NT 4.0 Service Pack 4 or higher


Windows 2000 Service Pack 1
Windows Me
Windows XP

DSK HARDWARE INSTALLATION

Shut down and power off the PC


Connect the supplied USB port cable to the board
Connect the other end of the cable to the USB port of PC
Note: If you plan to install a Microphone, speaker, or
Signal generator/CRO these must be plugged in properly
before you connect power to the DSK

Plug the power cable into the board


Plug the other end of the power cable into a power outlet
The user LEDs should flash several times to indicate board is operational
When you connect your DSK through USB for the first time on a Windows loaded PC the
new hardware found wizard will come up. So, Install the drivers (The CCS CD contains
the require drivers for C5416 DSK).
Install the CCS software for C5416 DSK.

Cranes Software Int. Ltd.

TI-DIVISION

DSK SOFTWARE INSTALLATION


You must install the hardware before you install the software on your system.
The requirements for the operating platform are;

Insert the installation CD into the CD-ROM drive


An install screen appears like below; if not, go to the windows Explorer
And run setup.exe

Cranes Software Int. Ltd.

TI-DIVISION

Choose the option to install Code Composer Studio

If you already have C5000 CC Studio IDE installed on your PC,


do not install DSK software. CC Studio IDE full tools supports
the DSK platform

Respond to the dialog boxes as the installation program runs.


The Installation program automatically configures CC Studio IDE for operation with your
DSK and creates a 5416 DSK CCStudio_v3.1 IDE DSK icon on your desktop.

5416 DSK CCStudio v3.1.lnk

Cranes Software Int. Ltd.

TI-DIVISION

DIAGNOSTIC:

Test the USB port by running DSK Port test from the start menu

Use StartProgramsTexas InstrumentsCode Composer StudioCode Composer Studio


C5416 DSK ToolsC5416 DSK Diagnostic Utilities

5416 DSK Diagnostics Utility v3.1.lnk

(Available on desktop)
Select StartSelect 5416 DSK Diagnostic Utility
The Screen Look like as below. The below Screen will appear
Select Start Option
Utility Program will test the board
After testing Diagnostic Status you will get PASS

If the board still fails to detect

Cranes Software Int. Ltd.

TI-DIVISION

TROUBLESHOOTING:1.

If installing on Windows XP and your PC is connected to the internet through a firewall the
USB install may take up to 15 minutes if you let it complete normally. The work-around for
this issue is to simply disconnect your network cable during the USB hardware install.

2. Make sure all of the Configuration Switches (SW3) are set in the off position. This configures
the DSK for the factory default settings of little endian processor mode booting out of the onboard Flash memory.
3. Some of the Help Files are links to Adobe Acrobat PDF files. If you intend to access these files
you must have Adobe Acrobat installed on your system.
4. If you want to verify a successful USB driver install, open your device manager by right
clicking on the My Computer icon on your desktop and selecting Properties --> HW -->
Device Manager. You should see a new class SD USB Based Debug Tools and one
Spectrum Digital TMS320C5416 DSK installed.
5. The BUSY LED above the USB connector comes on when power is applied to the DSK. Do not
launch Code Composer until the LED is off.
6. Go to CMOS setup Enable the USB Port Option
(The required Device drivers will load along with CCS Installation)
7. Message: Failed: USB device is NOT enumerated or plugged in.
Problem: Windows cannot find the DSK. Check power and USB Cable
8. Messages: Waiting for USB Enumeration.
Status: This is a status message which indicates that Code Composer Studio is waiting for
communication with the on-board JTAG emulator to be established. This should take less than 10
seconds. Do not hit the close button unless it is clear that something is wrong (the progress bar
runs forever).

Cranes Software Int. Ltd.

TI-DIVISION

TMS320C5416 DSK BOARD FEATURES


Package Contents

The 5416 DSP Starter Kit (DSK) is a low-cost platform, which lets enables customers to evaluate
and develop applications for the TI C54X DSP family.
The primary features of the DSK are:
160 MHz TMS320VC5416 DSP
PCM3002 Stereo Codec
Four Position User DIP Switch and Four User LEDs
On-board Flash and SRA
DSK Board Features
Features
TMS320VC5416 DSP
CPLD
External SRAM
External Flash
PCM3002 Codec
4 User LEDs
4 User DIP Switches
4 Jumpers
Daughter card Expansion Interface
HPI Expansion Interface
Embedded JTAG Emulator

Details
160MHz, fixed point, 128Kwords internal RAM
Programmable "glue" logic
64Kwords, 16-bit interface
256Kwords, 16-bit interface
Stereo, 6KHz 48KHz sample rate, 16 or 20 bit
samples, mic-in, line-in, line-out and speaker jacks
Writable through CPLD
Readable through CPLD
Selects power-on configuration and boot modes
Allows user to enhance functionality with add-on
daughter cards
Allows high speed communication with another DSP
Provides high speed JTAG debug through widely
accepted USB host interface

Cranes Software Int. Ltd.

TI-DIVISION

Key Features of the C5416 DSK


C5416 DSK Tutorial: Quick overview of the DSK and its contents, getting started with
Code Composer.
Hardware: Detailed descriptions of the C5416 DSK hardware components.
Board Support Library: Functions that provide an easy interface to DSK hardware.
Examples: Example code that runs on the DSK.
Diagnostic Utility: Host based diagnostics.
Schematics: Schematics for the DSK.
Datasheets: Datasheets related to the DSK.
Troubleshooting/Support: Solutions for problems you might encounter.
TMS320VC5416 KEY FEATURES
128Kwords memory high-speed internal memory for maximum performance.

On-chip PLL generates processor clock rate from slower external clock reference.

Timer generates periodic timer events as a function of the processor clock. Used by
DSP/BIOS to create time slices for multitasking.

DMA Controller 6 channel direct memory access controller for high speed data
transfers without intervention from the DSP.

3 McBSPs Multichannel buffered serial ports. Each McBSP can be used for high-speed
serial data transmission with external devices or reprogrammed as general purpose I/Os.

EMIF External memory interface. The EMIF provides a 16-bit bus on which external
memories and other devices can be connected. It also includes internal wait state and bank
switching controls.

Block diagram of 5416 DSK

10

Cranes Software Int. Ltd.

TI-DIVISION

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

Note:Launch the DSK help file by opening the following file using Windows Explorer.
C:\CCStudio_v3.1\docs\hlp\c5416dsk.hlp
Documents for Reference:
spru509  Code Composer Studio getting started guide.
spru598  Instruction set guide for 54xx family
spru131  CPU & Peripherals guide
pcm3002
 ADC & DAC(CODEC) Data Manual.
spru172  mnemonic instruction guide
spru179  Algebraic Instruction set.
spru173  Application Guide.
Soft Copy of datasheets are available at : C:\CCStudio_v3.1\docs\pdf.

11

Cranes Software Int. Ltd.

TI-DIVISION

Starting Code Composer


To start Code Composer Studio, double click the 5416 DSK CCStudio_v3.1 icon on your desktop.

5416 DSK CCStudio v3.1.lnk

Start Code Composer Studio (ignore this if CCS is already running) by double-clicking on the
C5416 DSK icon on your desktop.
Use the Debug  Connect (Alt+c) menu option to open a debug connection to the DSK board

Procedure to work on Code Composer Studio


Generation of sine waveform
1. To create a New Project
Project  New (Sin.pjt)

12

Cranes Software Int. Ltd.

TI-DIVISION

2. To Create a Source file


File  New source file.
Files of type  C Source File (*.c, *.ccc)

Type the code in editor window.


Save the file in project folder. (Eg: Sin.c).
Important note: Save your source code with preferred language extension. For ASM codes save
the file as code (.asm) For C and C++ codes code (*.c,*.cpp) respectively.

13

Cranes Software Int. Ltd.

TI-DIVISION

3. To Add Source files to Project


Project  Add files to Project  Sin.c
Files of type  C Source File (*.c, *.ccc)

14

Cranes Software Int. Ltd.

TI-DIVISION

4. To Add rts_EXT.lib file.


The run-time-support object libraries do not contain functions involving signals and locale
issues. They do contain the following:

ISO C/C++ standard library

C I/O library

Low-level support functions that provide I/O to the host operating system

Intrinsic arithmetic routines

System startup routine, _c_int00

Functions and macros that allow C/C++ to access specific instructionsrts5400.lib


Run-Time-Support (RTS) object libraries for use with little-endian C/C++ code
Project
 Add files to Project rts_EXT.lib
Files of type  Object and library files (*.o*, *.l*)
Path: C:\CCStudio_v3.1\c5400\cgtools\lib\rts_EXT.lib
5. Add Command and linking file
Linker command files allow you to put linking information in a file; this is useful when you invoke
the linker often with the same information. Linker command files are also useful because they
allow you to use the MEMORY and SECTIONS directives to customize your application.
Project
 Add files to Project hello.cmd
Files of type  Linker command file (*.cmd, *.lcf)
Path: C:\CCStudio_v3.1\tutorial\sim54xx\hello1\hello.cmd

15

Cranes Software Int. Ltd.

TI-DIVISION

5. To Compile:
Project  Compile File
6. To build or Link:
Project  build,
Which will create the final executable (.out) file.(Eg. Sin. out).
7. Procedure to Load and Run program:
Load program to DSK:

File  Load program  Sin. Out


From Debug folder

16

Cranes Software Int. Ltd.

TI-DIVISION

8. To execute project:
Debug  Run.
17

Cranes Software Int. Ltd.

TI-DIVISION

Sin.c
#include<stdio.h>
#include<math.h>
float a[100];
main()
{
int i;
for(i=0;i<99;i++)
{
a[i]= sin(2*3.14*5*i/100);
printf("%f",a[i]);
}
}
Graph Plotting
1. Goto CCS tool bar View Graph Time and frequency.
(In any graph the Start address is the important parameter to be checked.)
Refer fig.1.for the graph properties

Fig. 1

OUTPUT

18

Cranes Software Int. Ltd.

TI-DIVISION

To Perform Single Step Debugging:


1. Keep the cursor on the on to the line from where u want to start single step debugging.
(eg: set a break point on to first line int i=0; of your project.)
To set break point select

icon from tool bar menu.

2. Load the Sin. out file onto the target.


3. Go to view  Watch window.
4. Debug  Run.
5. Execution should halt at break point.
6. Press F10 for step over. See the changes happening in the watch window.

7. Similarly go to view & select CPU registers to view the changes happening in CPU
registers.
8. Repeat steps 2 to 6.

19

Cranes Software Int. Ltd.

TI-DIVISION

Difference Equation
Description:An Nth order linear constant coefficient difference equation can be represented as
N
M

ak y(n-k) =
k=0

br x(n-r)
r=0

If we assume that the system is causal a linear difference equation provides an explicit relationship
between the input and output. This can be seen by rewriting above equation.
M

y(n) =

br/a0 x(n-r) -r=0

ak/a0 y y(n-k)
k=1

C Program to Implement Difference Equation


#include <stdio.h>
#include<math.h>
#define FREQ 400
float y[3]={0,0,0};
float x[3]={0,0,0};
float z[128],m[128],n[128],p[128];
main()
{
int i=0,j;
float a[3]={0.072231,0.144462,0.072231};
float b[3]={ 1.000000,-1.109229,0.398152};
for(i=0;i<128;i++)
{
m[i]=sin(2*3.14*FREQ*i/24000);
}
for(j=0;j<128;j++)
{
x[0]=m[j];
y[0] = (a[0] *x[0]) +(a[1]* x[1] ) +(x[2]*a[2]) - (y[1]*b[1])-(y[2]*b[2]);
z[j]=y[0];
y[2]=y[1];
y[1]=y[0];
x[2]=x[1];
x[1] = x[0];
}
}

20

Cranes Software Int. Ltd.

TI-DIVISION

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:\CCStudio_v3.1\myprojects) with name Diff Eq.pjt.
 Add the source files DIFF EQ1.c
to the project using Project
add files to project pull down menu.
 Add the linker command file hello.cmd
(Path: C:\ CCStudio_v3.1\tutorial\sim54xx\hello1\hello.cmd)
Files of type  Linker command file (*.cmd, *.lcf)
 Add the run time support library file rts_EXT.lib
(Path: C:\ CCStudio_v3.1\c5400\cgtools\lib\rts_EXT.lib)
Files of type  Object and library files (*.o*, *.l*)
 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(Diff Eq.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.

21

Cranes Software Int. Ltd.

TI-DIVISION

Note: To verify the Diffence Equation , Observe the output for high
frequency and low frequency by changing variable FREQ
in the program.

22

Cranes Software Int. Ltd.

TI-DIVISION

LINEAR CONVOLUTION
Description:Linear Convolution Involves the following operations.
1. Folding
2. Multiplication
3. Addition
4. Shifting
These operations can be represented by a Mathematical Expression as follows:

x[ ]= Input signal Samples


h[ ]= Impulse response co-efficient.
y[ ]= Convolution output.
n = No. of Input samples
h = No. of Impulse response co-efficient.
Algorithm to implement C or Assembly program for Convolution:
Eg:

x[n] = {1, 2, 3, 4}
h[k] = {1, 2, 3, 4}

Where: n=4, k=4.

;Values of n & k should be a multiple of 4.


If n & k are not multiples of 4, pad with zeros to make
multiples of 4
r= n+k-1
; Size of output sequence.
= 4+4-1
= 7.

r=
n= 0
1
2
3

0
1
2
3
4
5
x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3]
x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3]
x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3]
x[3]h[0] x[3]h[1] x[3]h[2]

Output:

x[3]h[3]

y[r] = { 1, 4, 10, 20, 25, 24, 16}.

NOTE: At the end of input sequences pad n and k no. of zeros

23

Cranes Software Int. Ltd.

TI-DIVISION

C PROGRAM TO IMPLEMENT LINEAR CONVOLUTION

conv.c:
/* prg to implement linear convolution */
#include<stdio.h>
#include<math.h>
int y[20];
main()
{
int m=6;
/*Lenght of i/p samples sequence*/
int n=6;
/*Lenght of impulse response Co-efficients */
int i=0,j;
int x[15]={1,2,3,4,5,6,0,0,0,0,0,0}; /*Input Signal Samples*/
int h[15]={1,2,3,4,5,6,0,0,0,0,0,0}; /*Impulse Response Co-efficients*/
for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];
}
for(i=0;i<m+n-1;i++)
printf("%d\n",y[i]);
}
PROCEDURE:
 Open Code Composer Studio, make sure the DSP kit is turned on.
 Use the Debug  Connect menu option to open a debug connection to the DSK board
 Start a new project using Project-new pull down menu, save it in a
separate directory(C:\CCStudio_v3.1\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:\CCStudio_v3.1\tutorial\sim54xx\hello1\hello.cmd)
 Add the run time support library file rts_EXT.lib
(Path: C:\CCStudio_v3.1\c5400\cgtools\lib\rts_EXT.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.

24

Cranes Software Int. Ltd.

TI-DIVISION

 To View output graphically


Select view  graph  time and frequency.
Configure the graphical window as shown below

25

Cranes Software Int. Ltd.

TI-DIVISION

CIRCULAR CONVOLUTION
Description:Steps for Cyclic Convolution
Steps for cyclic convolution are the same as the usual convolution, except all index calculations
are done "mod N" = "on the wheel"
Steps for Cyclic Convolution
Step1: Plot f[m] and h[m]

Subfigure 1.1 Subfigure 1.2


Step 2: "Spin" h[m] n times Anti Clock Wise (counter-clockwise) to get h[n-m]
(i.e. Simply rotate the sequence, h[n], clockwise by n steps)

Figure 2: Step 2
Step 3: Point wise multiply the f[m] wheel and the h[nm] wheel. Sum=y[n]
Step 4: Repeat for all 0nN1
Example 1: Convolve (n = 4)

Subfigure 3.1
Subfigure 3.2
Figure 3: Two discrete-time signals to be convolved.

h[m] =
26

Cranes Software Int. Ltd.

TI-DIVISION

Figure 4
Multiply f[m] and sum to yield: y[0] =3

h[1m]

Figure 5
Multiply f[m] and sum to yield: y[1] =5

h[2m]

Figure 6
Multiply f[m] and sum to yield: y[2] =3

h[3m]

Figure 7
Multiply f[m] and sum to yield: y[3] =1

C Program to Implement Circular Convolution


#include<stdio.h>
#include<math.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);
27

Cranes Software Int. Ltd.

TI-DIVISION

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++)
printf("%d \t",y[i]);
}

28

Cranes Software Int. Ltd.

TI-DIVISION

IN PUT:
Eg:
OUT PUT

x[4]={1, 2, 3,4}
h[4]={1, 2, 3,4}
y[4]={26, 28, 26,20}

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:\CCStudio_v3.1\myprojects) with name cir conv.pjt.
 Add the source files Circular Convolution
to the project using Projectadd files to project pull down menu.
 Add the linker command file hello.cmd.
(Path: C:\CCStudio_v3.1\tutorial\sim54xx\hello1\hello.cmd)
 Add the run time support library file rts_EXT.lib
(Path: C:\CCStudio_v3.1\c5400\cgtools\lib\rts_EXT.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.

29

Cranes Software Int. Ltd.

TI-DIVISION

Frequency shift Keying (FSK)


Program for FSK.
/*Fsk.c*/
#include<stdio.h>
#include<string.h>
#include<math.h>
void freq_10( void );
void freq_5( void );
float c[1000];
int k=0;
main()
{
int i, length;
char b[10];
puts("\n Enter the value for FSK in 1's and 0's\n");
gets(b);
length = strlen(b);
for(i=0;i<length;i++)
{
if (b[i]=='1')
freq_10();
else
freq_5();
}
}
void freq_10 ()
{
int j;
for(j=k;j<(k+100);j++)
{
c[j]= sin(2*3.14*10*j/500);
printf("\n%d\t%d\t\t%.3f\n",j,k,c[j]);
}
k=j;
}
void freq_5 ()
{
int j;
for(j=k;j<(k+100);j++)
{
c[j]= sin(2*3.14*5*j/500);
printf("\n%d\t %d\t\t%.3f\n",j,k,c[j]);
}
k=j;
}
30

Cranes Software Int. Ltd.

TI-DIVISION

HOW TO PROCEED
 Open Code Composer Studio; make sure the DSP kit is turned on.
 Use the Debug  Connect menu option to open a debug connection to the DSK board
 Start a new project using Project-new pull down menu, save it in a
Separate directory (C:\CCStudio\myprojects) with name FSK.pjt.
 Add the source files FSK.c in the project using
Project add files to project pull down menu.
 Add the linker command file hello.cmd
 Add the rts file rts_EXT.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 in program memory of DSP chip using the File-load program pull
down menu.
 Run the program and observe output using graph utility.

Graphical display:
1. After run, Go to View<Graph<Time and Frequency.

31

Cranes Software Int. Ltd.

TI-DIVISION

Amplitude Shift Keying (ASK)


ASK.C
#include<stdio.h>
#include<string.h>
#include<math.h>
void freq_10( void );
void freq_0( void );
float c[1000];
int k=0;
main()
{
int i, length;
char b[10];
puts("\n Enter the value for ASK in 1's and 0's\n");
gets(b);
length = strlen(b);
for(i=0;i<length;i++)
{
if (b[i]=='1')
freq_10();
else
freq_0();
}
}
void freq_10 ()
{
int j;
for(j=k;j<(k+100);j++)
{
c[j]= sin(2*3.14*10*j/200);
printf("\n%d\t%d\t\t%.3f\n",j,k,c[j]);
}
k=j;
}
void freq_0 ()
{
int j;
for(j=k;j<(k+100);j++)
{
c[j]= 0;
printf("\n%d\t %d\t\t%.3f\n",j,k,c[j]);
}
k=j;
}

32

Cranes Software Int. Ltd.

TI-DIVISION

HOW TO PROCEED
 Open Code Composer Studio; make sure the DSP kit is turned on.
 Use the Debug  Connect menu option to open a debug connection to the DSK board
 Start a new project using Project-new pull down menu, save it in a
Separate directory (C:\CCStudio\myprojects) with name FSK.pjt.
 Add the source files ASK.c in the project using
Project
add files to project pull down menu.
 Add the linker command file hello.cmd
 Add the rts file rts_EXT.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 in program memory of DSP chip using the File-load program pull
down menu.
 Run the program and observe output using graph utility.

Graphical display:
1. After run, Go to View
Graph
Time and Frequency.

33

Cranes Software Int. Ltd.

TI-DIVISION

TMS320C5416DSK CODEC(PCM3002) Configuration Using Board


Support Library
Description:1.0 Unit Objective:
To configure the codec PCM3002 for a talk through program using the board support library.
2.0 Prerequisites
TMS320C5416DSP Starter Kit, PC with Code Composer Studio, CRO, Audio Source, Speakers
and Signal Generator.
3.0 Discussion on Fundamentals:
Refer BSL API Module under, help  contents  TMS5416 DSK.

Procedure for Real time Programs :


1. Connect a Signal Generator/audio input to the LINE IN Socket or connect a microphone to the
MICIN Socket.

2. Connect CRO/Desktop Speakers to the Socket Provided for LINE OUT or connect a
headphone to the Headphone out Socket.
3. Now Switch on the DSK and Bring Up Code Composer Studio on the PC.
4. Use the Debug  Connect menu option to open a debug connection to the DSK board
34

Cranes Software Int. Ltd.

TI-DIVISION

5. Create a new project with name codec.pjt.


6. Open the File Menu  new  DSP/BIOS Configuration select
dsk5416.cdb and save it as xyz.cdb

7. Add xyz.cdb to the current project.


Project
 Add files to project xyz.cdb
8. Automatically three files are added in the Generated file folder in project pane
xyzcfg.cmd Command and linking file
xyzcfg.s62  optimized assembly code for configuration
xyzcfg_c.c  Chip support initialization
9. Double click on the xyz.cdb from the project explorer and double click on the chip
support library explorer.
10. Double click on the MCBSP under the chip support library where you can see MCBSP
Configuration Manager and MCBSP Resource Manager.
11. Right click on the MCBSP Configuration Manager and select Insert mcbspCfg where
you can see mcbspCfg0 appearing under MCBSP Configuration Manager.
12. Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window
35

Cranes Software Int. Ltd.

TI-DIVISION

appears.
13. Under General property set Breakpoint Emulation to Do Not Stop.
14. Under Transmit modes property set clock polarity to Falling Edge.
15. Under Transmit Lengths property set Word Length Phase1 to 32-bits and set
Words/Frame phase1 to 2.
16. Under Receive modes property set clock polarity to Rising Edge.
17. Under Receive Multichannel property set Rx Channel Enable to All 128 Channels.
18. Under Transmit Multichannel property set Tx Channel Enable to All 128 Channels.
19. Under the Receive Lengths property set Word Length Phase1 to 32-bits and set
Words/Frame phase1 to 2.
20. Under the Sample-Rate Gen property set Generator Clock Source to BCLKR pin.
Set Frame Width to 32 and Frame period to 64.
21. Select Apply and click O.K.
22. Select McBSP2 under the MCBSP Resource Manager.
23. Right click on McBSP2 and select properties where a McBSP2 Properties Window
appears. Enable the Open handle to McBSP option and pre-initialization option. Select
msbspCfg0 under the Pre-initialize pop-up menu and change the Specify Handle
Name property to C54XX_DMA_MCBSP_hMcbsp. Select Apply and click O.K.
24. Add the generated YYYYcfg.cmd file to the current project.
25. Add the given mcbsp_io.c file to the current project which has the main function and calls
all the other necessary routines.
26. View the contents of the generated file YYYYcfg_c.c and copy the include header file
YYYYcfg.h to the mcbsp_io.c file.
27. Add the library file dsk5416f.lib from the location C:\ti\C5400\dsk5416\lib\dsk5416f.lib
to the current project
28. Select project
build options
Compiler
 Advance and enable the use Far calls option.
29. Build, Load and Run the program.
30. You can notice the input signal of 500 Hz. appearing on the CRO verifying the McBSP
configuration.
31. You can also pass an audio input and hear the output signal through the speakers.
32. You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq Function
in the mcbsp_io.c file and repeat the above steps.
33. Open the File Menu  new  Source file
34. Type the code in editor window. Save the file in project folder. (Eg: Codec.c).
Important note: Save your source code with preferred language extension. For ASM
codes save the file as code(.asm) For C and C++ codes code (*.c,*.cpp) respectively.

35. Add the saved Codec.c file to the current project which has the main function and calls all
the other necessary routines.
Project  Add files to Project  Codec.c
36

Cranes Software Int. Ltd.

TI-DIVISION

36. Add the library file dsk5416f.lib to the current project


Path  C:\CCStudio_v3.1\C5400\dsk5416\lib\dsk5416f.lib
Files of type  Object and library files (*.o*, *.l*)
37. Copy header files dsk5416.h and dsk5416_pcm3002.h from and paste it in current
project folder.
C:\CCStudio_v3.1\C5400\dsk5416\include.
38. Add the header file generated within xyzcfg_c.c to codec.c
Note:- Double click on xyzcfg_c.c. Copy the first line header (eg.#include xyzcfg.h) and
paste that in source file (eg.codec.c).
39. Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window.
40. Build the program using the Project-Build pull down menu or by clicking the shortcut icon
on the left side of program window.
41. Load the program (Codec. Out) in program memory of DSP chip using the
File-load program pull down menu.
42. Debug
 Run
43. You can notice the input signal of 500 Hz. appearing on the CRO verifying the codec
configuration.
44. You can also pass an audio input and hear the output signal through the speakers.

5.0 Conclusion:
The codec PCM3002 successfully configured using the board support library
and verified.

37

Cranes Software Int. Ltd.

TI-DIVISION

Mcbsp_io.c:
#include "YYYYcfg.h"
#include <dsk5416.h>
#include <dsk5416_pcm3002.h>
short left_input,right_input;
DSK5416_PCM3002_Config setup = {
0x1ff,
0x1ff,
0x2,
0x0

// Set-Up Reg 0 - Left channel DAC attenuation


// Set-Up Reg 1 - Right channel DAC attenuation
// Set-Up Reg 2 - Various ctl e.g. power-down modes
// Set-Up Reg 3 - Codec data format control

};
void main ()
{
DSK5416_PCM3002_CodecHandle hCodec;
// Initialize the board support library
DSK5416_init();
// Start the codec
hCodec = DSK5416_PCM3002_openCodec(0, &setup);
// Set codec frequency
DSK5416_PCM3002_setFreq(hCodec, 48000);
// Endless loop IO audio codec
while(1){
// Read 16 bits of codec data, loop to retry if data port is busy
while(!DSK5416_PCM3002_read16(hCodec, &left_input));
while(!DSK5416_PCM3002_read16(hCodec, &right_input));
// Write 16 bits to the codec, loop to retry if data port is busy
while(!DSK5416_PCM3002_write16(hCodec, left_input));
while(!DSK5416_PCM3002_write16(hCodec, right_input));
}
}

38

Cranes Software Int. Ltd.

TI-DIVISION

FIR filter Designing Experiments


GENERAL CONSIDERATIONS :
In the design of frequency selective filters, the desired filter characteristics are specified
in the frequency domain in terms of the desired magnitude and phase response of the filter. In the
filter design process, we determine the coefficients of a causal FIR filter that closely approximates
the desired frequency response specifications.

IMPLEMENTATION OF DISCRETE-TIME SYSTEMS :


Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant
coefficient linear difference equations. Representing a system in terms of constant coefficient
linear difference equation is its time domain characterization. In the design of a simple frequency
selective filter, we would take help of some basic implementation methods for realizations of LTI
systems described by linear constant coefficient difference equation.
UNIT OBJECTIVE :
The aim of this laboratory exercise is to design and implement Digital Kaiser low pass FIR Filter
& observe its frequency response. In this experiment we design a simple FIR filter so as to pass
required frequencies components (less than 1000 Hz) and stop or attenuate the frequency
components which are outside the required band ( greater than 1824 Hz).
BACKGROUND CONCEPTS :
A Finite impulse response (FIR) filter possesses an output response to an impulse which is of
infinite duration. The impulse response is "finite" since there is no feedback in the, filter that is if
you put in an impulse, then its output must produced for finite duration of time.
PREREQUISITES :

Concept of Discrete time signal processing.


Analog filter design concepts.
TMS320C5416 Architecture and instruction set.

EQUIPMENTS NEEDED :

Host (PC) with windows (95/98/Me/XP/NT/2000).


DSP kit with TMS 320C5416 chip and driver software.
Oscilloscope and Function generator.

39

Cranes Software Int. Ltd.

TI-DIVISION

ALGORITHM TO IMPLEMENT:
We need to realize the Kaiser high pass FIR filter by implementing the difference equation y[n] =
b0x[n] + b1x[n-1])+b2x[n-2]b2x[n-25] where b0 b25 are feed forward coefficients [Assume
25thorder of filter] . These coefficients are calculated using MATLAB. A direct form I
implementation approach is taken.

Step 1 - Initialize the McBSP, the DSP board and the on board codec.
Kindly refer the Topic Configuration of 5416 McBSP using CSL

Step 2 - Initialize the discrete time system, that is, specify the initial conditions. Generally
zero initial conditions are assumed.

Step 3 - Take sampled data from codec while input is fed to DSP kit from the signal
generator. Since Codec is stereo , take average of input data read from left and right channel .
Store sampled data at a memory location.

Step 4 - Perform filter operation using above said difference equation and store filter Output
at a memory location.

Step 5 - Output the value to codec (left channel and right channel) and view the output at
Oscilloscope.

Step 6 - Go to step 3.

DESIGNING AN FIR FILTER:


Following are the steps to design linear phase FIR filters Using Windowing Method.
I.

Clearly specify the filter specifications.


Eg: Order
= 450;
Sampling Rate = 8000 samples/sec
Cut off Freq.
= 400 Hz.

II.

Compute the cut-off frequency Wc


Eg: Wc = 2*pie* fc / Fs
= 2*pie* 400/8000
= 0.1*pie

III.

Compute the desired Impulse Response h d (n) using particular Window


Eg: b_rect1=fir1(order, Wc , 'high',boxcar(31));

IV.

Convolve input sequence with truncated Impulse Response x (n)*h (n)

40

Cranes Software Int. Ltd.

TI-DIVISION

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].
If Wn is a multi-element vector,
Wn = [W1 W2 W3 W4 W5 ... WN],
FIR1 returns an order N multiband filter with bands
0 < W < W1, W1 < W < W2, ..., WN < W < 1.
B = FIR1(N,Wn,'DC-1') makes the first band a passband.
B = FIR1(N,Wn,'DC-0') makes the first band a stopband.
For filters with a passband near Fs/2, e.g., highpass
and bandstop filters, N must be even.
By default FIR1 uses a Hamming window. Other available windows,
including Boxcar, Hanning, Bartlett, Blackman, Kaiser and Chebwin
can be specified with an optional trailing argument. For example,
B = FIR1(N,Wn,kaiser(N+1,4)) uses a Kaiser window with beta=4.
B = FIR1(N,Wn,'high',chebwin(N+1,R)) uses a Chebyshev window.
By default, the filter is scaled so the center of the first pass band
has magnitude exactly one after windowing. Use a trailing 'noscale'
argument to prevent this scaling, e.g. B = FIR1(N,Wn,'noscale'),
B = FIR1(N,Wn,'high','noscale'), B = FIR1(N,Wn,wind,'noscale').

Matlab Program to generate FIR Filter-Low Pass Coefficients using FIR1


% FIR Low pass filters using rectangular, triangular and kaiser windows
% sampling rate - 8000
order = 50;
cf=[500/4000,1000/4000,1500/4000];

cf--> contains set of cut-off frequencies[Wc ]

41

Cranes Software Int. Ltd.

% cutoff frequency - 500


b_rect1=fir1(order,cf(1),boxcar(51));
b_tri1=fir1(order,cf(1),bartlett(51));
b_kai1=fir1(order,cf(1),kaiser(51,8));

TI-DIVISION

Rectangular
Triangular
Kaisar [Where 8-->Beta Co-efficient]

% cutoff frequency - 1000


b_rect2=fir1(order,cf(2),boxcar(51));
b_tri2=fir1(order,cf(2),bartlett(51));
b_kai2=fir1(order,cf(2),kaiser(51,8));
% cutoff frequency - 1500
b_rect3=fir1(order,cf(3),boxcar(51));
b_tri3=fir1(order,cf(3),bartlett(51));
b_kai3=fir1(order,cf(3),kaiser(51,8));
fid=fopen('FIR_lowpass_rectangular.txt','wt');
fprintf(fid,'\t\t\t\t\t\t%s\n','Cutoff -400Hz');
fprintf(fid,'\nfloat b_rect1[51]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect1);
fseek(fid,-1,0);
fprintf(fid,'};');
fprintf(fid,'\n\n\n\n');
fprintf(fid,'\t\t\t\t\t\t%s\n','Cutoff -800Hz');
fprintf(fid,'\nfloat b_rect2[51]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect2);
fseek(fid,-1,0);
fprintf(fid,'};');
fprintf(fid,'\n\n\n\n');
fprintf(fid,'\t\t\t\t\t\t%s\n','Cutoff -1200Hz');
fprintf(fid,'\nfloat b_rect3[51]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect3);
fseek(fid,-1,0);
fprintf(fid,'};');
fclose(fid);
winopen('FIR_highpass_rectangular.txt');

42

Cranes Software Int. Ltd.

TI-DIVISION

T.1 : Matlab generated Coefficients for FIR Low Pass Kaiser filter:
Cutoff -500Hz
float b_kai1[51]={-0,0,0.0001,0.0002,0.0005,0.0008,0.0012,0.0013,0.001,-0,-0.0018,-0.0045,
-0.0076,-0.0106,-0.0125,-0.0121,0.0082,0,0.0129,0.0302,0.0506,0.0723,0.0929,0.1099,0.1211,0.125,0.1211,0.1099,0.0929,0.0723,
0.0506,0.0302,0.0129,0,-0.0082,-0.0121,-0.0125,-0.0106,-0.0076,-0.0045,-0.0018,0,0.001,0.0013,0.0012,0.0008,0.0005,0.0002,0.0001,0,-0};
Cutoff -1000Hz
IMPLEMENTATION OF AN FIR FILTER :
float b_kai2[51]={- 0,-0,-0.0001,-0.0003,-0.0004,0,0.0009,0.0019,0.0018,-0,-0.0034,-0.0064,0.0059,0,0.0096,0.0171,0.0152,-0,-0.0238,-0.0426,ALGORITHM TO IMPLEMENT :
0.0387,0,0.0711,0.1554,0.2237,0.25,0.2237,0.1554,0.0711,0,-0.0387,-0.0426,-0.0238,0,0.0152,0.0171,0.0096,0,-0.0059,-0.0064,-0.0034,-0,0.0018,0.0019,0.0009,0,-0.0004,-0.0003,We need to realize an advance FIR filter by implementing its difference equation as per the
0.0001,-0,0};
specifications. A direct form I implementation approach is taken. (The filter coefficients are taken
as ai as generated by the Matlab program.)
Cutoff -1500Hz
float b_kai3[51]={ 0,0.0001,0.0002,0.0001,-0.0003,-0.0008,-0.001,0,0.002,0.0035,0.0024,
0.0022,-0.0078,-0.0092,-0.0023,0.011,0.0214,0.0173,-0.0059,-0.0369,-0.0514,0.0247,0.0503,0.153,0.2424,0.2778,0.2424,0.153,0.0503,-0.0247,-0.0514,-0.0369,0.0059,0.0173,0.0214,0.011,-0.0023,-0.0092,-0.0078,-0.0022,0.0024,0.0035,0.002,0,-0.001,0.0008,-0.0003,0.0001,0.0002,0.0001,0};

T.2 :Matlab generated Coefficients for FIR Low Pass Rectangular filter
Cutoff -500Hz
float b_rect1[51]={-0.0046,0,0.005,0.0097,0.0133,0.0151,0.0147,0.0118,0.0068,-0,-0.0077,0.0152,-0.0214,-0.0251,-0.0253,-0.0213,0.0128,0,0.0165,0.0355,0.0557,0.0754,0.0929,0.1066,0.1154,0.1184,0.1154,0.1066,0.0929,0.0754
,0.0557,0.0355,0.0165,0,-0.0128,-0.0213,-0.0253,-0.0251,-0.0214,-0.0152,-0.0077,0,0.0068,0.0118,0.0147,0.0151,0.0133,0.0097,0.005,0,-0.0046};
Cutoff -1000Hz
float b_rect2[51]={-0.0091,-0,-0.0099,-0.0147,-0.0109,0,0.012,0.0179,0.0134,-0,-0.0152,0.0231,-0.0176,0,0.0207,0.0323,0.0254,-0,-0.0326,-0.0538,0.0456,0,0.0761,0.1614,0.2282,0.2535,0.2282,0.1614,0.0761,0,-0.0456,-0.0538,-0.0326,0,0.0254,0.0323,0.0207,0,-0.0176,-0.0231,-0.0152,-0,0.0134,0.0179,0.012,0,-0.0109,-0.0147,0.0099,-0,0.0091};
Cutoff -1500Hz
float b_rect2[51]={- 0.0021,0.0112,0.0126,0.0048,-0.0074,-0.0152,0.0125,0,0.0139,0.019,0.0103,-0.0076,-0.0224,-0.0223,-0.0049,0.0199,0.0344,0.0249,-0.0077,0.0447,-0.0582,-0.0265,0.0516,0.1524,0.237,0.27,0.237,0.1524,0.0516,-0.0265,-0.0582,-0.0447,T.3
: Matlab generated Coefficients for FIR Low Pass Triangular filter
0.0077,0.0249,0.0344,0.0199,-0.0049,-0.0223,-0.0224,-0.0076,0.0103,0.019,0.0139,0,-0.0125,0.0152,-0.0074,0.0048,0.0126,0.0112,0.0021};
43

Cranes Software Int. Ltd.

TI-DIVISION

T.3 : Matlab generated Coefficients for FIR Low Pass Triangular filter
Cutoff -500Hz
float b_tri1[51]={ [0,0,0.0005,0.0013,0.0024,0.0034,0.004,0.0037,0.0024,-0,-0.0035,-0.0075,0.0116,-0.0147,-0.016,-0.0144,0.0092,0,0.0134,0.0304,0.0502,0.0713,0.092,0.1105,0.1248,0.1334,0.1248,0.1105,0.092,0.071
3,0.0502,0.0304,0.0134,0,-0.0092,-0.0144,-0.016,-0.0147,-0.0116,-0.0075,-0.0035,0,0.0024,0.0037,0.004,0.0034,0.0024,0.0013,0.0005,0,0;]};
Cutoff -1000Hz
float b_tri2[51]={ 0,-0,-0.0008,-0.0018,-0.0018,0,0.0029,0.0051,0.0044,-0,-0.0062,-0.0103,0.0086,0,0.0118,0.0197,0.0165,-0,-0.0239,-0.0416,0.0372,0,0.0682,0.1512,0.2232,0.2582,0.2232,0.1512,0.0682,0,-0.0372,-0.0416,-0.0239,0,0.0165,0.0197,0.0118,0,-0.0086,-0.0103,-0.0062,-0,0.0044,0.0051,0.0029,0,-0.0018,-0.0018,0.0008,-0,0};
Cutoff -1500Hz
float b_tri3[51]={ 0,0.0005,0.0011,0.0006,-0.0012,-0.0032,-0.0032,0,0.0047,0.0073,0.0044,0.0035,-0.0114,-0.0123,-0.0029,0.0126,0.0233,0.0179,-0.0058,-0.0359,-0.0492,0.0235,0.048,0.1483,0.2407,0.2857,0.2407,0.1483,0.048,-0.0235,-0.0492,-0.0359,0.0058,0.0179,0.0233,0.0126,-0.0029,-0.0123,-0.0114,-0.0035,0.0044,0.0073,0.0047,0,0.0032,-0.0032,-0.0012,0.0006,0.0011,0.0005,0};

44

Cranes Software Int. Ltd.

TI-DIVISION

FLOWCHART FOR FIR:


Start

Initialize the DSP Board.

Take a new input in data


from the analog in of codec in
data

Initialize Counter = 0
Initialize Output = 0 , i = 0

Output += coeff[N-i]*val[i]
Shift the input value by one

No
Is the loop
Cnt = order
Poll the ready bit, when
asserted proceed.

Yes
Output += coeff[0]*data
Put the data in val array.

Write the value Output to


Analog output of the codec

45

Cranes Software Int. Ltd.

TI-DIVISION

C Program to implement FIR filter:


#include "xyzcfg.h"
#include <dsk5416.h>
#include <dsk5416_pcm3002.h>
Int16 left_input;
Int16 left_output;
Int16 right_input;
Int16 right_output;
static short in_buffer[100];
float filter_Coeff[]={- 0,-0,-0.0001,-0.0003,-0.0004,0,0.0009,0.0019,0.0018,-0,-0.0034,-0.0064,0.0059,0,0.0096,0.0171,0.0152,-0,-0.0238,-0.0426,0.0387,0,0.0711,0.1554,0.2237,0.25,0.2237,0.1554,0.0711,0,-0.0387,-0.0426,-0.0238,0,0.0152,0.0171,0.0096,0,-0.0059,-0.0064,-0.0034,-0,0.0018,0.0019,0.0009,0,-0.0004,-0.0003,0.0001,-0,0};
/*Select particular set of co-efficients different Cut-off frequencies from above given Tables */
DSK5416_PCM3002_Config setup = {
0x1ff,
0x1ff,
0x2,
0x0

// Set-Up Reg 0 - Left channel DAC attenuation


// Set-Up Reg 1 - Right channel DAC attenuation
// Set-Up Reg 2 - Various ctl e.g. power-down modes
// Set-Up Reg 3 - Codec data format control

};
void main ()
{
DSK5416_PCM3002_CodecHandle hCodec;
// Initialize the board support library
DSK5416_init();
// Start the codec
hCodec = DSK5416_PCM3002_openCodec(0, &setup);
// Set codec frequency
DSK5416_PCM3002_setFreq(hCodec,8000);
// Endless loop IO audio codec
while(1)
{
// Read 16 bits of codec data, loop to retry if data port is busy
while(!DSK5416_PCM3002_read16(hCodec, &left_input));
while(!DSK5416_PCM3002_read16(hCodec, &right_input));
left_output=FIR_FILTER(&filter_Coeff ,left_input);
right_output=left_output;
// Write 16 bits to the codec, loop to retry if data port is busy
46

Cranes Software Int. Ltd.

TI-DIVISION

while(!DSK5416_PCM3002_write16(hCodec, left_output));
while(!DSK5416_PCM3002_write16(hCodec, right_output));
}
}
signed int FIR_FILTER(float * h, signed int x)
{
int i=0;
signed long output=0;
in_buffer[0] = x; /* new input at buffer[0] */
for(i=51;i>0;i--)
in_buffer[i] = in_buffer[i-1]; /* shuffle the buffer */
for(i=0;i<51;i++)
output = output + h[i] * in_buffer[i];
return(output);
}

HOW TO PROCEED:

1. Connect a Signal Generator/audio input to the LINE IN Socket or connect a microphone to


the MIC IN Socket.
2. Connect CRO/Desktop Speakers to the Socket Provided for LINE OUT or connect a
headphone to the Headphone out Socket.
3. Now Switch on the DSK and Bring Up Code Composer Studio on the PC.
4. Use the Debug  Connect menu option to open a debug connection to the DSK board
5. Create a new project with name Fir.pjt.
6. Open the File Menu  new  DSP/BIOS Configuration select
dsk5416.cdb and save it as xyz.cdb
7. Add xyz.cdb to the current project.
Project
 Add files to project xyz.cdb
8. Automatically three files are added in the Generated file folder in project pane
xyzcfg.cmd Command and linking file
xyzcfg.s62  optimized assembly code for configuration
xyzcfg_c.c  Chip support initialization
47

Cranes Software Int. Ltd.

TI-DIVISION

9. Open the File Menu  new  Source file


10. Type the code in editor window. Save the file in project folder. (Eg: Codec.c).
Important note: Save your source code with preferred language extension. For ASM
codes save the file as code(.asm) For C and C++ codes code (*.c,*.cpp) respectively.

11. Add the saved Fir.c file to the current project which has the main function and calls all
the other necessary routines.
Project  Add files to Project  Codec.c
12. Add the library file dsk5416f.lib to the current project
Path  C:\CCStudio_v3.1\C5400\dsk5416\lib\dsk5416f.lib
Files of type  Object and library files (*.o*, *.l*)
13. Copy header files dsk5416.h and dsk5416_pcm3002.h from and paste it in current
project folder.
C:\CCStudio_v3.1\C5400\dsk5416\include.
14. Add the header file generated within xyzcfg_c.c to Fir.c
Note:- Double click on xyzcfg_c.c Copy the first line header (eg.xyzcfg.h) and paste that in
source file (eg.Fir.c).
15. Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window.
16. Build the program using the Project-Build pull down menu or by clicking the shortcut
icon on the left side of program window.
17. Load the program (Codec.out) in program memory of DSP chip using the
File-load program pull down menu.
18. Debug
 Run

48

Cranes Software Int. Ltd.

TI-DIVISION

MATLAB GENERATED FREQUENCY RESPONSE


High Pass FIR filter(Fc= 800Hz).

Low Pass FIR filter (Fc=1000Hz)

49

Cranes Software Int. Ltd.

TI-DIVISION

Advance Discrete Time Filter Design(IIR)


IIR filter Designing Experiments
GENERAL CONSIDERATIONS:
In the design of frequency selective filters, the desired filter characteristics are specified in the
frequency domain in terms of the desired magnitude and phase response of the filter. In the filter
design process, we determine the coefficients of a causal IIR filter that closely approximates the
desired frequency response specifications.
IMPLEMENTATION OF DISCRETE-TIME SYSTEMS:
Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant
coefficient linear difference equations. Representing a system in terms of constant coefficient
linear difference equation is its time domain characterization. In the design of a simple frequency
selective filter, we would take help of some basic implementation methods for realizations of LTI
systems described by linear constant coefficient difference equation.
UNIT OBJECTIVE:
The aim of this laboratory exercise is to design and implement a Digital IIR Filter & observe its
frequency response. In this experiment we design a simple IIR filter so as to stop or attenuate
required band of frequencies components and pass the frequency components which are outside
the required band.
BACKGROUND CONCEPTS:
An Infinite impulse response (IIR) filter possesses an output response to an impulse which is of an
infinite duration. The impulse response is "infinite" since there is feedback in the filter, that is if
you put in an impulse, then its output must produced for infinite duration of time.
PREREQUISITES:

Concept of Discrete time signal processing.


Analog filter design concepts.
TMS320C5416 Architecture and instruction set.

EQUIPMENTS NEEDED:

Host (PC) with windows (95/98/Me/XP/NT/2000).


TMS320C5416 DSP Starter Kit (DSK).
Oscilloscope and Function generator.

50

Cranes Software Int. Ltd.

TI-DIVISION

ALGORITHM TO IMPLEMENT:
We need to realize the Butter worth band pass IIR filter by implementing the difference equation
y[n] = b0x[n] + b1x[n-1]+b2x[n-2]-a1y[n-1]-a2y[n-2] where b0 b2, a0-a2 are feed forward and
feedback word coefficients respectively [Assume 2nd order of filter].These coefficients are
calculated using MATLAB.A direct form I implementation approach is taken.

Step 1 - Initialize the McBSP, the DSP board and the on board codec.
Kindly refer the Topic Configuration of 5416Codec using BSL

Step 2 - Initialize the discrete time system, that is, specify the initial conditions. Generally
zero initial conditions are assumed.

Step 3 - Take sampled data from codec while input is fed to DSP kit from the signal
generator. Since Codec is stereo, take average of input data read from left and right channel.
Store sampled data at a memory location.

Step 4 - Perform filter operation using above said difference equation and store filter Output
at a memory location.

Step 5 - Output the value to codec (left channel and right channel) and view the output at
Oscilloscope.

Step 6 - Go to step 3.

51

Cranes Software Int. Ltd.

TI-DIVISION

FLOWCHART FOR IIR IMPLEMENTATION:

Start

Initialize the DSP Board.

Set initial conditions of discrete


time system by making x[0]-x[2]
and y[0]-y[2] equal to zeros and
a0-a2,b0-b2 with MATLAB filter
coefficients
Take a new input and store it in
x[0].
Do y[-3] = y[-2],y[-2]=y[-1]
and Y[-1] = output .
x[-3] = x[-2], x[-2]=x[-1]

output = x[0]b0+x[-1]b1+
x[-2]b2 - y[-1]a1 - y[-2]a2

x[-1]=x[0]
Poll for ready bit
Poll the ready bit, when
asserted proceed.

Write output to analog i/o.

Stop
F.1 : Flowchart for implementing IIR filter.

52

Cranes Software Int. Ltd.

TI-DIVISION

MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS


% IIR Low pass Butterworth and Chebyshev filters
% sampling rate - 24000
order = 2;
cf=[2500/12000,8000/12000,1600/12000];
% cutoff frequency - 2500
[num_bw1,den_bw1]=butter(order,cf(1));
[num_cb1,den_cb1]=cheby1(order,3,cf(1));
% cutoff frequency - 8000
[num_bw2,den_bw2]=butter(order,cf(2));
[num_cb2,den_cb2]=cheby1(order,3,cf(2));
fid=fopen('IIR_LP_BW.txt','wt');
fprintf(fid,'\t\t-----------Pass band range: 0-2500Hz----------\n');
fprintf(fid,'\t\t-----------Magnitude response: Monotonic-----\n\n\');
fprintf(fid,'\n float num_bw1[9]={');
fprintf(fid,'%f,%f,%f,%f,%f,\n%f,%f,%f,%f};\n',num_bw1);
fprintf(fid,'\nfloat den_bw1[9]={');
fprintf(fid,'%f,%f,%f,%f,%f,\n%f,%f,%f,%f};\n',den_bw1);
fprintf(fid,'\n\n\n\t\t-----------Pass band range: 0-8000Hz----------\n');
fprintf(fid,'\t\t-----------Magnitude response: Monotonic-----\n\n');
fprintf(fid,'\nfloat num_bw2[9]={');
fprintf(fid,'%f,%f,%f,%f,%f,\n%f,%f,%f,%f};\n',num_bw2);
fprintf(fid,'\nfloat den_bw2[9]={');
fprintf(fid,'%f,%f,%f,%f,%f,\n%f,%f,%f,%f};\n',den_bw2);
fclose(fid);
winopen('IIR_LP_BW.txt');
fid=fopen('IIR_LP_CHEB Type1.txt','wt');
fprintf(fid,'\t\t-----------Pass band range: 2500Hz----------\n');
fprintf(fid,'\t\t-----------Magnitude response: Rippled (3dB) -----\n\n\');
fprintf(fid,'\nfloat num_cb1[9]={');
fprintf(fid,'%f,%f,%f,%f,%f,\n%f,%f,%f,%f};\n',num_cb1);
fprintf(fid,'\nfloat den_cb1[9]={');
fprintf(fid,'%f,%f,%f,%f,%f,\n%f,%f,%f,%f};\n',den_cb1);
fprintf(fid,'\n\n\n\t\t-----------Pass band range: 8000Hz----------\n');
fprintf(fid,'\t\t-----------Magnitude response: Rippled (3dB)-----\n\n');
fprintf(fid,'\nfloat num_cb2[9]={');
fprintf(fid,'%f,%f,%f,%f,%f,\n%f,%f,%f,%f};\n',num_cb2);
fprintf(fid,'\nfloat den_cb2[9]={');
fprintf(fid,'%f,%f,%f,%f,%f,\n%f,%f,%f,%f};\n',den_cb2);
fclose(fid);
winopen('IIR_LP_CHEB Type1.txt');
%%%%%%%%%%%%%%%%%%
figure(1);
[h,w]=freqz(num_bw1,den_bw1);
w=(w/max(w))*12000;
plot(w,20*log10(abs(h)),'linewidth',2)
hold on
[h,w]=freqz(num_cb1,den_cb1);

53

Cranes Software Int. Ltd.

TI-DIVISION

w=(w/max(w))*12000;
plot(w,20*log10(abs(h)),'linewidth',2,'color','r')
grid on
legend('Butterworth','Chebyshev Type-1');
xlabel('Frequency in Hertz');
ylabel('Magnitude in Decibels');
title('Magnitude response of Low pass IIR filters (Fc=2500Hz)');
figure(2);
[h,w]=freqz(num_bw2,den_bw2);
w=(w/max(w))*12000;
plot(w,20*log10(abs(h)),'linewidth',2)
hold on
[h,w]=freqz(num_cb2,den_cb2);
w=(w/max(w))*12000;
plot(w,20*log10(abs(h)),'linewidth',2,'color','r')
grid on
legend('Butterworth','Chebyshev Type-1 (Ripple: 3dB)');
xlabel('Frequency in Hertz');
ylabel('Magnitude in Decibels');
title('Magnitude response in the passband');
axis([0 12000 -20 20]);

IIR_CHEB_LP FILTER CO-EFFICIENTS:


CoEffici
ents
B0
B1
B2
A0
A1
A2

Fc=2500Hz
Floating Point
Values
0.044408
0.088815
0.044408
1.000000
-1.412427
0.663336

Fixed Point
Values(Q15)
1455
1455[B1/2]
1455
32767
-23140[A1/2]
21735

Fc=800Hz
Floating Point
Values
0.005147
0.010295
0.005147
1.000000
-1.844881
0.873965

Fixed Point
Values(Q15)
168
168[B1/2]
168
32767
-30225[A1/2]
28637

Fc=8000Hz
Floating Point
Values
0.354544
0.709088
0.354544
1.000000
0.530009
0.473218

Fixed Point
Values(Q15)
11617
11617[B1/2]
11617
32767
8683[A1/2]
15506

Note: We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values.

IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS:


CoEffici
ents
B0
B1
B2
A0
A1
A2

Fc=2500Hz
Floating Point
Values
0.072231
0.144462
0.072231
1.000000
-1.109229
0.398152

Fixed Point
Values(Q15)
2366
2366[B1/2]
2366
32767
-18179[A1/2]
13046

Fc=800Hz
Floating Point
Values
0.009526
0.019052
0.009526
1.000000
-1.705552,
0.743655

Fixed Point
Values(Q15)
312
312[B1/2]
312
32767
-27943[A1/2]
24367

Fc=8000Hz
Floating Point
Values
0.465153
0.930306
0.465153
1.000000
0.620204
0.240408

Fixed Point
Values(Q15)
15241
15241[B1/2]
15241
32767
10161[A1/2]
7877

Note: We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values.

54

Cranes Software Int. Ltd.

TI-DIVISION

IIR_CHEB_HP FILTER CO-EFFICIENTS:


CoEffici
ents
B0
B1
B2
A0
A1
A2

Fc=2500Hz
Floating Point
Values
0.388513
-0.777027
0.388513
1.000000
-1.118450
0.645091

Fixed Point
Values(Q15)
12730
-12730[B1/2]
12730
32767
-18324[A1/2]
21137

Fc=4000Hz
Floating Point
Values
0.282850
-0.565700
0.282850
1.000000
-0.451410
0.560534

Fixed Point
Values(Q15)
9268
-9268[B1/2]
9268
32767
-7395[A1/2]
18367

Fc=7000Hz
Floating Point
Values
0.117279
-0.234557
0.117279
1.000000
0.754476
0.588691

Fixed Point
Values(Q15)
3842
-3842[B1/2]
3842
32767
12360[A1/2]
19289

Note: We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values.

IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS:


CoEffici
ents
B0
B1
B2
A0
A1
A2

Fc=2500Hz
Floating Point
Values
0.626845
-1.253691
0.626845
1.000000
-1.109229
0.398152

Fixed Point
Values(Q15)
20539
-20539[B1/2]
20539
32767
-18173[A1/2]
13046

Fc=4000Hz
Floating Point
Values
0.465153
-0.930306
0.465153
1.000000
-0.620204
0.240408

Fixed Point
Values(Q15)
15241
-15241[B1/2]
15241
32767
-10161[A1/2]
7877

Fc=7000Hz
Floating Point
Values
0.220195
-0.440389
0.220195
1.000000
0.307566
0.188345

Fixed Point
Values(Q15)
7215
-7215[B1/2]
7215
32767
5039[A1/2}
6171

Note: We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values.

C PROGRAM TO IMPLEMENT IIR FILTER


#include "xyzcfg.h"
#include <dsk5416.h>
#include <dsk5416_pcm3002.h>
Int16 left_input;
Int16 left_output;
Int16 right_input;
Int16 right_output;
const signed int filter_Coeff[ ] =
{
48,48,48, 32767, -30949, 29322
//96,96,96,32767,-30918,29614 /*fc=600*/
//188,259,188,32767,-29498,27695/*fc=1000*/
//11617,11617,11617,32767,8683,15505/*fc=8000*/
//1455,1455,1455,32767,-23140,21735/*fc=2500*/
//5058,-5058,5058,32767,9995,15803/*fc=7000 -HP */
55

Cranes Software Int. Ltd.

TI-DIVISION

//22586,-22586,22586,32767,-20964,15649
//20539,-20539,20539,32767,-18173,13046
//15241,-15241,15241,32767,-10161,7877
};
/*Select particular set of co-efficients different Cut-off frequencies from given Tables */
DSK5416_PCM3002_Config setup = {
0x1ff,
0x1ff,
0x2,
0x0

// Set-Up Reg 0 - Left channel DAC attenuation


// Set-Up Reg 1 - Right channel DAC attenuation
// Set-Up Reg 2 - Various ctl e.g. power-down modes
// Set-Up Reg 3 - Codec data format control

};
void main ()
{
DSK5416_PCM3002_CodecHandle hCodec;
// Initialize the board support library
DSK5416_init();
// Start the codec
hCodec = DSK5416_PCM3002_openCodec(0, &setup);
// Set codec frequency
DSK5416_PCM3002_setFreq(hCodec,24000);
// Endless loop IO audio codec
while(1)
{
// Read 16 bits of codec data, loop to retry if data port is busy
while(!DSK5416_PCM3002_read16(hCodec, &left_input));
while(!DSK5416_PCM3002_read16(hCodec, &right_input));
left_output=IIR_FILTER(&filter_Coeff , left_input);
right_output=left_output;
// Write 16 bits to the codec, loop to retry if data port is busy
while(!DSK5416_PCM3002_write16(hCodec, left_output));
while(!DSK5416_PCM3002_write16(hCodec, right_output));
}
}
signed int IIR_FILTER(const signed int * h, signed int x1)
{
signed int x[6] = { 0, 0, 0, 0, 0, 0 }; /* x(n), x(n-1), x(n-2). Must be static */
signed int y[6] = { 0, 0, 0, 0, 0, 0 }; /* y(n), y(n-1), y(n-2). Must be static */
long temp=0;
temp = x1;
x[0] = (signed int) temp;
temp = ( (long)h[0] * x[0]) ;
temp += ( (long)h[1] * x[1]);
temp += ( (long)h[1] * x[1]);

/* Copy input to temp */


/* Copy input to x[stages][0] */
/* B0 * x(n) */
/* B1/2 * x(n-1) */
/* B1/2 * x(n-1) */
56

Cranes Software Int. Ltd.

TI-DIVISION

temp += ( (long)h[2] * x[2]);

/* B2 * x(n-2) */

temp -= ( (long)h[4] * y[1]);

/* A1/2 * y(n-1) */

temp -= ( (long)h[4] * y[1]);

/* A1/2 * y(n-1) */

temp -= ( (long)h[5] * y[2]);

/* A2 * y(n-2) */

/* Divide temp by coefficients[A0] */


temp >>= 15;
if ( temp > 32767 )
{
temp = 32767;
}
else if ( temp < -32767)
{
temp = -32767;
}
y[0] = (short int) ( temp );
/* Shuffle values along one place for next time */
y[2] = y[1]; /* y(n-2) = y(n-1) */
y[1] = y[0]; /* y(n-1) = y(n) */
x[2] = x[1]; /* x(n-2) = x(n-1) */
x[1] = x[0]; /* x(n-1) = x(n) */
/* temp is used as input next time through */
return ((short int)temp*1);
}
HOW TO PROCEED:
1. Connect a Signal Generator/audio input to the LINE IN Socket or connect a microphone to
the MIC IN Socket.
2. Connect CRO/Desktop Speakers to the Socket Provided for LINE OUT or connect a
headphone to the Headphone out Socket.
3. Now Switch on the DSK and Bring Up Code Composer Studio on the PC.
4. Use the Debug  Connect menu option to open a debug connection to the DSK board
5. Create a new project with name Iir.pjt.
57

Cranes Software Int. Ltd.

TI-DIVISION

6. Open the File Menu  new  DSP/BIOS Configuration select


dsk5416.cdb and save it as xyz.cdb
7. Add xyz.cdb to the current project.
Project
 Add files to project xyz.cdb
8. Automatically three files are added in the Generated file folder in project pane
xyzcfg.cmd Command and linking file
xyzcfg.s62  optimized assembly code for configuration
xyzcfg_c.c  Chip support initialization
9. Open the File Menu  new  Source file
10. Type the code in editor window. Save the file in project folder. (Eg: Iir.c).
Important note: Save your source code with preferred language extension. For ASM
codes save the file as code(.asm) For C and C++ codes code (*.c,*.cpp) respectively.

11. Add the saved Iir.c file to the current project which has the main function and calls all
the other necessary routines.
Project  Add files to Project  Codec.c
12. Add the library file dsk5416f.lib to the current project
Path  C:\CCStudio_v3.1\C5400\dsk5416\lib\dsk5416f.lib
13. Copy header files dsk5416.h and dsk5416_pcm3002.h from and paste it in current
project folder.
C:\CCStudio_v3.1\C5400\dsk5416\include.
14. Add the header file generated within xyzcfg_c.c to Iir.c
Note:- Double click on xyzcfg_c.c Copy the first line header (eg.xyzcfg.h) and paste that in
source file (eg.Fir.c).
15. Compile the program using the Project-compile pull down menu or by
Clicking the shortcut icon on the left side of program window.
16. Build the program using the Project-Build pull down menu or by clicking the shortcut
icon on the left side of program window.
17. Load the program (Iir.out) in program memory of DSP chip using the
File-load program pull down menu.
18. Debug
 Run

58

Cranes Software Int. Ltd.

TI-DIVISION

59

Cranes Software Int. Ltd.

TI-DIVISION

Fast Fourier Transforms(FFT)


The DFT Equation

Twiddle Factor
In the Definition of the DFT, there is a factor called the Twiddle Factor
where N = number of samples.
If we take an 8 bit sample sequence we can represent the twiddle factor as a vector in the unit
circle. e.g.

Note that
It is periodic. (i.e. it goes round and round the circle !!)
1. That the vectors are symmetric
2. The vectors are equally spaced around the circle.
Why the FFT?
If you look at the equation for the Discrete Fourier Transform you will see that it is quite
complicated to work out as it involves many additions and multiplications involving complex
60

Cranes Software Int. Ltd.

TI-DIVISION

numbers. Even a simple eight sample signal would require 49 complex multiplications and 56
complex additions to work out the DFT. At this level it is still manageable, however a realistic
signal could have 1024 samples which requires over 20,000,000 complex multiplications and
additions. As you can see the number of calculations required soon mounts up to unmanageable
proportions.
The Fast Fourier Transform is a simply a method of laying out the computation, which is much
faster for large values of N, where N is the number of samples in the sequence. It is an ingenious
way of achieving rather than the DFT's clumsy P^2 timing.
The idea behind the FFT is the divide and conquer approach, to break up the original N point
sample into two (N / 2) sequences. This is because a series of smaller problems is easier to solve
than one large one. The DFT requires (N-1)^2 complex multiplications and N(N-1) complex
additions as opposed to the FFT's approach of breaking it down into a series of 2 point samples
which only require 1 multiplication and 2 additions and the recombination of the points which is
minimal.
For example Seismic Data contains hundreds of thousands of samples and would take months to
evaluate the DFT. Therefore we use the FFT.
FFT Algorithm
The FFT has a fairly easy algorithm to implement, and it is shown step by step in the list below.
Thjis version of the FFT is the Decimation in Time Method
1. Pad input sequence, of N samples, with ZERO's until the number of samples is the nearest
power of two.
e.g. 500 samples are padded to 512 (2^9)
2. Bit reverse the input sequence.
e.g. 3 = 011 goes to 110 = 6
3. Compute (N / 2) two sample DFT's from the shuffled inputs.
See "Shuffled Inputs"
4. Compute (N / 4) four sample DFT's from the two sample DFT's.
See "Shuffled Inputs"
5. Compute (N / 2) eight sample DFT's from the four sample DFT's.
See "Shuffled Inputs"
6. Until the all the samples combine into one N-sample DFT
61

Cranes Software Int. Ltd.

TI-DIVISION

Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be reordered. For an 8 point signal the original order of the samples is
0, 1, 2, 3, 4, 5, 6, 7
But after decimation the order is
0, 4, 2, 6, 1, 5, 3, 7
At first it may look as if there is no order to this new sequence, BUT if the numbers are
represented as binary a patter soon becomes apparent.

What has happened is that the bit patterns representing the sample number has been reversed. This
new sequence is the order that the samples enter the FFT.
ALGORITHM TO IMPLEMENT FFT:

Step 1 - Select no. of points for FFT(Eg: 64)

Step 2 Generate a sine wave of frequency f (eg: 10 Hz with a sampling rate = No. of
Points of FFT(eg. 64)) using math library function.

Step 3 - Take sampled data and apply FFT algorithm.

Step 4 Use Graph option to view the Input & Output.

Step 5 - Repeat Step-1 to 4 for different no. of points & frequencies.

62

Cranes Software Int. Ltd.

TI-DIVISION

C PROGRAM TO IMPLEMENT FFT:


Main.c (fft 256.c):
#include <math.h>
#define PTS 64
#define PI 3.14159265358979

//# of points for FFT

typedef struct {float real,imag;} COMPLEX;


void FFT(COMPLEX *Y, int n);
//FFT prototype
float iobuffer[PTS];
//as input and output buffer
float x1[PTS];
//intermediate buffer
short i;
//general purpose index variable
short buffercount = 0;
//number of new samples in iobuffer
short flag = 0;
//set to 1 by ISR when iobuffer full
COMPLEX w[PTS];
//twiddle constants stored in w
COMPLEX samples[PTS];
//primary working buffer
main()
{
for (i = 0 ; i<PTS ; i++)
// set up twiddle constants in w
{
w[i].real = cos(2*PI*i/(PTS*2.0)); //Re component of twiddle constants
w[i].imag =-sin(2*PI*i/(PTS*2.0)); //Im component of twiddle constants
}
for (i = 0 ; i < PTS ; i++) //swap buffers
{
iobuffer[i] = sin(2*PI*10*i/64.0);/*10- > freq,
64 -> sampling freq*/
samples[i].real=0.0;
samples[i].imag=0.0;
}
for (i = 0 ; i < PTS ; i++) //swap buffers
{
samples[i].real=iobuffer[i]; //buffer with new data
}
for (i = 0 ; i < PTS ; i++)
samples[i].imag = 0.0;
//imag components = 0
FFT(samples,PTS);
//call function FFT.c
for (i = 0 ; i < PTS ; i++) //compute magnitude
{
x1[i] = sqrt(samples[i].real*samples[i].real
+ samples[i].imag*samples[i].imag);
}
}
//end of main
63

Cranes Software Int. Ltd.

TI-DIVISION

fft.c:
#define PTS 64
//# of points for FFT
typedef struct {float real,imag;} COMPLEX;
extern COMPLEX w[PTS];
//twiddle constants stored in w
void FFT(COMPLEX *Y, int N) //input sample array, # of points
{
COMPLEX temp1,temp2;
//temporary storage variables
int i,j,k;
//loop counter variables
int upper_leg, lower_leg;
//index of upper/lower butterfly leg
int leg_diff;
//difference between upper/lower leg
int num_stages = 0;
//number of FFT stages (iterations)
int index, step;
//index/step through twiddle constant
i = 1;
//log(base2) of N points= # of stages
do
{
num_stages +=1;
i = i*2;
}while (i!=N);
leg_diff = N/2;
//difference between upper&lower legs
step = (PTS*2)/N; //step between values in twiddle.h
for (i = 0;i < num_stages; i++) //for N-point FFT
{
index = 0;
for (j = 0; j < leg_diff; j++)
{
for (upper_leg = j; upper_leg < N; upper_leg += (2*leg_diff))
{
lower_leg = upper_leg+leg_diff;
temp1.real = (Y[upper_leg]).real + (Y[lower_leg]).real;
temp1.imag = (Y[upper_leg]).imag + (Y[lower_leg]).imag;
temp2.real = (Y[upper_leg]).real - (Y[lower_leg]).real;
temp2.imag = (Y[upper_leg]).imag - (Y[lower_leg]).imag;
(Y[lower_leg]).real = temp2.real*(w[index]).real
-temp2.imag*(w[index]).imag;
(Y[lower_leg]).imag = temp2.real*(w[index]).imag
+temp2.imag*(w[index]).real;
(Y[upper_leg]).real = temp1.real;
(Y[upper_leg]).imag = temp1.imag;
}
index += step;
}
leg_diff = leg_diff/2;
step *= 2;
}
64

Cranes Software Int. Ltd.

j = 0;
for (i = 1; i < (N-1); i++)
{
k = N/2;
while (k <= j)
{
j = j - k;
k = k/2;
}
j = j + k;

TI-DIVISION

//bit reversal for resequencing data

if (i<j)
{
temp1.real = (Y[j]).real;
temp1.imag = (Y[j]).imag;
(Y[j]).real = (Y[i]).real;
(Y[j]).imag = (Y[i]).imag;
(Y[i]).real = temp1.real;
(Y[i]).imag = temp1.imag;
}
}
return;
}
HOW TO PROCEED
1. Open Code Composer Studio; make sure the DSP kit is turned on.
2. Start a new project using Project-new pull down menu, save it in a
separate directory(C:\CCstudio_v3.1\myprojects) with name FFT.pjt.
3. Add the source files FFT256.c and FFT.C in the project using
Project
add files to project pull down menu.
4. Add the linker command file hello.cmd
5. Add the rts file rts_EXT.lib
6. Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window.
7. Project
 Build
8. Load the program in program memory of DSP chip using the File-load program pull
down menu.
9. Run the program and observe output using graph utility.

65

Cranes Software Int. Ltd.

TI-DIVISION

Input:

Output:

66

Cranes Software Int. Ltd.

TI-DIVISION

POWER SPECTRUM
Description:The total or the average power in a signal is often not of as great an interest. We are most often
interested in the PSD or the Power Spectrum. We often want to see is how the input power has
been redistributed by the channel and in this frequency-based redistribution of power is where
most of the interesting information lies. The total area under the Power Spectrum or PSD is equal
to the total avg. power of the signal. The PSD is an even function of frequency or in other words
To compute PSD:
The value of the auto-correlation function at zero-time equals the total power in the signal. To
compute PSD We compute the auto-correlation of the signal and then take its FFT. The autocorrelation function and PSD are a Fourier transform pair. (Another estimation method called
period gram uses sampled FFT to compute the PSD.)
E.g.: For a process x(n) correlation is defined as:

R ( ) = E { x ( n ) x ( n + ) }

= lim

1
N

x(n ) x (n + )

n =1

Power Spectral Density is a Fourier transform of the auto correlation.


N 1

S ( ) = F T ( R ( )) = lim

R ( ) = F T

( S ( )) =

R ( ) e j

= N +1

1
2

S ( ) e

ALGORITHM TO IMPLEMENT PSD:

Step 1 - Select no. of points for FFT(Eg: 64)

Step 2 Generate a sine wave of frequency f (eg: 10 Hz with a sampling rate = No. of
Points of FFT (eg. 64)) using math library function.

Step 3 - Compute the Auto Correlation of Sine wave

Step4 - Take output of auto correlation, apply FFT algorithm.

Step 4 - Use Graph option to view the PSD.

Step 5 - Repeat Step-1 to 4 for different no. of points & frequencies.


67

Cranes Software Int. Ltd.

TI-DIVISION

C Program to Implement PSD:


PSD.c:
/*************************************************************
* FILENAME
* Non_real_time_PSD.c
* DESCRIPTION
* Program to Compute Non real time PSD
***************************************************************
* DESCRIPTION
* Number of points for FFT (PTS)
* x --> Sine Wave Co-Efficients
* iobuffer --> Out put of Auto Correlation.
* x1 --> use in graph window to view PSD
/*===========================================================*/

#include <math.h>
#define PTS 128
#define PI 3.14159265358979

//# of points for FFT

typedef struct {float real,imag;} COMPLEX;


void FFT(COMPLEX *Y, int n);
//FFT prototype
float iobuffer[PTS];
//as input and output buffer
float x1[PTS],x[PTS];
//intermediate buffer
short i;
//general purpose index variable
short buffercount = 0;
//number of new samples in iobuffer
short flag = 0;
//set to 1 by ISR when iobuffer full
float y[128];
COMPLEX w[PTS];
//twiddle constants stored in w
COMPLEX samples[PTS];
//primary working buffer
main()
{
float j,sum=0.0 ;
int n,k,i,a;
for (i = 0 ; i<PTS ; i++)
// set up twiddle constants in w
{
w[i].real = cos(2*PI*i/(PTS*2.0));
/*Re component of twiddle constants*/
w[i].imag =-sin(2*PI*i/(PTS*2.0));
/*Im component of twiddle constants*/
}
68

Cranes Software Int. Ltd.

TI-DIVISION

/****************Input Signal X(n) *************************/


for(i=0,j=0;i<PTS;i++)
{ x[i] = sin(2*PI*5*i/PTS);
// Signal x(Fs)=sin(2*pi*f*i/Fs);
samples[i].real=0.0;
samples[i].imag=0.0;
}
/********************Auto Correlation of X(n)=R(t) ***********/
for(n=0;n<PTS;n++)
{
sum=0;
for(k=0;k<PTS-n;k++)
{
sum=sum+(x[k]*x[n+k]); // Auto Correlation R(t)
}
iobuffer[n] = sum;
}
/********************** FFT of R(t) ***********************/
for (i = 0 ; i < PTS ; i++) //swap buffers
{
samples[i].real=iobuffer[i]; //buffer with new data
}
for (i = 0 ; i < PTS ; i++)
samples[i].imag = 0.0;
FFT(samples,PTS);

//imag components = 0
//call function FFT.c

/******************** PSD ********************/


for (i = 0 ; i < PTS ; i++) //compute magnitude
{
x1[i] = sqrt(samples[i].real*samples[i].real
+ samples[i].imag*samples[i].imag);
}

//end of main

69

Cranes Software Int. Ltd.

TI-DIVISION

FFT.c:
#define PTS 128

//# of points for FFT

typedef struct {float real,imag;} COMPLEX;


extern COMPLEX w[PTS];

//twiddle constants stored in w

void FFT(COMPLEX *Y, int N) //input sample array, # of points


{
COMPLEX temp1,temp2;
//temporary storage variables
int i,j,k;
//loop counter variables
int upper_leg, lower_leg; //indexof upper/lower butterfly leg
int leg_diff;
//difference between upper/lower leg
int num_stages = 0;
//number of FFT stages (iterations)
int index, step;
//index/step through twiddle constant
i = 1;
//log(base2) of N points= # of stages
do
{
num_stages +=1;
i = i*2;
}while (i!=N);
leg_diff = N/2;
//difference between upper&lower legs
step = (PTS*2)/N;
//step between values in twiddle.h// 512
for (i = 0;i < num_stages; i++) //for N-point FFT
{
index = 0;
for (j = 0; j < leg_diff; j++)
{

for (upper_leg = j; upper_leg < N; upper_leg += (2*leg_diff))


{
lower_leg = upper_leg+leg_diff;
temp1.real = (Y[upper_leg]).real + (Y[lower_leg]).real;
temp1.imag = (Y[upper_leg]).imag + (Y[lower_leg]).imag;
temp2.real = (Y[upper_leg]).real - (Y[lower_leg]).real;
temp2.imag = (Y[upper_leg]).imag - (Y[lower_leg]).imag;
(Y[lower_leg]).real = temp2.real*(w[index]).real
-temp2.imag*(w[index]).imag;
(Y[lower_leg]).imag = temp2.real*(w[index]).imag
+temp2.imag*(w[index]).real;
(Y[upper_leg]).real = temp1.real;
(Y[upper_leg]).imag = temp1.imag;
}
index += step;
}
leg_diff = leg_diff/2;
70

Cranes Software Int. Ltd.

TI-DIVISION

step *= 2;
}
j = 0;
for (i = 1; i < (N-1); i++)
//bit reversal for resequencing data
{
k = N/2;
while (k <= j)
{
j = j - k;
k = k/2;
}
j = j + k;
if (i<j)
{
temp1.real = (Y[j]).real;
temp1.imag = (Y[j]).imag;
(Y[j]).real = (Y[i]).real;
(Y[j]).imag = (Y[i]).imag;
(Y[i]).real = temp1.real;
(Y[i]).imag = temp1.imag;
}
}
return;
}
HOW TO PROCEED
1. Open Code Composer Studio, make sure the DSP kit is turned on.
2. Start a new project using Project-new pull down menu, save it in a
separate directory(c:\ti\myprojects) with name PSD.pjt.
3. Add the source files PSD.c and FFT.c in the project using
Projectadd files to project pull down menu.
4. Add the linker command file hello.cmd .
5. Add the rts file rts_EXT.lib
6. Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window.
7. Load the program in program memory of DSP chip using the File-load program pull
down menu.
8. Run the program and observe output using graph utility.
71

Cranes Software Int. Ltd.

TI-DIVISION

INPUT:

OUTPUT

72

Cranes Software Int. Ltd.

TI-DIVISION

73

Você também pode gostar