Cranes Software International Ltd TI - Solutions
1
USER MANUAL TMS320C5416 DSK
Cranes Software International Ltd No5 Airport Road Domlur Layout Bangalore 560 071
Phone 080-53526365352637 Fax 080-5356299 E mail tisalescranessoftwarecom
Cranes Software International Ltd TI - Solutions
2
CONTENTS
1 DSK FEATURES 2 INSTALLATION PROCEDURE 3 INTRODUCTON TO CODE COMPOSER STUDIO 4 EXPERIMENTS USING DSK
DESIGN amp CONDUCT SIMPLE IIR HIGH PASS FILTER DESIGN amp CONDUCT SIMPLE IIR LOW PASS FILTER DESIGN amp CONDUCT ADVANCE FIR LOW PASS FILTER DESIGN amp CONDUCT ADVANCE FIR HIGH PASS FILTER DESIGN amp CONDUCT SIMPLE FIR LOW PASS FILTER DESIGN amp CONDUCT SIMPLE FIR HIGH PASS FILTER
Cranes Software International Ltd TI - Solutions
3
Package Contents
Overview 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
bull 160 MHz TMS320VC5416 DSP bull PCM3002 Stereo Codec bull Four Position User DIP Switch and Four User LEDs bull On-board Flash and SRA
Cranes Software International Ltd TI - Solutions
4
DSK Board Features
Feature Details TMS320VC5416 DSP 160MHz fixed point 128Kwords internal RAM CPLD Programmable glue logic External SRAM 64Kwords 16-bit interface External Flash 256Kwords 16-bit interface PCM3002 Codec Stereo 6KHz 48KHz sample rate 16 or 20
bit samples mic line-in line-out and speaker jacks
4 User LEDs Writable through CPLD 4 User DIP Switches Readable through CPLD 4 Jumpers Selects power-on configuration and boot
modes Daughter card Expansion Interface
Allows user to enhance functionality with add-on daughter cards
HPI Expansion Interface Allows high speed communication with another DSP
Embedded JTAG Emulator
Provides high speed JTAG debug through widely accepted USB host interface
Cranes Software International Ltd TI - Solutions
5
Key Features of the C5416 DSK
bull C5416 DSK TutorialQuick overview of the DSK and its contents getting started with Code Composer
bull Hardware Detailed descriptions of the C5416 DSK hardware components bull Board Support Library Functions that provide an easy interface to DSK
hardware bull Examples Example code that runs on the DSK bull Diagnostic Utility Host based diagnostics bull Schematics Schematics for the DSK bull Datasheets Datasheets related to the DSK bull TroubleshootingSupport Solutions for problems you might encounter
TMS320VC5416 KEY FEATURES
bull 128Kwords memory high-speed internal memory for maximum performance
bull On-chip PLL generates processor clock rate from slower external clock reference
bull Timer generates periodic timer events as a function of the processor clock
Used by DSPBIOS to create time slices for multitasking
bull DMA Controller 6 channel direct memory access controller for high speed data transfers without intervention from the DSP
bull 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 IOs
bull 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
Cranes Software International Ltd TI - Solutions
6
INSTALLATION DSK HARDWARE INSTALLATION
bull Shut down and power off the PC bull Connect the supplied USB port cable to the board bull Connect the other end of the cable to the USB port of PC
Note If you plan to install a Microphone speaker or Signal generatorCRO these must be plugged in properly before you connect power to the DSK
bull Plug the power cable into the board bull Plug the other end of the power cable into a power outlet bull The user LEDs should flash several times to indicate board is operational bull 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)
bull Install the CCS software for C5416 DSK Troubleshooting DSK Connectivity If Code Composer Studio IDE fails to configure your port correctly perform the following steps
bull 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
bull The below Screen will appear bull Select StartSelect 5416 DSK Diagnostic Utility Icon from Desktop bull The Screen Look like as below bull Select Start Option bull Utility Program will test the board bull After testing Diagnostic Status you will get PASS
Cranes Software International Ltd TI - Solutions
7
If the board still fails to detect Go to CMOS setup Enable the USB Port Option (The required Device drivers will load along with CCS Installation)
Cranes Software International Ltd TI - Solutions
8
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The System requirements for the Host Machine are
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU with USB Ports
bull 600MB of free hard disk space bull 64MB of RAM bull SVGA (800 x 600 ) display bull Internet Explorer (40 or later) or bull Netscape Navigator (47 or later) bull Local CD-ROM drive
bull 500MHz or Higher Pentium Compatible CPU with USB Ports
bull 128MB RAM
bull 16bit Color
Supported Operating Systems
bull Windowsreg 98 bull Windows NTreg 40 Service Pack 4 or higher bull Windowsreg 2000 Service Pack 1 bull Windowsreg Me bull Windowsreg XP
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
bull Insert the installation CD into the CD-ROM drive
An install screen appears if not goes to the windows Explorer and run setupexe
bull Choose the option to install Code Composer Sutido
If you already have C6000 CC Studio IDE installed on your PC do not install DSK software CC Studio IDE full tools supports the DSK platform
bull Respond to the dialog boxes as the installation program runs
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
2
CONTENTS
1 DSK FEATURES 2 INSTALLATION PROCEDURE 3 INTRODUCTON TO CODE COMPOSER STUDIO 4 EXPERIMENTS USING DSK
DESIGN amp CONDUCT SIMPLE IIR HIGH PASS FILTER DESIGN amp CONDUCT SIMPLE IIR LOW PASS FILTER DESIGN amp CONDUCT ADVANCE FIR LOW PASS FILTER DESIGN amp CONDUCT ADVANCE FIR HIGH PASS FILTER DESIGN amp CONDUCT SIMPLE FIR LOW PASS FILTER DESIGN amp CONDUCT SIMPLE FIR HIGH PASS FILTER
Cranes Software International Ltd TI - Solutions
3
Package Contents
Overview 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
bull 160 MHz TMS320VC5416 DSP bull PCM3002 Stereo Codec bull Four Position User DIP Switch and Four User LEDs bull On-board Flash and SRA
Cranes Software International Ltd TI - Solutions
4
DSK Board Features
Feature Details TMS320VC5416 DSP 160MHz fixed point 128Kwords internal RAM CPLD Programmable glue logic External SRAM 64Kwords 16-bit interface External Flash 256Kwords 16-bit interface PCM3002 Codec Stereo 6KHz 48KHz sample rate 16 or 20
bit samples mic line-in line-out and speaker jacks
4 User LEDs Writable through CPLD 4 User DIP Switches Readable through CPLD 4 Jumpers Selects power-on configuration and boot
modes Daughter card Expansion Interface
Allows user to enhance functionality with add-on daughter cards
HPI Expansion Interface Allows high speed communication with another DSP
Embedded JTAG Emulator
Provides high speed JTAG debug through widely accepted USB host interface
Cranes Software International Ltd TI - Solutions
5
Key Features of the C5416 DSK
bull C5416 DSK TutorialQuick overview of the DSK and its contents getting started with Code Composer
bull Hardware Detailed descriptions of the C5416 DSK hardware components bull Board Support Library Functions that provide an easy interface to DSK
hardware bull Examples Example code that runs on the DSK bull Diagnostic Utility Host based diagnostics bull Schematics Schematics for the DSK bull Datasheets Datasheets related to the DSK bull TroubleshootingSupport Solutions for problems you might encounter
TMS320VC5416 KEY FEATURES
bull 128Kwords memory high-speed internal memory for maximum performance
bull On-chip PLL generates processor clock rate from slower external clock reference
bull Timer generates periodic timer events as a function of the processor clock
Used by DSPBIOS to create time slices for multitasking
bull DMA Controller 6 channel direct memory access controller for high speed data transfers without intervention from the DSP
bull 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 IOs
bull 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
Cranes Software International Ltd TI - Solutions
6
INSTALLATION DSK HARDWARE INSTALLATION
bull Shut down and power off the PC bull Connect the supplied USB port cable to the board bull Connect the other end of the cable to the USB port of PC
Note If you plan to install a Microphone speaker or Signal generatorCRO these must be plugged in properly before you connect power to the DSK
bull Plug the power cable into the board bull Plug the other end of the power cable into a power outlet bull The user LEDs should flash several times to indicate board is operational bull 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)
bull Install the CCS software for C5416 DSK Troubleshooting DSK Connectivity If Code Composer Studio IDE fails to configure your port correctly perform the following steps
bull 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
bull The below Screen will appear bull Select StartSelect 5416 DSK Diagnostic Utility Icon from Desktop bull The Screen Look like as below bull Select Start Option bull Utility Program will test the board bull After testing Diagnostic Status you will get PASS
Cranes Software International Ltd TI - Solutions
7
If the board still fails to detect Go to CMOS setup Enable the USB Port Option (The required Device drivers will load along with CCS Installation)
Cranes Software International Ltd TI - Solutions
8
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The System requirements for the Host Machine are
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU with USB Ports
bull 600MB of free hard disk space bull 64MB of RAM bull SVGA (800 x 600 ) display bull Internet Explorer (40 or later) or bull Netscape Navigator (47 or later) bull Local CD-ROM drive
bull 500MHz or Higher Pentium Compatible CPU with USB Ports
bull 128MB RAM
bull 16bit Color
Supported Operating Systems
bull Windowsreg 98 bull Windows NTreg 40 Service Pack 4 or higher bull Windowsreg 2000 Service Pack 1 bull Windowsreg Me bull Windowsreg XP
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
bull Insert the installation CD into the CD-ROM drive
An install screen appears if not goes to the windows Explorer and run setupexe
bull Choose the option to install Code Composer Sutido
If you already have C6000 CC Studio IDE installed on your PC do not install DSK software CC Studio IDE full tools supports the DSK platform
bull Respond to the dialog boxes as the installation program runs
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
3
Package Contents
Overview 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
bull 160 MHz TMS320VC5416 DSP bull PCM3002 Stereo Codec bull Four Position User DIP Switch and Four User LEDs bull On-board Flash and SRA
Cranes Software International Ltd TI - Solutions
4
DSK Board Features
Feature Details TMS320VC5416 DSP 160MHz fixed point 128Kwords internal RAM CPLD Programmable glue logic External SRAM 64Kwords 16-bit interface External Flash 256Kwords 16-bit interface PCM3002 Codec Stereo 6KHz 48KHz sample rate 16 or 20
bit samples mic line-in line-out and speaker jacks
4 User LEDs Writable through CPLD 4 User DIP Switches Readable through CPLD 4 Jumpers Selects power-on configuration and boot
modes Daughter card Expansion Interface
Allows user to enhance functionality with add-on daughter cards
HPI Expansion Interface Allows high speed communication with another DSP
Embedded JTAG Emulator
Provides high speed JTAG debug through widely accepted USB host interface
Cranes Software International Ltd TI - Solutions
5
Key Features of the C5416 DSK
bull C5416 DSK TutorialQuick overview of the DSK and its contents getting started with Code Composer
bull Hardware Detailed descriptions of the C5416 DSK hardware components bull Board Support Library Functions that provide an easy interface to DSK
hardware bull Examples Example code that runs on the DSK bull Diagnostic Utility Host based diagnostics bull Schematics Schematics for the DSK bull Datasheets Datasheets related to the DSK bull TroubleshootingSupport Solutions for problems you might encounter
TMS320VC5416 KEY FEATURES
bull 128Kwords memory high-speed internal memory for maximum performance
bull On-chip PLL generates processor clock rate from slower external clock reference
bull Timer generates periodic timer events as a function of the processor clock
Used by DSPBIOS to create time slices for multitasking
bull DMA Controller 6 channel direct memory access controller for high speed data transfers without intervention from the DSP
bull 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 IOs
bull 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
Cranes Software International Ltd TI - Solutions
6
INSTALLATION DSK HARDWARE INSTALLATION
bull Shut down and power off the PC bull Connect the supplied USB port cable to the board bull Connect the other end of the cable to the USB port of PC
Note If you plan to install a Microphone speaker or Signal generatorCRO these must be plugged in properly before you connect power to the DSK
bull Plug the power cable into the board bull Plug the other end of the power cable into a power outlet bull The user LEDs should flash several times to indicate board is operational bull 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)
bull Install the CCS software for C5416 DSK Troubleshooting DSK Connectivity If Code Composer Studio IDE fails to configure your port correctly perform the following steps
bull 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
bull The below Screen will appear bull Select StartSelect 5416 DSK Diagnostic Utility Icon from Desktop bull The Screen Look like as below bull Select Start Option bull Utility Program will test the board bull After testing Diagnostic Status you will get PASS
Cranes Software International Ltd TI - Solutions
7
If the board still fails to detect Go to CMOS setup Enable the USB Port Option (The required Device drivers will load along with CCS Installation)
Cranes Software International Ltd TI - Solutions
8
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The System requirements for the Host Machine are
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU with USB Ports
bull 600MB of free hard disk space bull 64MB of RAM bull SVGA (800 x 600 ) display bull Internet Explorer (40 or later) or bull Netscape Navigator (47 or later) bull Local CD-ROM drive
bull 500MHz or Higher Pentium Compatible CPU with USB Ports
bull 128MB RAM
bull 16bit Color
Supported Operating Systems
bull Windowsreg 98 bull Windows NTreg 40 Service Pack 4 or higher bull Windowsreg 2000 Service Pack 1 bull Windowsreg Me bull Windowsreg XP
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
bull Insert the installation CD into the CD-ROM drive
An install screen appears if not goes to the windows Explorer and run setupexe
bull Choose the option to install Code Composer Sutido
If you already have C6000 CC Studio IDE installed on your PC do not install DSK software CC Studio IDE full tools supports the DSK platform
bull Respond to the dialog boxes as the installation program runs
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
4
DSK Board Features
Feature Details TMS320VC5416 DSP 160MHz fixed point 128Kwords internal RAM CPLD Programmable glue logic External SRAM 64Kwords 16-bit interface External Flash 256Kwords 16-bit interface PCM3002 Codec Stereo 6KHz 48KHz sample rate 16 or 20
bit samples mic line-in line-out and speaker jacks
4 User LEDs Writable through CPLD 4 User DIP Switches Readable through CPLD 4 Jumpers Selects power-on configuration and boot
modes Daughter card Expansion Interface
Allows user to enhance functionality with add-on daughter cards
HPI Expansion Interface Allows high speed communication with another DSP
Embedded JTAG Emulator
Provides high speed JTAG debug through widely accepted USB host interface
Cranes Software International Ltd TI - Solutions
5
Key Features of the C5416 DSK
bull C5416 DSK TutorialQuick overview of the DSK and its contents getting started with Code Composer
bull Hardware Detailed descriptions of the C5416 DSK hardware components bull Board Support Library Functions that provide an easy interface to DSK
hardware bull Examples Example code that runs on the DSK bull Diagnostic Utility Host based diagnostics bull Schematics Schematics for the DSK bull Datasheets Datasheets related to the DSK bull TroubleshootingSupport Solutions for problems you might encounter
TMS320VC5416 KEY FEATURES
bull 128Kwords memory high-speed internal memory for maximum performance
bull On-chip PLL generates processor clock rate from slower external clock reference
bull Timer generates periodic timer events as a function of the processor clock
Used by DSPBIOS to create time slices for multitasking
bull DMA Controller 6 channel direct memory access controller for high speed data transfers without intervention from the DSP
bull 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 IOs
bull 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
Cranes Software International Ltd TI - Solutions
6
INSTALLATION DSK HARDWARE INSTALLATION
bull Shut down and power off the PC bull Connect the supplied USB port cable to the board bull Connect the other end of the cable to the USB port of PC
Note If you plan to install a Microphone speaker or Signal generatorCRO these must be plugged in properly before you connect power to the DSK
bull Plug the power cable into the board bull Plug the other end of the power cable into a power outlet bull The user LEDs should flash several times to indicate board is operational bull 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)
bull Install the CCS software for C5416 DSK Troubleshooting DSK Connectivity If Code Composer Studio IDE fails to configure your port correctly perform the following steps
bull 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
bull The below Screen will appear bull Select StartSelect 5416 DSK Diagnostic Utility Icon from Desktop bull The Screen Look like as below bull Select Start Option bull Utility Program will test the board bull After testing Diagnostic Status you will get PASS
Cranes Software International Ltd TI - Solutions
7
If the board still fails to detect Go to CMOS setup Enable the USB Port Option (The required Device drivers will load along with CCS Installation)
Cranes Software International Ltd TI - Solutions
8
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The System requirements for the Host Machine are
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU with USB Ports
bull 600MB of free hard disk space bull 64MB of RAM bull SVGA (800 x 600 ) display bull Internet Explorer (40 or later) or bull Netscape Navigator (47 or later) bull Local CD-ROM drive
bull 500MHz or Higher Pentium Compatible CPU with USB Ports
bull 128MB RAM
bull 16bit Color
Supported Operating Systems
bull Windowsreg 98 bull Windows NTreg 40 Service Pack 4 or higher bull Windowsreg 2000 Service Pack 1 bull Windowsreg Me bull Windowsreg XP
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
bull Insert the installation CD into the CD-ROM drive
An install screen appears if not goes to the windows Explorer and run setupexe
bull Choose the option to install Code Composer Sutido
If you already have C6000 CC Studio IDE installed on your PC do not install DSK software CC Studio IDE full tools supports the DSK platform
bull Respond to the dialog boxes as the installation program runs
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
5
Key Features of the C5416 DSK
bull C5416 DSK TutorialQuick overview of the DSK and its contents getting started with Code Composer
bull Hardware Detailed descriptions of the C5416 DSK hardware components bull Board Support Library Functions that provide an easy interface to DSK
hardware bull Examples Example code that runs on the DSK bull Diagnostic Utility Host based diagnostics bull Schematics Schematics for the DSK bull Datasheets Datasheets related to the DSK bull TroubleshootingSupport Solutions for problems you might encounter
TMS320VC5416 KEY FEATURES
bull 128Kwords memory high-speed internal memory for maximum performance
bull On-chip PLL generates processor clock rate from slower external clock reference
bull Timer generates periodic timer events as a function of the processor clock
Used by DSPBIOS to create time slices for multitasking
bull DMA Controller 6 channel direct memory access controller for high speed data transfers without intervention from the DSP
bull 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 IOs
bull 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
Cranes Software International Ltd TI - Solutions
6
INSTALLATION DSK HARDWARE INSTALLATION
bull Shut down and power off the PC bull Connect the supplied USB port cable to the board bull Connect the other end of the cable to the USB port of PC
Note If you plan to install a Microphone speaker or Signal generatorCRO these must be plugged in properly before you connect power to the DSK
bull Plug the power cable into the board bull Plug the other end of the power cable into a power outlet bull The user LEDs should flash several times to indicate board is operational bull 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)
bull Install the CCS software for C5416 DSK Troubleshooting DSK Connectivity If Code Composer Studio IDE fails to configure your port correctly perform the following steps
bull 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
bull The below Screen will appear bull Select StartSelect 5416 DSK Diagnostic Utility Icon from Desktop bull The Screen Look like as below bull Select Start Option bull Utility Program will test the board bull After testing Diagnostic Status you will get PASS
Cranes Software International Ltd TI - Solutions
7
If the board still fails to detect Go to CMOS setup Enable the USB Port Option (The required Device drivers will load along with CCS Installation)
Cranes Software International Ltd TI - Solutions
8
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The System requirements for the Host Machine are
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU with USB Ports
bull 600MB of free hard disk space bull 64MB of RAM bull SVGA (800 x 600 ) display bull Internet Explorer (40 or later) or bull Netscape Navigator (47 or later) bull Local CD-ROM drive
bull 500MHz or Higher Pentium Compatible CPU with USB Ports
bull 128MB RAM
bull 16bit Color
Supported Operating Systems
bull Windowsreg 98 bull Windows NTreg 40 Service Pack 4 or higher bull Windowsreg 2000 Service Pack 1 bull Windowsreg Me bull Windowsreg XP
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
bull Insert the installation CD into the CD-ROM drive
An install screen appears if not goes to the windows Explorer and run setupexe
bull Choose the option to install Code Composer Sutido
If you already have C6000 CC Studio IDE installed on your PC do not install DSK software CC Studio IDE full tools supports the DSK platform
bull Respond to the dialog boxes as the installation program runs
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
6
INSTALLATION DSK HARDWARE INSTALLATION
bull Shut down and power off the PC bull Connect the supplied USB port cable to the board bull Connect the other end of the cable to the USB port of PC
Note If you plan to install a Microphone speaker or Signal generatorCRO these must be plugged in properly before you connect power to the DSK
bull Plug the power cable into the board bull Plug the other end of the power cable into a power outlet bull The user LEDs should flash several times to indicate board is operational bull 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)
bull Install the CCS software for C5416 DSK Troubleshooting DSK Connectivity If Code Composer Studio IDE fails to configure your port correctly perform the following steps
bull 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
bull The below Screen will appear bull Select StartSelect 5416 DSK Diagnostic Utility Icon from Desktop bull The Screen Look like as below bull Select Start Option bull Utility Program will test the board bull After testing Diagnostic Status you will get PASS
Cranes Software International Ltd TI - Solutions
7
If the board still fails to detect Go to CMOS setup Enable the USB Port Option (The required Device drivers will load along with CCS Installation)
Cranes Software International Ltd TI - Solutions
8
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The System requirements for the Host Machine are
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU with USB Ports
bull 600MB of free hard disk space bull 64MB of RAM bull SVGA (800 x 600 ) display bull Internet Explorer (40 or later) or bull Netscape Navigator (47 or later) bull Local CD-ROM drive
bull 500MHz or Higher Pentium Compatible CPU with USB Ports
bull 128MB RAM
bull 16bit Color
Supported Operating Systems
bull Windowsreg 98 bull Windows NTreg 40 Service Pack 4 or higher bull Windowsreg 2000 Service Pack 1 bull Windowsreg Me bull Windowsreg XP
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
bull Insert the installation CD into the CD-ROM drive
An install screen appears if not goes to the windows Explorer and run setupexe
bull Choose the option to install Code Composer Sutido
If you already have C6000 CC Studio IDE installed on your PC do not install DSK software CC Studio IDE full tools supports the DSK platform
bull Respond to the dialog boxes as the installation program runs
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
7
If the board still fails to detect Go to CMOS setup Enable the USB Port Option (The required Device drivers will load along with CCS Installation)
Cranes Software International Ltd TI - Solutions
8
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The System requirements for the Host Machine are
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU with USB Ports
bull 600MB of free hard disk space bull 64MB of RAM bull SVGA (800 x 600 ) display bull Internet Explorer (40 or later) or bull Netscape Navigator (47 or later) bull Local CD-ROM drive
bull 500MHz or Higher Pentium Compatible CPU with USB Ports
bull 128MB RAM
bull 16bit Color
Supported Operating Systems
bull Windowsreg 98 bull Windows NTreg 40 Service Pack 4 or higher bull Windowsreg 2000 Service Pack 1 bull Windowsreg Me bull Windowsreg XP
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
bull Insert the installation CD into the CD-ROM drive
An install screen appears if not goes to the windows Explorer and run setupexe
bull Choose the option to install Code Composer Sutido
If you already have C6000 CC Studio IDE installed on your PC do not install DSK software CC Studio IDE full tools supports the DSK platform
bull Respond to the dialog boxes as the installation program runs
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
8
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The System requirements for the Host Machine are
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU with USB Ports
bull 600MB of free hard disk space bull 64MB of RAM bull SVGA (800 x 600 ) display bull Internet Explorer (40 or later) or bull Netscape Navigator (47 or later) bull Local CD-ROM drive
bull 500MHz or Higher Pentium Compatible CPU with USB Ports
bull 128MB RAM
bull 16bit Color
Supported Operating Systems
bull Windowsreg 98 bull Windows NTreg 40 Service Pack 4 or higher bull Windowsreg 2000 Service Pack 1 bull Windowsreg Me bull Windowsreg XP
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
bull Insert the installation CD into the CD-ROM drive
An install screen appears if not goes to the windows Explorer and run setupexe
bull Choose the option to install Code Composer Sutido
If you already have C6000 CC Studio IDE installed on your PC do not install DSK software CC Studio IDE full tools supports the DSK platform
bull Respond to the dialog boxes as the installation program runs
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
9
INTRODUCTION TO CODE COMPOSER STUDIO Code Composer is the DSP industrys 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 injectionextraction of data signals via file IO multi-processor debugging automated testing and customization via a C-interpretive scripting language and much more CODE COMPOSER FEATURES INCLUDE
bull IDE bull Debug IDE bull Advanced watch windows bull Integrated editor bull File IO Probe Points and graphical algorithm scope probes bull Advanced graphical signal analysis bull Interactive profiling bull Automated testing and customization via scripting bull Visual project management system bull Compile in the background while editing and debugging bull Multi-processor debugging bull Help on the target DSP
Note
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
10
Troubleshooting
Problem Solution CCS does not support target error recovery for RTDXBIOS applications
To re-enable RTDXBIOS following target error recovery you must exit CCS and restart CCS Otherwise RTDXBIOS data transmission tofrom the host is disabled RTDXBIOS will be target error recovery compliant in a later version of CCS
Cant load program right after building it
When you build your project you must wait until the build process is complete before attempting to load the code into the target There is no internal safety check for this in addition any time your want to re-run a program you cannot simply hit run again Select the restart option or re-load the program to start again from the beginning
Nothing happens when you click on a help file link to the schematics or datasheets
Documents like the schematics and datasheets are stored in Adobe PDF format For the link to work you must have Adobes acrobat reader installed
When you halt a program Code Composer will sometimes display a dialog box saying that it cannot find a source code or library file
When you halt a program Code Composer attempts to display the source code associated with the current program counter If the program stops in a system library like the runtime library DSPBIOS or the board support library it will not be able to find the source code for debug You can either find the source code if you want to debug it or click on the checkbox to ignore messages like this
Installation and Startup
Problem Solution When you connect your DSK through USB for the first time on a Windows XP PC the new hardware found wizard may not find the driver for about 15 minutes
The drivers are on the CD but Windows XP does some network accesses before it even checks the CD Some firewall configurations can cause the network accesses to fail and Windows will wait for a timeout period before it checks the CD If you wait long enough the process will complete normally To avoid the wait temporarily unplug the network cable for this part of the install process
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
11
Problem Solution I cant verify that the driver install was successful
Go into your device manager and look for the SD USB Based Debug Tools hardware class There should be one Spectrum Digital TMS320C5416 DSK installed in the class
What are the factory default jumper settings for JP4
CLKMD1-Off CLKMD2-On CLKMD3-On MPMC-On
Code Composer wont launch if I power cycle the DSK and try to start CCS really quickly
Windows needs time to recognize a USB device and get it initializaed The Busy LED (DS201) above the USB connector illuminates when the DSK is powered on during this period Do not launch Code Composer until the led turns off
Both Code Composer and the Diagnostic Utility run fine in Administrator mode but not in user mode
The solution is to relax the user mode privilege restrictions Please see the section at the bottom of this page entitled Relaxing Administrator Mode Restrictions
Relaxing Administrator Mode Privileges On Windows NT 2000 and XP there is a large distinction between the privileges of programs running in administrator mode vs user mode The 5416 DSK must be installed in Administrator mode but most users will run the program in user mode The exact problem depends on your system configuration but stems from two general problems
1 The program cannot modify certain pieces of the registry because were marked as read-only in Administrator mode during the install
2 The program cannot write to or execute a file because it was created in
Administrator mode and User mode programs usually have read-only access to files created by the Administrator If you build a project in Administrator mode you will not be able to rebuild it in user mode because CCS will not be able to overwrite the temporary files
The exact access restriction depends on your system configuration and the operations performed so no one thing will fix all problems The following is a list of things you can do to relax the Administrator mode privileges so the user program can get full access to Code Composer related resources
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
12
1 Allow user mode access to Code Composers registry settings
bull Log in as an Administrator bull Choose Start agrave Run from the Windows 2000 Start menu bull Type regedt32 (not regedit) to bring up the Registry Editor bull From the Window menu select HKEY_LOCAL_MACHINE on the Local Machine bull Expand the SOFTWARE key bull Locate and select the sub-key GODSP bull With GODSP selected choose Security Permissions from the menu bull In the upper half of the Permissions for GODSP window there will be a list of all
the users and groups that access the key bull Select the Users group bull In the Permissions box locate the item Full Control and check the corresponding
box under Allow bull Repeat steps e through i for HKLMSoftwareTexas Instruments
2 On Windows 2000 and XP allow users that will be using the Code Composer Studio product access via a Power User account Power users are automatically granted more privileges than a normal user 3 Give users full access to all files in the Code Composer program tree
bull Log in as Administrator bull Open Windows Explorer and find the location of your install (usually cti) bull Right click on the directory name in the Folders pane on the left Select the
Properties menu option bull You will see a set of tabs that contain settings for that folder Select the Security
tab to see the privileges associated with the folder bull Click on the Users group (or any other group you would like to change privileges
for) to see the group specific privileges bull Click on the Full Control box and hit Apply The new settings will be applied to all
files and subdirectories of the Code Composer install directory
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
13
Error Messages
Error Message Problem Failed Requires Win98 WinME Win2k or WinXP
Your Windows operating system does not support USB
Failed USB device is NOT enumerated or plugged in
Windows cannot find the C5416 DSK in the list of enumerated USB devices DSK may not be powered or the USB cable may not be plugged in
Failed Load Usb Application Loader could not download the C5416 DSK emulation app to the DSK The on-chip emulation controller may be in a bad state Cycle power on the DSK
Failed USB channel in use by another app
Another application is using the C5416 DSK USB communication channel Only one application is allowed to talk to the C5416 DSK over the USB channel You may have to check for active applications using Windows task manager if nothing seems apparent This is a status message which indicates that the C5416 DSK emulation application has been loaded The loader will maintain the message until the C5416 DSK re-enumerates
Waiting for USB Enumeration
On Windows 2000XP the Waiting for USB Enumeration will generally be active for less then 10 seconds On Windows 98ME the enumeration process may take as long as 40 seconds Do NOT select the OK button unless you have let the specified time expire If you select the OK button while waiting enumeration the application will terminate and return control back to CCS which will probably fail and give the normal target error dialog
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
14
Procedure to work on Code Composer Studio
To create the New Project Project New (File Name pjt Eg Vectorspjt)
To Create a Source file File New Type the code (Save amp give file name Eg sumc) To Add Source files to Project Project Add files to Project sumc To Add rtslib file amp hellocmd Project Add files to Project rts_extlib Library files rts_extlib(Path c tic5400cgtoolslibrts_extlib) Note Select Object amp Library in(ol) in Type of files
Project Add files to Project hellocmd CMD file Which is common for all non real time programs (Path ctitutorialdsk5416hello1hellocmd) Note Select Linker Command file(cmd) in Type of files To Enable ndashmf option
Project Build Options Advanced (in Category) Use Far Calls (- mf) (C548 and higher) Compile To Compile Project Compile To Rebuild Project rebuild Which will create the final out executable file(Eg Vectorsout) Procedure to Load and Run program
Load the program to DSK File Load program Vectors out
To Execute project Debug Run
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
15
sumc includeltstdiohgt main() int i=0 i++ printf(di) 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 Vectors out file onto the target
3 Go to view and select Watch window
4 Debug Run
5 Execution should halt at break point
6 Now press F10 See the changes happening in the watch window
7 Similarly go to view amp select CPU registers to view the changes happening in CPU registers
8 Repeat steps 2 to 6
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
16
LINEAR CONVOLUTION
To Verify Linear Convolution 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 lsquoCrsquo 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 amp k should be a multiple of 4 If n amp k are not multiples of 4 pad with zerorsquos to make
multiples of 4 r= n+k-1 Size of output sequence = 4+4-1 = 7 r= 0 1 2 3 4 5 6 n= 0 x[0]h[0] x[0]h[1] x[0]h[2] x[0]h[3] 1 x[1]h[0] x[1]h[1] x[1]h[2] x[1]h[3] 2 x[2]h[0] x[2]h[1] x[2]h[2] x[2]h[3] 3 x[3]h[0] x[3]h[1] x[3]h[2] x[3]h[3] Output y[r] = 1 4 10 20 25 24 16 NOTE At the end of input sequences pad lsquonrsquo and lsquokrsquo no of zerorsquos
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
17
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION lconvc includeltstdiohgt main() int m=4 Lenght of ip samples sequence int n=4 Lenght of impulse response Co-efficients int i=0j int x[10]=12340000 Input Signal Samples int h[10]=12340000 Impulse Response Co-efficients At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos int y[10] for(i=0iltm+n-1i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0iltm+n-1i++) printf(dny[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(ctimyprojects) with name lconvpjt
Add the source file lconvc to the project using Projectadd files to project pull down menu
Add the linker command file hellocmd (Path ctitutorialdsk5416hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic5400cgtoolslibrts_extlib)
Enable mf option 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(lconvout) in program memory of DSP chip using the File-load program pull down menu
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
18
To View output graphically Select view graph time and frequency
Configure the graphical window as shown below
INPUT x[n] = 1 2 3 40000 h[k] = 1 2 3 40000 OUTPUT
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
19
CIRCULAR CONVOLUTION
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[minusm]
Subfigure 11 Subfigure 12
Step 2 Spin h[minusm] n times Anti Clock Wise (counter-clockwise) to get h[n-m] (ie Simply rotate the sequence h[n] clockwise by n steps)
Figure 2 Step 2
Step 3 Pointwise multiply the f[m] wheel and the h[nminusm] wheel sum=y[n]
Step 4 Repeat for all 0lenleNminus1
Example 1 Convolve (n = 4)
Subfigure 31 Subfigure 32
Figure 3 Two discrete-time signals to be convolved
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
20
bull h[minusm] =
Figure 4
Multiply f[m] and sum to yield y[0] =3
bull h[1minusm]
Figure 5
Multiply f[m] and sum to yield y[1] =5
bull h[2minusm]
Figure 6
Multiply f[m] and sum to yield y[2] =3
bull h[3minusm]
Figure 7
Multiply f[m] and sum to yield y[3] =1
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
21
Program to Implement Circular Convolution includeltstdiohgt int mnx[30]h[30]y[30]ijtemp[30]kx2[30]a[30] void main() printf( enter the length of the first sequencen) scanf(dampm) printf( enter the length of the second sequencen) scanf(dampn) printf( enter the first sequencen) for(i=0iltmi++) scanf(dampx[i]) printf( enter the second sequencen) for(j=0jltnj++) scanf(damph[j]) if(m-n=0) If length of both sequences are not equal if(mgtn) Pad the smaller sequence with zero for(i=niltmi++) h[i]=0 n=m for(i=miltni++) x[i]=0 m=n y[0]=0 a[0]=h[0] for(j=1jltnj++) folding h(n) to h(-n) a[j]=h[n-j] Circular convolution for(i=0iltni++) y[0]+=x[i]a[i] for(k=1kltnk++) y[k]=0 circular shift for(j=1jltnj++) x2[j]=a[j-1] x2[0]=a[n-1] for(i=0iltni++) a[i]=x2[i] y[k]+=x[i]x2[i] displaying the result printf( the circular convolution isn) for(i=0iltni++) printf(d ty[i])
IN PUT Eg x[4]=3 2 1 0 h[4]=1 1 0 0 OUT PUT y[4]=3 5 3 1
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
22
TMS320C5416 DSP Multi Channel Buffered Serial Port
[McBSP] Configuration Using Chip Support Library 10 Unit Objective To configure the Multi Channel Buffered Serial Port for a talk through program using the chip support library 20 Prerequisites TMS320C5416 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer McBSP topic under TMS320C5416 DSP Module 40 Procedure
bull All the Real time implementations covered in the Implementations module follow McBSP Configuration using chip support library
bull The Chip Support Library (CSL) is a collection of functions macros and symbols used to configure and control on-chip peripherals
bull The goal is peripheral ease of use shortened development time portability hardware abstraction and some level of standardization and compatibility among TI devices
bull CSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code McBSP
Steps 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 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name XXXXpjt 6 From the File Menu new DSPBIOS Configuration select
dsk5416cdb and save it as YYYYcdb and add it to the current project 7 Double click on the YYYYcdbrdquo from the project explorer and double click on the
chip support library explorer 8 Double click on the MCBSP under the chip support library where you can see
MCBSP Configuration Manager and MCBSP Resource Manager 9 Right click on the MCBSP Configuration Manager and select Insert mcbspCfg
where you can see ldquomcbspCfg0rdquo appearing under MCBSP Configuration Manager
10 Right click on mcbspCfg0 and select properties where mcbspCfg0 properties window appears
11 Under General property set Breakpoint Emulation to Do Not Stop 12 Under Transmit modes property set clock polarity to Falling Edge
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
23
13 Under Transmit Lengths property set Word Length Phase1 to 32-bits and set WordsFrame phase1 to 2
14 Under Receive modes property set clock polarity to Rising Edge 15 Under Receive Multichannel property set Rx Channel Enable to All 128
Channels 16 Under Transmit Multichannel property set Tx Channel Enable to All 128
Channels 17 Under the Receive Lengths property set Word Length Phase1 to 32-bits and
set WordsFrame phase1 to 2 18 Under the Sample-Rate Gen property set Generator Clock Source to BCLKR
pin Set Frame Width to 32 and Frame period to 64 19 Select Apply and click OK 20 Select McBSP2 under the MCBSP Resource Manager 21 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 OK
22 Add the generated YYYYcfgcmd file to the current project 23 Add the given mcbsp_ioc file to the current project which has the main function
and calls all the other necessary routines 24 View the contents of the generated file YYYYcfg_cc and copy the include header
file lsquoYYYYcfghrsquo to the mcbsp_ioc file 25 Add the library file dsk5416flib from the location
CtiC5400dsk5416libdsk5416flib to the current project 26 Select projectbuild optionsCompiler Advance and enable the use Far
calls option 27 Build Load and Run the program 28 You can notice the input signal of 500 Hz appearing on the CRO verifying the
McBSP configuration 29 You can also pass an audio input and hear the output signal through the speakers 30 You can also vary the sampling frequency using the DSK5416_PCM3002_setFreq
Function in the mcbsp_ioc file and repeat the above steps
50 Conclusion The Multichannel Buffered Serial Port is successfully configured using the chip support library and verified
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
24
Mcbsp_ioc include YYYYcfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt short left_inputright_input DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_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))
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
25
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 frequencyselective 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 amp 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
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
26
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C5416 DSP Starter Kit (DSK) Oscilloscope and Function generator 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 MATLABA direct form I implementation approach is taken bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
27
FLOWCHART FOR IIR IMPLEMENTATION F1 Flowchart for implementing IIR filter
Stop
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-a2b0-b2 with MATLAB filter coefficients
output = x[0]b0+x[-1]b1+ x[-2]b2 - y[-1]a1 - y[-2]a2
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do y[-3] = y[-2]y[-2]=y[-1] and Y[-1] = output x[-3] = x[-2] x[-2]=x[-1] x[-1]=x[0] Poll for ready bit
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
28
MATLAB PROGRAM TO GENRATE FILTER CO-EFFICIENTS IIR Low pass Butterworth and Chebyshev filters sampling rate - 24000 order = 2 cf=[250012000800012000160012000] cutoff frequency - 2500 [num_bw1den_bw1]=butter(ordercf(1)) [num_cb1den_cb1]=cheby1(order3cf(1)) cutoff frequency - 8000 [num_bw2den_bw2]=butter(ordercf(2)) [num_cb2den_cb2]=cheby1(order3cf(2)) fid=fopen(IIR_LP_BWtxtwt) fprintf(fidtt-----------Pass band range 0-2500Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidn float num_bw1[9]=) fprintf(fidfffffnffffnnum_bw1) fprintf(fidnfloat den_bw1[9]=) fprintf(fidfffffnffffnden_bw1) fprintf(fidnnntt-----------Pass band range 0-8000Hz----------n) fprintf(fidtt-----------Magnitude response Monotonic-----nn) fprintf(fidnfloat num_bw2[9]=) fprintf(fidfffffnffffnnum_bw2) fprintf(fidnfloat den_bw2[9]=) fprintf(fidfffffnffffnden_bw2) fclose(fid) winopen(IIR_LP_BWtxt) fid=fopen(IIR_LP_CHEB Type1txtwt) fprintf(fidtt-----------Pass band range 2500Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB) -----nn) fprintf(fidnfloat num_cb1[9]=) fprintf(fidfffffnffffnnum_cb1) fprintf(fidnfloat den_cb1[9]=) fprintf(fidfffffnffffnden_cb1) fprintf(fidnnntt-----------Pass band range 8000Hz----------n) fprintf(fidtt-----------Magnitude response Rippled (3dB)-----nn) fprintf(fidnfloat num_cb2[9]=) fprintf(fidfffffnffffnnum_cb2) fprintf(fidnfloat den_cb2[9]=) fprintf(fidfffffnffffnden_cb2)
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
29
fclose(fid) winopen(IIR_LP_CHEB Type1txt) figure(1) [hw]=freqz(num_bw1den_bw1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb1den_cb1) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev Type-1) xlabel(Frequency in Hertz) ylabel(Magnitude in Decibels) title(Magnitude response of Low pass IIR filters (Fc=2500Hz)) figure(2) [hw]=freqz(num_bw2den_bw2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2) hold on [hw]=freqz(num_cb2den_cb2) w=(wmax(w))12000 plot(w20log10(abs(h))linewidth2colorr) grid on legend(ButterworthChebyshev 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
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0044408 1455 0005147 168 0354544 11617 B1 0088815 1455[B12] 0010295 168[B12] 0709088 11617[B12] B2 0044408 1455 0005147 168 0354544 11617 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1412427 -23140[A12] -1844881 -30225[A12] 0530009 8683[A12] A2 0663336 21735 0873965 28637 0473218 15506
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
30
IIR_BUTTERWORTH_LP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=800Hz Fc=8000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0072231 2366 0009526 312 0465153 15241 B1 0144462 2366[B12] 0019052 312[B12] 0930306 15241[B12] B2 0072231 2366 0009526 312 0465153 15241 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18179[A12] -1705552 -27943[A12] 0620204 10161[A12] A2 0398152 13046 0743655 24367 0240408 7877
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_CHEB_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0388513 12730 0282850 9268 0117279 3842 B1 -0777027 -12730[B12] -0565700 -9268[B12] -0234557 -3842[B12] B2 0388513 12730 0282850 9268 0117279 3842 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1118450 -18324[A12] -0451410 -7395[A12] 0754476 12360[A12] A2 0645091 21137 0560534 18367 0588691 19289
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values IIR_BUTTERWORTH_HP FILTER CO-EFFICIENTS
Fc=2500Hz Fc=4000Hz Fc=7000Hz Co-Efficients
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
Floating Point Values
Fixed Point Values(Q15)
B0 0626845 20539 0465153 15241 0220195 7215 B1 -1253691 -20539[B12] -0930306 -15241[B12] -0440389 -7215[B12] B2 0626845 20539 0465153 15241 0220195 7215 A0 1000000 32767 1000000 32767 1000000 32767 A1 -1109229 -18173[A12] -0620204 -10161[A12] 0307566 5039[A12 A2 0398152 13046 0240408 7877 0188345 6171
Note We have Multiplied Floating Point Values with 32767(215) to get Fixed Point Values
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
31
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output const signed int filter_Coeff[ ] = 484848 32767 -30949 29322 96969632767-3091829614 fc=600 18825918832767-2949827695fc=1000 11617116171161732767868315505fc=8000 14551455145532767-2314021735fc=2500 5058-5058505832767999515803fc=7000 -HP 22586-225862258632767-2096415649 20539-205392053932767-1817313046 15241-152411524132767-101617877 Select particular set of co-efficients different Cut-off frequencies from given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec24000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=IIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
32
signed int IIR_FILTER(const signed int h signed int x1) static signed int x[6] = 0 0 0 0 0 0 x(n) x(n-1) x(n-2) Must be static 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 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (long)h[0] x[0]) B0 x(n) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[1] x[1]) B12 x(n-1) temp += ( (long)h[2] x[2]) B2 x(n-2) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[4] y[1]) A12 y(n-1) temp -= ( (long)h[5] y[2]) A2 y(n-2) Divide temp by coefficients[A0] temp gtgt= 15 if ( temp gt 32767 ) temp = 32767 else if ( temp lt -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)temp1)
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
33
Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the given above C source file to the current project(remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
34
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
35
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 frequencyselective 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 amp 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 possess 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
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
36
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) DSP kit with TMS 320C5416 chip and driver software Oscilloscope and Function generator 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 bull Step 1 - Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL
bull Step 2 - Initialize the discrete time system that is specify the initial conditions Generally zero initial conditions are assumed
bull 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
bull Step 4 - Perform filter operation using above said difference equation and store
filter Output at a memory location bull Step 5 - Output the value to codec (left channel and right channel) and view the
output at Oscilloscope bull Step 6 - Go to step 3
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
37
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 = 30 Sampling Rate = 8000 samplessec Cut off Freq = 400 Hz
II Compute the cut-off frequency Wc
Eg Wc = 2pie fc Fs = 2pie 4008000 = 01pie III Compute the desired Impulse Response h d (n) using particular Window
Eg b_rect1=fir1(order Wc highboxcar(31))
IV Convolve input sequence with truncated Impulse Response x (n)h (n) USING MATLAB TO DETERMINE FILTER COEFFICIENTS Using FIR1 Function on Matlab B = FIR1(NWn) designs an Nth 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 lt Wn lt 10 with 10 corresponding to half the sample rate The filter B is real and has linear phase ie even symmetric coefficients obeying B(k) = B(N+2-k) k = 12N+1 If Wn is a two-element vector Wn = [W1 W2] FIR1 returns an order N bandpass filter with passband W1 lt W lt W2 B = FIR1(NWnhigh) designs a highpass filter B = FIR1(NWnstop) 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 lt W lt W1 W1 lt W lt W2 WN lt W lt 1 B = FIR1(NWnDC-1) makes the first band a passband B = FIR1(NWnDC-0) makes the first band a stopband For filters with a passband near Fs2 eg 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(NWnkaiser(N+14)) uses a Kaiser window with beta=4
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
38
B = FIR1(NWnhighchebwin(N+1R)) 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 eg B = FIR1(NWnnoscale) B = FIR1(NWnhighnoscale) B = FIR1(NWnwindnoscale) Matlab Program to generate lsquoFIR Filter-Low Passrsquo Coefficients using FIR1 FIR Low pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[50040001000400015004000] cf--gt contains set of cut-off frequencies[Wc ] cutoff frequency - 500 b_rect1=fir1(ordercf(1)boxcar(31)) Rectangular b_tri1=fir1(ordercf(1)bartlett(31)) Triangular b_kai1=fir1(ordercf(1)kaiser(318)) Kaisar [Where 8--gtBeta Co-efficient] cutoff frequency - 1000 b_rect2=fir1(ordercf(2)boxcar(31)) b_tri2=fir1(ordercf(2)bartlett(31)) b_kai2=fir1(ordercf(2)kaiser(318)) cutoff frequency - 1500 b_rect3=fir1(ordercf(3)boxcar(31)) b_tri3=fir1(ordercf(3)bartlett(31)) b_kai3=fir1(ordercf(3)kaiser(318)) fid=fopen(FIR_lowpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid)
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
39
fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 Matlab generated Coefficients for FIR Low Pass Kaiser filter
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter
Cutoff -500Hz float b_kai1[31]=-0000019-0000170-0000609-0001451-0002593-0003511-000315000000000007551002065500393830062306008649401080310122944 012827901229440108031008649400623060039383002065500075510000000 -0003150-0003511-0002593-0001451-0000609-0000170-0000019 Cutoff -1000Hz float b_kai2[31]=-0000035-0000234-00004540000000000193300048380005671 -0000000-0013596-0028462-002937000000000064504014886302213490249983 0221349014886300645040000000-0029370-0028462-0013596-00000000005671 000483800019330000000-0000454-0000234 -0000035 Cutoff -1500Hz float b_kai3[31]=-0000046-0000166000024600014140001046-0003421-0007410 000000000177640020126-0015895-0060710-0034909010526302892090374978 02892090105263-0034909-0060710-0015895002012600177640000000-0007410 -0003421000104600014140000246-0000166 -0000046
Cutoff -500Hz float b_rect1[31]=-0008982-0017782-0025020-0029339-0029569-0024895 -001497000000000019247004149100650530088016010842101244730134729 013825501347290124473010842100880160065053004149100192470000000 -0014970-0024895-0029569-0029339-0025020-0017782-0008982 Cutoff -1000Hz float b_rect2[31]=-0015752-0023869-00181760000000002148100334160026254-0000000-0033755-0055693-004725700000000078762016708002362860262448 0236286016708000787620000000-0047257-0055693-0033755-00000000026254 003341600214810000000-0018176-0023869-0015752 Cutoff -1500Hz float b_rect2[31]=-0020203-0016567000965600273350011411-0023194-0033672 000000000432930038657-0025105-0082004-0041842011597103030480386435 03030480115971-0041842-0082004-0025105003865700432930000000-0033672 -0023194001141100273350009656-0016567-0020203
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
40
T3 Matlab generated Coefficients for FIR Low Pass Triangular filter MATLAB Program to generate lsquoFIR Filter-High Passrsquo Coefficients using FIR1 FIR High pass filters using rectangular triangular and kaiser windows sampling rate - 8000 order = 30 cf=[4004000800400012004000] cf--gt contains set of cut-off frequencies[Wc] cutoff frequency - 400 b_rect1=fir1(ordercf(1)highboxcar(31)) b_tri1=fir1(ordercf(1)highbartlett(31)) b_kai1=fir1(ordercf(1)highkaiser(318)) Where Kaiser(318)--gt 8defines the value of beta cutoff frequency - 800 b_rect2=fir1(ordercf(2)highboxcar(31)) b_tri2=fir1(ordercf(2)highbartlett(31)) b_kai2=fir1(ordercf(2)highkaiser(318)) cutoff frequency - 1200 b_rect3=fir1(ordercf(3)highboxcar(31)) b_tri3=fir1(ordercf(3)highbartlett(31)) b_kai3=fir1(ordercf(3)highkaiser(318))
Cutoff -500Hz float b_tri1[31]=0000000-0001185-0003336-0005868-0007885-0008298-0005988 000000000102650024895004336800645450086737010787701257470138255 01257470107877008673700645450043368002489500102650000000-0005988 -0008298-0007885-0005868-0003336-00011850000000 Cutoff -1000Hz float b_tri2[31]=0000000-0001591-00024230000000000572800111390010502 -0000000-0018003-0033416-003150500000000063010014480202205340262448 0220534014480200630100000000-0031505-0033416-0018003-00000000010502 001113900057280000000-0002423-00015910000000 Cutoff -1500Hz float b_tri3[31]=0000000-0001104000128700054670003043-0007731-0013469 000000000230890023194-0016737-0060136-0033474010050802828440386435 02828440100508-0033474-0060136-0016737002319400230890000000-0013469 -0007731000304300054670001287-00011040000000
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
41
fid=fopen(FIR_highpass_rectangulartxtwt) fprintf(fidttttttsnCutoff -400Hz) fprintf(fidnfloat b_rect1[31]=) fprintf(fidffffffffffnb_rect1) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -800Hz) fprintf(fidnfloat b_rect2[31]=) fprintf(fidffffffffffnb_rect2) fseek(fid-10) fprintf(fid) fprintf(fidnnnn) fprintf(fidttttttsnCutoff -1200Hz) fprintf(fidnfloat b_rect3[31]=) fprintf(fidffffffffffnb_rect3) fseek(fid-10) fprintf(fid) fclose(fid) winopen(FIR_highpass_rectangulartxt) T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter bull bull
Cutoff -400Hz float b_kai1[31]=00000500000223000052000008310000845-0000000-0002478 -0007437-0015556-0027071-0041538-0057742-0073805-0087505-0096739 0899998-0096739-0087505-0073805-0057742-0041538-0027071-0015556 -0007437-0002478-000000000008450000831000052000002230000050 Cutoff -800Hz float b_kai2[31]=0000000-0000138-0000611-0001345-0001607-00000000004714 0012033001828700167310000000-0035687-0086763-0141588-01840110800005 -0184011-0141588-0086763-003568700000000016731001828700120330004714 -0000000-0001607-0001345-0000611-00001380000000 Cutoff -1200Hz float b_kai3[31]=-0000050-0000138000019800013450002212-0000000-0006489 -0012033-0005942001673100415390035687-0028191-0141589-02532700700008 -0253270-0141589-0028191003568700415390016731-0005942-0012033-0006489 -0000000000221200013450000198-0000138-0000050
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
42
T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
Cutoff -400Hz float b_rect1[31]=00216650022076002022400159180009129-0000000-0011158 -0023877-0037558-0051511-0064994-0077266-0087636-0095507-1004220918834 -0100422-0095507-0087636-0077266-0064994-0051511-0037558-0023877 -0011158-000000000091290015918002022400220760021665 Cutoff -800Hz float b_rect2[31]=0000000-0013457-0023448-0025402-0017127-00000000020933 0038103004354700313990000000-0047098-0101609-0152414-01883940805541 -0188394-0152414-0101609-004709800000000031399004354700381030020933 -0000000-0017127-0025402-0023448-00134570000000 Cutoff -1200Hz float b_rect3[31]=-0020798-0013098000741600247250022944-0000000-0028043 -0037087-0013772003056200623930045842-0032134-0148349-02523860686050 -0252386-0148349-0032134004584200623930030562-0013772-0037087-0028043 -0000000002294400247250007416-0013098-0020798
Cutoff -400Hz float b_tri1[31]=00000000001445000264800031270002391-0000000-0004383 -0010943-0019672-0030353-0042554-0055647-0068853-0081290-0092048 0902380-0092048-0081290-0068853-0055647-0042554-0030353-0019672 -0010943-0004383-000000000023910003127000264800014450000000 Cutoff -800Hz float b_tri2[31]=0000000-0000897-0003126-0005080-0004567-00000000008373 0017782002322500188390000000-0034539-0081287-0132092-01758340805541 -0175834-0132092-0081287-003453900000000018839002322500177820008373 -0000000-0004567-0005080-0003126-00008970000000 Cutoff -1200Hz float b_tri3[31]=0000000-0000901000102100051050006317-0000000-0011581 -0017868-0007583001893100429440034707-0026541-0132736-02431960708287 -0243196-0132736-0026541003470700429440018931-0007583-0017868-0011581 -0000000000631700051050001021-00009010000000
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
43
FLOWCHART FOR FIR IMPLEMENTATION F1 Flowchart for implementing FIR filter
Stop
Initialize the DSP Board
Set initial conditions of discrete time system by making x[0]-x[25] equal to zeros and b0-b25 with matlab filter coefficients
output = x[0]b0+x[-1]b1+x[-2]b2+hellip +x[-25]b25
Take a new input and store it in x[0]
Start
Write lsquooutputrsquo to analog io
Do x[-25]=x[-24]helliphelliphellip x[-2]=x[-1]x[-1]=x[0] Poll the ready bit when asserted proceed
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
44
lsquoCrsquo Program to implement FIR filter include filtercfgh include ltdsk5416hgt include ltdsk5416_pcm3002hgt Int16 left_input Int16 left_output Int16 right_input Int16 right_output static short in_buffer[100] float filter_Coeff[] =-0000050-0000138000019800013450002212-0000000-0006489-0012033-0005942001673100415390035687-0028191-0141589-02532700700008-0253270 -0141589-0028191003568700415390016731-0005942-0012033-0006489-0000000 0002212 00013450000198-0000138-0000050 Select particular set of co-efficients different Cut-off frequencies from above given Tables DSK5416_PCM3002_Config setup = 0x1ff Set-Up Reg 0 - Left channel DAC attenuation 0x1ff Set-Up Reg 1 - Right channel DAC attenuation 0x0 Set-Up Reg 2 - Various ctl eg power-down modes 0x0 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 ampsetup) Set codec frequency DSK5416_PCM3002_setFreq(hCodec8000) 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 ampleft_input)) while(DSK5416_PCM3002_read16(hCodec ampright_input)) left_output=FIR_FILTER(ampfilter_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))
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
45
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=31igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt32i++) output = output + h[i] in_buffer[i] return(output) Procedure
Switch on the DSP board Open the Code Composer Studio Create a new project
Project New (File Name pjt Eg Filterpjt) Initialize the McBSP the DSP board and the on board codec
Kindly refer the Topic Configuration of 5416 McBSP using CSL Add the above given C source file to the current project (remove mcbsp_ioc
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 using F5 Observe the waveform that appears on the CRO screen
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
46
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
47
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 eg
Note that
1 It is periodic (ie it goes round and round the circle ) 2 That the vectors are symmetric 3 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 numbers Even a simple eight sample signal would require 49 complex
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
48
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 20000000 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 DFTs 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 FFTs 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 ZEROs until the number of samples is the nearest power of two
eg 500 samples are padded to 512 (2^9)
2 Bit reverse the input sequence
eg 3 = 011 goes to 110 = 6
3 Compute (N 2) two sample DFTs from the shuffled inputs
See Shuffled Inputs
4 Compute (N 4) four sample DFTs from the two sample DFTs
See Shuffled Inputs
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
49
6 Until the all the samples combine into one N-sample DFT
Shuffled Inputs
The process of decimating the signal in the time domain has caused the INPUT samples to be re-ordered 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
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
50
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 bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Take sampled data and apply FFT algorithm
bull Step 4 ndash Use Graph option to view the Input amp Output
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
51
C PROGRAM TO IMPLEMENT FFT Mainc (fft 256c) include ltmathhgt define PTS 64 of points for FFT define PI 314159265358979 typedef struct float realimag 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 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20)) Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants for (i = 0 i lt PTS i++) swap buffers iobuffer[i] = sin(2PI10i640)10- gt freq
64 -gt sampling freq samples[i]real=00 samples[i]imag=00
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
52
for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main fftc define PTS 64 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg index of upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant i = 1 log(base2) of N points= of stages
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
53
do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++) bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
54
(Y[i])real = temp1real (Y[i])imag = temp1imag return
Input
Output
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
55
HOW TO PROCEED
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(ctimyprojects) with name FFTpjtrdquo
Add the source files FFT256c and ldquoFFTCrdquo in the project using Projectadd files to project pull down menu
Add the linker command file hellocmd Add the rts file ldquorts_extlibrdquo
Set lsquo-mf lsquooption
Compile the program using the Project-compile 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
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
56
POWER SPECTRUM 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 auto-correlation function and PSD are a Fourier transform pair (Another estimation method called period gram uses sampled FFT to compute the PSD) Eg For a process x(n) correlation is defined as Power Spectral Density is a Fourier transform of the auto correlation
( ) ( ) ( ) R E x n x nτ τ= +
1
1l i m ( ) ( )N
N nx n x n
Nτ
rarr infin =
= +sum
1
1
1
ˆ( ) ( ( )) lim ( )
1( ) ( ( )) ( )2
Nj
N N
j
S F T R R e
R F T S S e d
ω τ
τ
ω τ
ω τ τ
τ ω ω ωπ
minusminus
rarr infin = minus +
minus
= =
= =
sum
int
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
57
ALGORITHM TO IMPLEMENT PSD bull Step 1 - Select no of points for FFT(Eg 64) bull Step 2 ndash 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 bull Step 3 - Compute the Auto Correlation of Sine wave
bull Step4 - Take output of auto correlation apply FFT algorithm
bull Step 4 - Use Graph option to view the PSD
bull Step 5 - Repeat Step-1 to 4 for different no of points amp frequencies
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
58
lsquoCrsquo Program to Implement PSD PSDc FILENAME Non_real_time_PSDc DESCRIPTION Program to Compute Non real time PSD using the TMS320C6711 DSK DESCRIPTION Number of points for FFT (PTS) x --gt Sine Wave Co-Efficients iobuffer --gt Out put of Auto Correlation x1 --gt use in graph window to view PSD =========================================================== include ltmathhgt define PTS 128 of points for FFT define PI 314159265358979 typedef struct float realimag 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 jsum=00 int nkia for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi(PTS20))
Re component of twiddle constants w[i]imag =-sin(2PIi(PTS20)) Im component of twiddle constants Input Signal X(n) for(i=0j=0iltPTSi++) x[i] = sin(2PI5iPTS) Signal x(Fs)=sin(2pifiFs)
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
59
samples[i]real=00 samples[i]imag=00 Auto Correlation of X(n)=R(t) for(n=0nltPTSn++) sum=0 for(k=0kltPTS-nk++) sum=sum+(x[k]x[n+k]) Auto Correlation R(t) iobuffer[n] = sum
FFT of R(t) for (i = 0 i lt PTS i++) swap buffers samples[i]real=iobuffer[i] buffer with new data for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call function FFTc PSD for (i = 0 i lt PTS i++) compute magnitude x1[i] = sqrt(samples[i]realsamples[i]real + samples[i]imagsamples[i]imag) end of main FFTc define PTS 128 of points for FFT typedef struct float realimag COMPLEX extern COMPLEX w[PTS] twiddle constants stored in w void FFT(COMPLEX Y int N) input sample array of points COMPLEX temp1temp2 temporary storage variables int ijk loop counter variables int upper_leg lower_leg indexof upperlower butterfly leg int leg_diff difference between upperlower leg int num_stages = 0 number of FFT stages (iterations) int index step indexstep through twiddle constant
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
60
i = 1 log(base2) of N points= of stages do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = (PTS2)N step between values in twiddleh 512 for (i = 0i lt num_stages i++) for N-point FFT index = 0 for (j = 0 j lt leg_diff j++) for (upper_leg = j upper_leg lt N upper_leg += (2leg_diff)) lower_leg = upper_leg+leg_diff temp1real = (Y[upper_leg])real + (Y[lower_leg])real temp1imag = (Y[upper_leg])imag + (Y[lower_leg])imag temp2real = (Y[upper_leg])real - (Y[lower_leg])real temp2imag = (Y[upper_leg])imag - (Y[lower_leg])imag (Y[lower_leg])real = temp2real(w[index])real -temp2imag(w[index])imag (Y[lower_leg])imag = temp2real(w[index])imag +temp2imag(w[index])real (Y[upper_leg])real = temp1real (Y[upper_leg])imag = temp1imag index += step leg_diff = leg_diff2 step = 2 j = 0 for (i = 1 i lt (N-1) i++)
bit reversal for resequencing data k = N2 while (k lt= j) j = j - k k = k2 j = j + k if (iltj) temp1real = (Y[j])real temp1imag = (Y[j])imag (Y[j])real = (Y[i])real (Y[j])imag = (Y[i])imag (Y[i])real = temp1real (Y[i])imag = temp1imag return
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
61
HOW TO PROCEED
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(ctimyprojects) with name PSDpjtrdquo
Add the source files PSDc and ldquoFFTcrdquo in the project using Projectadd files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file ldquorts_extlibrdquo
Compile the program using the Project-compile 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
OUT PUT
Cranes Software International Ltd TI - Solutions
62
Cranes Software International Ltd TI - Solutions
62