Uptu User Manual
-
Upload
aamir-mohd -
Category
Documents
-
view
71 -
download
1
Transcript of Uptu User Manual
TMS320C6713 DSK USER MANUAL
Cranes Software International Limited (TI-Solutions)
5 Airport Road Domlur Layout Bangalore ndash 560 071 Phone 080 ndash 51254131 2 3 4 Fax 080 - 25356299
tisalescranessoftwarecom
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 2
CONTENTS
DSK FEATURES INSTALLATION PROCEDURE INTRODUCTON TO CODE COMPOSER STUDIO
PROCEDURE TO WORK ON CCS EXPERIMENTS USING DSK
1 SOLUTION OF DIFFERENCE EQUATIONS 2 IMPULSE RESPONSE
3 TO VERIFY LINEAR CONVOLUTION
4 TO VERIFY CIRCULAR CONVOLUTION
5 PROCEDURE TO WORK IN REALTIME
6 TO DESIGN FIR(LOW PASSHIGH PASS)USING WINDOWING
TECHNIQUE a) USING RECTANGULAR WINDOW
b) USING TRIANGULAR WINDOW
c) USING KAISER WINDOW
7 TO DESIGN IIR FILTER (LPHP) 8 NOISE CANCELLATION USING ADAPTIVE FILTERS
9 TO PLOT SPECTROGRAM OF A SIGNAL OR SPEECH
10 TO FIND THE FFT OF GIVEN 1-D SIGNAL AND PLOT
11 TO COMPUTE POWER DENSITY SPECTRUM OF A SEQUENCE
12 MINI PROJECT
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 3
TMS320C6713 DSK Package Contents
The C6713 DSK builds on TIs industry-leading line of low cost easy-to-use DSP Starter Kit (DSK) development boards The high-performance board features the TMS320C6713 floating-point DSP Capable of performing 1350 million floating-point operations per second (MFLOPS) the C6713 DSP makes the C6713 DSK the most powerful DSK development board The DSK is USB port interfaced platform that allows to efficiently develop and test applications for the C6713 The DSK consists of a C6713-based printed circuit board that will serve as a hardware reference design for TIs customers products With extensive host PC and target DSP software support including bundled TI tools the DSK provides ease-of-use and capabilities that are attractive to DSP engineers The following checklist details items that are shipped with the C6711 DSK kit
TMS320C6713 DSK TMS320C6713 DSK development board
Other hardware External 5VDC power supply
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 4
IEEE 1284 compliant male-to-female cable
CD-ROM Code Composer Studio DSK tools The C6713 DSK has a TMS320C6713 DSP onboard that allows full-speed verification of code with Code Composer Studio The C6713 DSK provides
bull A USB Interface bull SDRAM and ROM bull An analog interface circuit for Data conversion (AIC) bull An IO port bull Embedded JTAG emulation support
Connectors on the C6713 DSK provide DSP external memory interface (EMIF) and peripheral signals that enable its functionality to be expanded with custom or third party daughter boards The DSK provides a C6713 hardware reference design that can assist you in the development of your own C6713-based products In addition to providing a reference for interfacing the DSP to various types of memories and peripherals the design also addresses power clock JTAG and parallel peripheral interfaces The C6713 DSK includes a stereo codec This analog interface circuit (AIC) has the following characteristics High-Performance Stereo Codec
bull 90-dB SNR Multibit Sigma-Delta ADC (A-weighted at 48 kHz) bull 100-dB SNR Multibit Sigma-Delta DAC (A-weighted at 48 kHz) bull 142 V 36 V Core Digital Supply Compatible With TI C54x DSP Core
Voltages bull 27 V 36 V Buffer and Analog Supply Compatible Both TI C54x DSP
Buffer Voltages bull 8-kHz 96-kHz Sampling-Frequency Support
Software Control Via TI McBSP-Compatible Multiprotocol Serial Port
bull I 2 C-Compatible and SPI-Compatible Serial-Port Protocols bull Glueless Interface to TI McBSPs bull
Audio-Data InputOutput Via TI McBSP-Compatible Programmable Audio Interface bull I 2 S-Compatible Interface Requiring Only One McBSP for both ADC and
DAC bull Standard I 2 S MSB or LSB Justified-Data Transfers bull 16202432-Bit Word Lengths
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 5
The C6713DSK has the following features The 6713 DSK is a low-cost standalone development platform that enables customers to evaluate and develop applications for the TI C67XX DSP family The DSK also serves as a hardware reference design for the TMS320C6713 DSP Schematics logic equations and application notes are available to ease hardware development and reduce time to market The DSK uses the 32-bit EMIF for the SDRAM (CE0) and daughtercard expansion interface (CE2 and CE3) The Flash is attached to CE1 of the EMIF in 8-bit mode An on-board AIC23 codec allows the DSP to transmit and receive analog signals McBSP0 is used for the codec control interface and McBSP1 is used for data Analog audio IO is done through four 35mm audio jacks that correspond to microphone input line input line output and headphone output The codec can select the microphone or the line input as the active input The analog output is driven to both the line out (fixed gain) and headphone (adjustable gain) connectors McBSP1 can be re-routed to the expansion connectors in software A programmable logic device called a CPLD is used to implement glue logic that ties the board components together The CPLD has a register based user interface that lets the user configure the board by reading and writing to the CPLD registers The registers reside at the midpoint of CE1 The DSK includes 4 LEDs and 4 DIP switches as a simple way to provide the user with interactive feedback Both are accessed by reading and writing to the CPLD registers An included 5V external power supply is used to power the board On-board voltage regulators provide the 126V DSP core voltage 33V digital and 33V analog voltages A voltage supervisor monitors the internally generated voltage and will hold the board in reset until the supplies are within operating specifications and the reset button is released If desired JP1 and JP2 can be used as power test points for the core and IO power supplies Code Composer communicates with the DSK through an embedded JTAG emulator with a USB host interface The DSK can also be used with an external emulator through the external JTAG connector TMS320C6713 DSP Features
Highest-Performance Floating-Point Digital Signal Processor (DSP) Eight 32-Bit InstructionsCycle 3264-Bit Data Word 300- 225- 200-MHz (GDP) and 225- 200- 167-MHz (PYP) Clock Rates
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 6
33- 44- 5- 6-Instruction Cycle Times 24001800 18001350 16001200 and 13361000 MIPS MFLOPS Rich Peripheral Set Optimized for Audio Highly Optimized CC++ Compiler Extended Temperature Devices Available
Advanced Very Long Instruction Word (VLIW) TMS320C67x DSP Core Eight Independent Functional Units
Two ALUs (Fixed-Point) Four ALUs (Floating- and Fixed-Point) Two Multipliers (Floating- and Fixed-Point)
Load-Store Architecture With 32 32-Bit General-Purpose Registers Instruction Packing Reduces Code Size All Instructions Conditional
Instruction Set Features Native Instructions for IEEE 754
Single- and Double-Precision Byte-Addressable (8- 16- 32-Bit Data) 8-Bit Overflow Protection Saturation Bit-Field Extract Set Clear Bit-Counting Normalization
L1L2 Memory Architecture 4K-Byte L1P Program Cache (Direct-Mapped) 4K-Byte L1D Data Cache (2-Way) 256K-Byte L2 Memory Total 64K-Byte L2 Unified CacheMapped RAM and 192K-
Byte Additional L2 Mapped RAM Device Configuration
Boot Mode HPI 8- 16- 32-Bit ROM Boot Endianness Little Endian Big Endian
32-Bit External Memory Interface (EMIF) Glueless Interface to SRAM EPROM Flash SBSRAM and SDRAM 512M-Byte Total Addressable External Memory Space
Enhanced Direct-Memory-Access (EDMA) Controller (16 Independent Channels) 16-Bit Host-Port Interface (HPI) Two Multichannel Audio Serial Ports (McASPs)
Two Independent Clock Zones Each (1 TX and 1 RX) Eight Serial Data Pins Per Port
Individually Assignable to any of the Clock Zones Each Clock Zone Includes
Programmable Clock Generator Programmable Frame Sync Generator TDM Streams From 2-32 Time Slots Support for Slot Size
8 12 16 20 24 28 32 Bits Data Formatter for Bit Manipulation
Wide Variety of I2S and Similar Bit Stream Formats
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 7
Integrated Digital Audio Interface Transmitter (DIT) Supports SPDIF IEC60958-1 AES-3 CP-430 Formats Up to 16 transmit pins Enhanced Channel StatusUser Data
Extensive Error Checking and Recovery Two Inter-Integrated Circuit Bus (I2C Bus) Multi-Master and Slave Interfaces Two Multichannel Buffered Serial Ports
Serial-Peripheral-Interface (SPI) High-Speed TDM Interface AC97 Interface
Two 32-Bit General-Purpose Timers Dedicated GPIO Module With 16 pins (External Interrupt Capable) Flexible Phase-Locked-Loop (PLL) Based Clock Generator Module IEEE-11491 (JTAG ) Boundary-Scan-Compatible Package Options
208-Pin PowerPAD Plastic (Low-Profile) Quad Flatpack (PYP) 272-BGA Packages (GDP and ZDP)
013-microm6-Level Copper Metal Process CMOS Technology
33-V IOs 12 -V Internal (GDP amp PYP) 33-V IOs 14-V Internal (GDP)(300 MHz only)
TMS320C6713 DSK Overview Block Diagram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 8
INSTALLATION SYSTEM REQUIREMENTS
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU
bull 600MB of free hard disk space bull 128MB 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
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 2
CONTENTS
DSK FEATURES INSTALLATION PROCEDURE INTRODUCTON TO CODE COMPOSER STUDIO
PROCEDURE TO WORK ON CCS EXPERIMENTS USING DSK
1 SOLUTION OF DIFFERENCE EQUATIONS 2 IMPULSE RESPONSE
3 TO VERIFY LINEAR CONVOLUTION
4 TO VERIFY CIRCULAR CONVOLUTION
5 PROCEDURE TO WORK IN REALTIME
6 TO DESIGN FIR(LOW PASSHIGH PASS)USING WINDOWING
TECHNIQUE a) USING RECTANGULAR WINDOW
b) USING TRIANGULAR WINDOW
c) USING KAISER WINDOW
7 TO DESIGN IIR FILTER (LPHP) 8 NOISE CANCELLATION USING ADAPTIVE FILTERS
9 TO PLOT SPECTROGRAM OF A SIGNAL OR SPEECH
10 TO FIND THE FFT OF GIVEN 1-D SIGNAL AND PLOT
11 TO COMPUTE POWER DENSITY SPECTRUM OF A SEQUENCE
12 MINI PROJECT
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 3
TMS320C6713 DSK Package Contents
The C6713 DSK builds on TIs industry-leading line of low cost easy-to-use DSP Starter Kit (DSK) development boards The high-performance board features the TMS320C6713 floating-point DSP Capable of performing 1350 million floating-point operations per second (MFLOPS) the C6713 DSP makes the C6713 DSK the most powerful DSK development board The DSK is USB port interfaced platform that allows to efficiently develop and test applications for the C6713 The DSK consists of a C6713-based printed circuit board that will serve as a hardware reference design for TIs customers products With extensive host PC and target DSP software support including bundled TI tools the DSK provides ease-of-use and capabilities that are attractive to DSP engineers The following checklist details items that are shipped with the C6711 DSK kit
TMS320C6713 DSK TMS320C6713 DSK development board
Other hardware External 5VDC power supply
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 4
IEEE 1284 compliant male-to-female cable
CD-ROM Code Composer Studio DSK tools The C6713 DSK has a TMS320C6713 DSP onboard that allows full-speed verification of code with Code Composer Studio The C6713 DSK provides
bull A USB Interface bull SDRAM and ROM bull An analog interface circuit for Data conversion (AIC) bull An IO port bull Embedded JTAG emulation support
Connectors on the C6713 DSK provide DSP external memory interface (EMIF) and peripheral signals that enable its functionality to be expanded with custom or third party daughter boards The DSK provides a C6713 hardware reference design that can assist you in the development of your own C6713-based products In addition to providing a reference for interfacing the DSP to various types of memories and peripherals the design also addresses power clock JTAG and parallel peripheral interfaces The C6713 DSK includes a stereo codec This analog interface circuit (AIC) has the following characteristics High-Performance Stereo Codec
bull 90-dB SNR Multibit Sigma-Delta ADC (A-weighted at 48 kHz) bull 100-dB SNR Multibit Sigma-Delta DAC (A-weighted at 48 kHz) bull 142 V 36 V Core Digital Supply Compatible With TI C54x DSP Core
Voltages bull 27 V 36 V Buffer and Analog Supply Compatible Both TI C54x DSP
Buffer Voltages bull 8-kHz 96-kHz Sampling-Frequency Support
Software Control Via TI McBSP-Compatible Multiprotocol Serial Port
bull I 2 C-Compatible and SPI-Compatible Serial-Port Protocols bull Glueless Interface to TI McBSPs bull
Audio-Data InputOutput Via TI McBSP-Compatible Programmable Audio Interface bull I 2 S-Compatible Interface Requiring Only One McBSP for both ADC and
DAC bull Standard I 2 S MSB or LSB Justified-Data Transfers bull 16202432-Bit Word Lengths
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 5
The C6713DSK has the following features The 6713 DSK is a low-cost standalone development platform that enables customers to evaluate and develop applications for the TI C67XX DSP family The DSK also serves as a hardware reference design for the TMS320C6713 DSP Schematics logic equations and application notes are available to ease hardware development and reduce time to market The DSK uses the 32-bit EMIF for the SDRAM (CE0) and daughtercard expansion interface (CE2 and CE3) The Flash is attached to CE1 of the EMIF in 8-bit mode An on-board AIC23 codec allows the DSP to transmit and receive analog signals McBSP0 is used for the codec control interface and McBSP1 is used for data Analog audio IO is done through four 35mm audio jacks that correspond to microphone input line input line output and headphone output The codec can select the microphone or the line input as the active input The analog output is driven to both the line out (fixed gain) and headphone (adjustable gain) connectors McBSP1 can be re-routed to the expansion connectors in software A programmable logic device called a CPLD is used to implement glue logic that ties the board components together The CPLD has a register based user interface that lets the user configure the board by reading and writing to the CPLD registers The registers reside at the midpoint of CE1 The DSK includes 4 LEDs and 4 DIP switches as a simple way to provide the user with interactive feedback Both are accessed by reading and writing to the CPLD registers An included 5V external power supply is used to power the board On-board voltage regulators provide the 126V DSP core voltage 33V digital and 33V analog voltages A voltage supervisor monitors the internally generated voltage and will hold the board in reset until the supplies are within operating specifications and the reset button is released If desired JP1 and JP2 can be used as power test points for the core and IO power supplies Code Composer communicates with the DSK through an embedded JTAG emulator with a USB host interface The DSK can also be used with an external emulator through the external JTAG connector TMS320C6713 DSP Features
Highest-Performance Floating-Point Digital Signal Processor (DSP) Eight 32-Bit InstructionsCycle 3264-Bit Data Word 300- 225- 200-MHz (GDP) and 225- 200- 167-MHz (PYP) Clock Rates
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 6
33- 44- 5- 6-Instruction Cycle Times 24001800 18001350 16001200 and 13361000 MIPS MFLOPS Rich Peripheral Set Optimized for Audio Highly Optimized CC++ Compiler Extended Temperature Devices Available
Advanced Very Long Instruction Word (VLIW) TMS320C67x DSP Core Eight Independent Functional Units
Two ALUs (Fixed-Point) Four ALUs (Floating- and Fixed-Point) Two Multipliers (Floating- and Fixed-Point)
Load-Store Architecture With 32 32-Bit General-Purpose Registers Instruction Packing Reduces Code Size All Instructions Conditional
Instruction Set Features Native Instructions for IEEE 754
Single- and Double-Precision Byte-Addressable (8- 16- 32-Bit Data) 8-Bit Overflow Protection Saturation Bit-Field Extract Set Clear Bit-Counting Normalization
L1L2 Memory Architecture 4K-Byte L1P Program Cache (Direct-Mapped) 4K-Byte L1D Data Cache (2-Way) 256K-Byte L2 Memory Total 64K-Byte L2 Unified CacheMapped RAM and 192K-
Byte Additional L2 Mapped RAM Device Configuration
Boot Mode HPI 8- 16- 32-Bit ROM Boot Endianness Little Endian Big Endian
32-Bit External Memory Interface (EMIF) Glueless Interface to SRAM EPROM Flash SBSRAM and SDRAM 512M-Byte Total Addressable External Memory Space
Enhanced Direct-Memory-Access (EDMA) Controller (16 Independent Channels) 16-Bit Host-Port Interface (HPI) Two Multichannel Audio Serial Ports (McASPs)
Two Independent Clock Zones Each (1 TX and 1 RX) Eight Serial Data Pins Per Port
Individually Assignable to any of the Clock Zones Each Clock Zone Includes
Programmable Clock Generator Programmable Frame Sync Generator TDM Streams From 2-32 Time Slots Support for Slot Size
8 12 16 20 24 28 32 Bits Data Formatter for Bit Manipulation
Wide Variety of I2S and Similar Bit Stream Formats
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 7
Integrated Digital Audio Interface Transmitter (DIT) Supports SPDIF IEC60958-1 AES-3 CP-430 Formats Up to 16 transmit pins Enhanced Channel StatusUser Data
Extensive Error Checking and Recovery Two Inter-Integrated Circuit Bus (I2C Bus) Multi-Master and Slave Interfaces Two Multichannel Buffered Serial Ports
Serial-Peripheral-Interface (SPI) High-Speed TDM Interface AC97 Interface
Two 32-Bit General-Purpose Timers Dedicated GPIO Module With 16 pins (External Interrupt Capable) Flexible Phase-Locked-Loop (PLL) Based Clock Generator Module IEEE-11491 (JTAG ) Boundary-Scan-Compatible Package Options
208-Pin PowerPAD Plastic (Low-Profile) Quad Flatpack (PYP) 272-BGA Packages (GDP and ZDP)
013-microm6-Level Copper Metal Process CMOS Technology
33-V IOs 12 -V Internal (GDP amp PYP) 33-V IOs 14-V Internal (GDP)(300 MHz only)
TMS320C6713 DSK Overview Block Diagram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 8
INSTALLATION SYSTEM REQUIREMENTS
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU
bull 600MB of free hard disk space bull 128MB 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
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 3
TMS320C6713 DSK Package Contents
The C6713 DSK builds on TIs industry-leading line of low cost easy-to-use DSP Starter Kit (DSK) development boards The high-performance board features the TMS320C6713 floating-point DSP Capable of performing 1350 million floating-point operations per second (MFLOPS) the C6713 DSP makes the C6713 DSK the most powerful DSK development board The DSK is USB port interfaced platform that allows to efficiently develop and test applications for the C6713 The DSK consists of a C6713-based printed circuit board that will serve as a hardware reference design for TIs customers products With extensive host PC and target DSP software support including bundled TI tools the DSK provides ease-of-use and capabilities that are attractive to DSP engineers The following checklist details items that are shipped with the C6711 DSK kit
TMS320C6713 DSK TMS320C6713 DSK development board
Other hardware External 5VDC power supply
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 4
IEEE 1284 compliant male-to-female cable
CD-ROM Code Composer Studio DSK tools The C6713 DSK has a TMS320C6713 DSP onboard that allows full-speed verification of code with Code Composer Studio The C6713 DSK provides
bull A USB Interface bull SDRAM and ROM bull An analog interface circuit for Data conversion (AIC) bull An IO port bull Embedded JTAG emulation support
Connectors on the C6713 DSK provide DSP external memory interface (EMIF) and peripheral signals that enable its functionality to be expanded with custom or third party daughter boards The DSK provides a C6713 hardware reference design that can assist you in the development of your own C6713-based products In addition to providing a reference for interfacing the DSP to various types of memories and peripherals the design also addresses power clock JTAG and parallel peripheral interfaces The C6713 DSK includes a stereo codec This analog interface circuit (AIC) has the following characteristics High-Performance Stereo Codec
bull 90-dB SNR Multibit Sigma-Delta ADC (A-weighted at 48 kHz) bull 100-dB SNR Multibit Sigma-Delta DAC (A-weighted at 48 kHz) bull 142 V 36 V Core Digital Supply Compatible With TI C54x DSP Core
Voltages bull 27 V 36 V Buffer and Analog Supply Compatible Both TI C54x DSP
Buffer Voltages bull 8-kHz 96-kHz Sampling-Frequency Support
Software Control Via TI McBSP-Compatible Multiprotocol Serial Port
bull I 2 C-Compatible and SPI-Compatible Serial-Port Protocols bull Glueless Interface to TI McBSPs bull
Audio-Data InputOutput Via TI McBSP-Compatible Programmable Audio Interface bull I 2 S-Compatible Interface Requiring Only One McBSP for both ADC and
DAC bull Standard I 2 S MSB or LSB Justified-Data Transfers bull 16202432-Bit Word Lengths
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 5
The C6713DSK has the following features The 6713 DSK is a low-cost standalone development platform that enables customers to evaluate and develop applications for the TI C67XX DSP family The DSK also serves as a hardware reference design for the TMS320C6713 DSP Schematics logic equations and application notes are available to ease hardware development and reduce time to market The DSK uses the 32-bit EMIF for the SDRAM (CE0) and daughtercard expansion interface (CE2 and CE3) The Flash is attached to CE1 of the EMIF in 8-bit mode An on-board AIC23 codec allows the DSP to transmit and receive analog signals McBSP0 is used for the codec control interface and McBSP1 is used for data Analog audio IO is done through four 35mm audio jacks that correspond to microphone input line input line output and headphone output The codec can select the microphone or the line input as the active input The analog output is driven to both the line out (fixed gain) and headphone (adjustable gain) connectors McBSP1 can be re-routed to the expansion connectors in software A programmable logic device called a CPLD is used to implement glue logic that ties the board components together The CPLD has a register based user interface that lets the user configure the board by reading and writing to the CPLD registers The registers reside at the midpoint of CE1 The DSK includes 4 LEDs and 4 DIP switches as a simple way to provide the user with interactive feedback Both are accessed by reading and writing to the CPLD registers An included 5V external power supply is used to power the board On-board voltage regulators provide the 126V DSP core voltage 33V digital and 33V analog voltages A voltage supervisor monitors the internally generated voltage and will hold the board in reset until the supplies are within operating specifications and the reset button is released If desired JP1 and JP2 can be used as power test points for the core and IO power supplies Code Composer communicates with the DSK through an embedded JTAG emulator with a USB host interface The DSK can also be used with an external emulator through the external JTAG connector TMS320C6713 DSP Features
Highest-Performance Floating-Point Digital Signal Processor (DSP) Eight 32-Bit InstructionsCycle 3264-Bit Data Word 300- 225- 200-MHz (GDP) and 225- 200- 167-MHz (PYP) Clock Rates
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 6
33- 44- 5- 6-Instruction Cycle Times 24001800 18001350 16001200 and 13361000 MIPS MFLOPS Rich Peripheral Set Optimized for Audio Highly Optimized CC++ Compiler Extended Temperature Devices Available
Advanced Very Long Instruction Word (VLIW) TMS320C67x DSP Core Eight Independent Functional Units
Two ALUs (Fixed-Point) Four ALUs (Floating- and Fixed-Point) Two Multipliers (Floating- and Fixed-Point)
Load-Store Architecture With 32 32-Bit General-Purpose Registers Instruction Packing Reduces Code Size All Instructions Conditional
Instruction Set Features Native Instructions for IEEE 754
Single- and Double-Precision Byte-Addressable (8- 16- 32-Bit Data) 8-Bit Overflow Protection Saturation Bit-Field Extract Set Clear Bit-Counting Normalization
L1L2 Memory Architecture 4K-Byte L1P Program Cache (Direct-Mapped) 4K-Byte L1D Data Cache (2-Way) 256K-Byte L2 Memory Total 64K-Byte L2 Unified CacheMapped RAM and 192K-
Byte Additional L2 Mapped RAM Device Configuration
Boot Mode HPI 8- 16- 32-Bit ROM Boot Endianness Little Endian Big Endian
32-Bit External Memory Interface (EMIF) Glueless Interface to SRAM EPROM Flash SBSRAM and SDRAM 512M-Byte Total Addressable External Memory Space
Enhanced Direct-Memory-Access (EDMA) Controller (16 Independent Channels) 16-Bit Host-Port Interface (HPI) Two Multichannel Audio Serial Ports (McASPs)
Two Independent Clock Zones Each (1 TX and 1 RX) Eight Serial Data Pins Per Port
Individually Assignable to any of the Clock Zones Each Clock Zone Includes
Programmable Clock Generator Programmable Frame Sync Generator TDM Streams From 2-32 Time Slots Support for Slot Size
8 12 16 20 24 28 32 Bits Data Formatter for Bit Manipulation
Wide Variety of I2S and Similar Bit Stream Formats
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 7
Integrated Digital Audio Interface Transmitter (DIT) Supports SPDIF IEC60958-1 AES-3 CP-430 Formats Up to 16 transmit pins Enhanced Channel StatusUser Data
Extensive Error Checking and Recovery Two Inter-Integrated Circuit Bus (I2C Bus) Multi-Master and Slave Interfaces Two Multichannel Buffered Serial Ports
Serial-Peripheral-Interface (SPI) High-Speed TDM Interface AC97 Interface
Two 32-Bit General-Purpose Timers Dedicated GPIO Module With 16 pins (External Interrupt Capable) Flexible Phase-Locked-Loop (PLL) Based Clock Generator Module IEEE-11491 (JTAG ) Boundary-Scan-Compatible Package Options
208-Pin PowerPAD Plastic (Low-Profile) Quad Flatpack (PYP) 272-BGA Packages (GDP and ZDP)
013-microm6-Level Copper Metal Process CMOS Technology
33-V IOs 12 -V Internal (GDP amp PYP) 33-V IOs 14-V Internal (GDP)(300 MHz only)
TMS320C6713 DSK Overview Block Diagram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 8
INSTALLATION SYSTEM REQUIREMENTS
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU
bull 600MB of free hard disk space bull 128MB 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
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 4
IEEE 1284 compliant male-to-female cable
CD-ROM Code Composer Studio DSK tools The C6713 DSK has a TMS320C6713 DSP onboard that allows full-speed verification of code with Code Composer Studio The C6713 DSK provides
bull A USB Interface bull SDRAM and ROM bull An analog interface circuit for Data conversion (AIC) bull An IO port bull Embedded JTAG emulation support
Connectors on the C6713 DSK provide DSP external memory interface (EMIF) and peripheral signals that enable its functionality to be expanded with custom or third party daughter boards The DSK provides a C6713 hardware reference design that can assist you in the development of your own C6713-based products In addition to providing a reference for interfacing the DSP to various types of memories and peripherals the design also addresses power clock JTAG and parallel peripheral interfaces The C6713 DSK includes a stereo codec This analog interface circuit (AIC) has the following characteristics High-Performance Stereo Codec
bull 90-dB SNR Multibit Sigma-Delta ADC (A-weighted at 48 kHz) bull 100-dB SNR Multibit Sigma-Delta DAC (A-weighted at 48 kHz) bull 142 V 36 V Core Digital Supply Compatible With TI C54x DSP Core
Voltages bull 27 V 36 V Buffer and Analog Supply Compatible Both TI C54x DSP
Buffer Voltages bull 8-kHz 96-kHz Sampling-Frequency Support
Software Control Via TI McBSP-Compatible Multiprotocol Serial Port
bull I 2 C-Compatible and SPI-Compatible Serial-Port Protocols bull Glueless Interface to TI McBSPs bull
Audio-Data InputOutput Via TI McBSP-Compatible Programmable Audio Interface bull I 2 S-Compatible Interface Requiring Only One McBSP for both ADC and
DAC bull Standard I 2 S MSB or LSB Justified-Data Transfers bull 16202432-Bit Word Lengths
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 5
The C6713DSK has the following features The 6713 DSK is a low-cost standalone development platform that enables customers to evaluate and develop applications for the TI C67XX DSP family The DSK also serves as a hardware reference design for the TMS320C6713 DSP Schematics logic equations and application notes are available to ease hardware development and reduce time to market The DSK uses the 32-bit EMIF for the SDRAM (CE0) and daughtercard expansion interface (CE2 and CE3) The Flash is attached to CE1 of the EMIF in 8-bit mode An on-board AIC23 codec allows the DSP to transmit and receive analog signals McBSP0 is used for the codec control interface and McBSP1 is used for data Analog audio IO is done through four 35mm audio jacks that correspond to microphone input line input line output and headphone output The codec can select the microphone or the line input as the active input The analog output is driven to both the line out (fixed gain) and headphone (adjustable gain) connectors McBSP1 can be re-routed to the expansion connectors in software A programmable logic device called a CPLD is used to implement glue logic that ties the board components together The CPLD has a register based user interface that lets the user configure the board by reading and writing to the CPLD registers The registers reside at the midpoint of CE1 The DSK includes 4 LEDs and 4 DIP switches as a simple way to provide the user with interactive feedback Both are accessed by reading and writing to the CPLD registers An included 5V external power supply is used to power the board On-board voltage regulators provide the 126V DSP core voltage 33V digital and 33V analog voltages A voltage supervisor monitors the internally generated voltage and will hold the board in reset until the supplies are within operating specifications and the reset button is released If desired JP1 and JP2 can be used as power test points for the core and IO power supplies Code Composer communicates with the DSK through an embedded JTAG emulator with a USB host interface The DSK can also be used with an external emulator through the external JTAG connector TMS320C6713 DSP Features
Highest-Performance Floating-Point Digital Signal Processor (DSP) Eight 32-Bit InstructionsCycle 3264-Bit Data Word 300- 225- 200-MHz (GDP) and 225- 200- 167-MHz (PYP) Clock Rates
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 6
33- 44- 5- 6-Instruction Cycle Times 24001800 18001350 16001200 and 13361000 MIPS MFLOPS Rich Peripheral Set Optimized for Audio Highly Optimized CC++ Compiler Extended Temperature Devices Available
Advanced Very Long Instruction Word (VLIW) TMS320C67x DSP Core Eight Independent Functional Units
Two ALUs (Fixed-Point) Four ALUs (Floating- and Fixed-Point) Two Multipliers (Floating- and Fixed-Point)
Load-Store Architecture With 32 32-Bit General-Purpose Registers Instruction Packing Reduces Code Size All Instructions Conditional
Instruction Set Features Native Instructions for IEEE 754
Single- and Double-Precision Byte-Addressable (8- 16- 32-Bit Data) 8-Bit Overflow Protection Saturation Bit-Field Extract Set Clear Bit-Counting Normalization
L1L2 Memory Architecture 4K-Byte L1P Program Cache (Direct-Mapped) 4K-Byte L1D Data Cache (2-Way) 256K-Byte L2 Memory Total 64K-Byte L2 Unified CacheMapped RAM and 192K-
Byte Additional L2 Mapped RAM Device Configuration
Boot Mode HPI 8- 16- 32-Bit ROM Boot Endianness Little Endian Big Endian
32-Bit External Memory Interface (EMIF) Glueless Interface to SRAM EPROM Flash SBSRAM and SDRAM 512M-Byte Total Addressable External Memory Space
Enhanced Direct-Memory-Access (EDMA) Controller (16 Independent Channels) 16-Bit Host-Port Interface (HPI) Two Multichannel Audio Serial Ports (McASPs)
Two Independent Clock Zones Each (1 TX and 1 RX) Eight Serial Data Pins Per Port
Individually Assignable to any of the Clock Zones Each Clock Zone Includes
Programmable Clock Generator Programmable Frame Sync Generator TDM Streams From 2-32 Time Slots Support for Slot Size
8 12 16 20 24 28 32 Bits Data Formatter for Bit Manipulation
Wide Variety of I2S and Similar Bit Stream Formats
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 7
Integrated Digital Audio Interface Transmitter (DIT) Supports SPDIF IEC60958-1 AES-3 CP-430 Formats Up to 16 transmit pins Enhanced Channel StatusUser Data
Extensive Error Checking and Recovery Two Inter-Integrated Circuit Bus (I2C Bus) Multi-Master and Slave Interfaces Two Multichannel Buffered Serial Ports
Serial-Peripheral-Interface (SPI) High-Speed TDM Interface AC97 Interface
Two 32-Bit General-Purpose Timers Dedicated GPIO Module With 16 pins (External Interrupt Capable) Flexible Phase-Locked-Loop (PLL) Based Clock Generator Module IEEE-11491 (JTAG ) Boundary-Scan-Compatible Package Options
208-Pin PowerPAD Plastic (Low-Profile) Quad Flatpack (PYP) 272-BGA Packages (GDP and ZDP)
013-microm6-Level Copper Metal Process CMOS Technology
33-V IOs 12 -V Internal (GDP amp PYP) 33-V IOs 14-V Internal (GDP)(300 MHz only)
TMS320C6713 DSK Overview Block Diagram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 8
INSTALLATION SYSTEM REQUIREMENTS
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU
bull 600MB of free hard disk space bull 128MB 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
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 5
The C6713DSK has the following features The 6713 DSK is a low-cost standalone development platform that enables customers to evaluate and develop applications for the TI C67XX DSP family The DSK also serves as a hardware reference design for the TMS320C6713 DSP Schematics logic equations and application notes are available to ease hardware development and reduce time to market The DSK uses the 32-bit EMIF for the SDRAM (CE0) and daughtercard expansion interface (CE2 and CE3) The Flash is attached to CE1 of the EMIF in 8-bit mode An on-board AIC23 codec allows the DSP to transmit and receive analog signals McBSP0 is used for the codec control interface and McBSP1 is used for data Analog audio IO is done through four 35mm audio jacks that correspond to microphone input line input line output and headphone output The codec can select the microphone or the line input as the active input The analog output is driven to both the line out (fixed gain) and headphone (adjustable gain) connectors McBSP1 can be re-routed to the expansion connectors in software A programmable logic device called a CPLD is used to implement glue logic that ties the board components together The CPLD has a register based user interface that lets the user configure the board by reading and writing to the CPLD registers The registers reside at the midpoint of CE1 The DSK includes 4 LEDs and 4 DIP switches as a simple way to provide the user with interactive feedback Both are accessed by reading and writing to the CPLD registers An included 5V external power supply is used to power the board On-board voltage regulators provide the 126V DSP core voltage 33V digital and 33V analog voltages A voltage supervisor monitors the internally generated voltage and will hold the board in reset until the supplies are within operating specifications and the reset button is released If desired JP1 and JP2 can be used as power test points for the core and IO power supplies Code Composer communicates with the DSK through an embedded JTAG emulator with a USB host interface The DSK can also be used with an external emulator through the external JTAG connector TMS320C6713 DSP Features
Highest-Performance Floating-Point Digital Signal Processor (DSP) Eight 32-Bit InstructionsCycle 3264-Bit Data Word 300- 225- 200-MHz (GDP) and 225- 200- 167-MHz (PYP) Clock Rates
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 6
33- 44- 5- 6-Instruction Cycle Times 24001800 18001350 16001200 and 13361000 MIPS MFLOPS Rich Peripheral Set Optimized for Audio Highly Optimized CC++ Compiler Extended Temperature Devices Available
Advanced Very Long Instruction Word (VLIW) TMS320C67x DSP Core Eight Independent Functional Units
Two ALUs (Fixed-Point) Four ALUs (Floating- and Fixed-Point) Two Multipliers (Floating- and Fixed-Point)
Load-Store Architecture With 32 32-Bit General-Purpose Registers Instruction Packing Reduces Code Size All Instructions Conditional
Instruction Set Features Native Instructions for IEEE 754
Single- and Double-Precision Byte-Addressable (8- 16- 32-Bit Data) 8-Bit Overflow Protection Saturation Bit-Field Extract Set Clear Bit-Counting Normalization
L1L2 Memory Architecture 4K-Byte L1P Program Cache (Direct-Mapped) 4K-Byte L1D Data Cache (2-Way) 256K-Byte L2 Memory Total 64K-Byte L2 Unified CacheMapped RAM and 192K-
Byte Additional L2 Mapped RAM Device Configuration
Boot Mode HPI 8- 16- 32-Bit ROM Boot Endianness Little Endian Big Endian
32-Bit External Memory Interface (EMIF) Glueless Interface to SRAM EPROM Flash SBSRAM and SDRAM 512M-Byte Total Addressable External Memory Space
Enhanced Direct-Memory-Access (EDMA) Controller (16 Independent Channels) 16-Bit Host-Port Interface (HPI) Two Multichannel Audio Serial Ports (McASPs)
Two Independent Clock Zones Each (1 TX and 1 RX) Eight Serial Data Pins Per Port
Individually Assignable to any of the Clock Zones Each Clock Zone Includes
Programmable Clock Generator Programmable Frame Sync Generator TDM Streams From 2-32 Time Slots Support for Slot Size
8 12 16 20 24 28 32 Bits Data Formatter for Bit Manipulation
Wide Variety of I2S and Similar Bit Stream Formats
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 7
Integrated Digital Audio Interface Transmitter (DIT) Supports SPDIF IEC60958-1 AES-3 CP-430 Formats Up to 16 transmit pins Enhanced Channel StatusUser Data
Extensive Error Checking and Recovery Two Inter-Integrated Circuit Bus (I2C Bus) Multi-Master and Slave Interfaces Two Multichannel Buffered Serial Ports
Serial-Peripheral-Interface (SPI) High-Speed TDM Interface AC97 Interface
Two 32-Bit General-Purpose Timers Dedicated GPIO Module With 16 pins (External Interrupt Capable) Flexible Phase-Locked-Loop (PLL) Based Clock Generator Module IEEE-11491 (JTAG ) Boundary-Scan-Compatible Package Options
208-Pin PowerPAD Plastic (Low-Profile) Quad Flatpack (PYP) 272-BGA Packages (GDP and ZDP)
013-microm6-Level Copper Metal Process CMOS Technology
33-V IOs 12 -V Internal (GDP amp PYP) 33-V IOs 14-V Internal (GDP)(300 MHz only)
TMS320C6713 DSK Overview Block Diagram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 8
INSTALLATION SYSTEM REQUIREMENTS
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU
bull 600MB of free hard disk space bull 128MB 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
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 6
33- 44- 5- 6-Instruction Cycle Times 24001800 18001350 16001200 and 13361000 MIPS MFLOPS Rich Peripheral Set Optimized for Audio Highly Optimized CC++ Compiler Extended Temperature Devices Available
Advanced Very Long Instruction Word (VLIW) TMS320C67x DSP Core Eight Independent Functional Units
Two ALUs (Fixed-Point) Four ALUs (Floating- and Fixed-Point) Two Multipliers (Floating- and Fixed-Point)
Load-Store Architecture With 32 32-Bit General-Purpose Registers Instruction Packing Reduces Code Size All Instructions Conditional
Instruction Set Features Native Instructions for IEEE 754
Single- and Double-Precision Byte-Addressable (8- 16- 32-Bit Data) 8-Bit Overflow Protection Saturation Bit-Field Extract Set Clear Bit-Counting Normalization
L1L2 Memory Architecture 4K-Byte L1P Program Cache (Direct-Mapped) 4K-Byte L1D Data Cache (2-Way) 256K-Byte L2 Memory Total 64K-Byte L2 Unified CacheMapped RAM and 192K-
Byte Additional L2 Mapped RAM Device Configuration
Boot Mode HPI 8- 16- 32-Bit ROM Boot Endianness Little Endian Big Endian
32-Bit External Memory Interface (EMIF) Glueless Interface to SRAM EPROM Flash SBSRAM and SDRAM 512M-Byte Total Addressable External Memory Space
Enhanced Direct-Memory-Access (EDMA) Controller (16 Independent Channels) 16-Bit Host-Port Interface (HPI) Two Multichannel Audio Serial Ports (McASPs)
Two Independent Clock Zones Each (1 TX and 1 RX) Eight Serial Data Pins Per Port
Individually Assignable to any of the Clock Zones Each Clock Zone Includes
Programmable Clock Generator Programmable Frame Sync Generator TDM Streams From 2-32 Time Slots Support for Slot Size
8 12 16 20 24 28 32 Bits Data Formatter for Bit Manipulation
Wide Variety of I2S and Similar Bit Stream Formats
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 7
Integrated Digital Audio Interface Transmitter (DIT) Supports SPDIF IEC60958-1 AES-3 CP-430 Formats Up to 16 transmit pins Enhanced Channel StatusUser Data
Extensive Error Checking and Recovery Two Inter-Integrated Circuit Bus (I2C Bus) Multi-Master and Slave Interfaces Two Multichannel Buffered Serial Ports
Serial-Peripheral-Interface (SPI) High-Speed TDM Interface AC97 Interface
Two 32-Bit General-Purpose Timers Dedicated GPIO Module With 16 pins (External Interrupt Capable) Flexible Phase-Locked-Loop (PLL) Based Clock Generator Module IEEE-11491 (JTAG ) Boundary-Scan-Compatible Package Options
208-Pin PowerPAD Plastic (Low-Profile) Quad Flatpack (PYP) 272-BGA Packages (GDP and ZDP)
013-microm6-Level Copper Metal Process CMOS Technology
33-V IOs 12 -V Internal (GDP amp PYP) 33-V IOs 14-V Internal (GDP)(300 MHz only)
TMS320C6713 DSK Overview Block Diagram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 8
INSTALLATION SYSTEM REQUIREMENTS
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU
bull 600MB of free hard disk space bull 128MB 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
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 7
Integrated Digital Audio Interface Transmitter (DIT) Supports SPDIF IEC60958-1 AES-3 CP-430 Formats Up to 16 transmit pins Enhanced Channel StatusUser Data
Extensive Error Checking and Recovery Two Inter-Integrated Circuit Bus (I2C Bus) Multi-Master and Slave Interfaces Two Multichannel Buffered Serial Ports
Serial-Peripheral-Interface (SPI) High-Speed TDM Interface AC97 Interface
Two 32-Bit General-Purpose Timers Dedicated GPIO Module With 16 pins (External Interrupt Capable) Flexible Phase-Locked-Loop (PLL) Based Clock Generator Module IEEE-11491 (JTAG ) Boundary-Scan-Compatible Package Options
208-Pin PowerPAD Plastic (Low-Profile) Quad Flatpack (PYP) 272-BGA Packages (GDP and ZDP)
013-microm6-Level Copper Metal Process CMOS Technology
33-V IOs 12 -V Internal (GDP amp PYP) 33-V IOs 14-V Internal (GDP)(300 MHz only)
TMS320C6713 DSK Overview Block Diagram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 8
INSTALLATION SYSTEM REQUIREMENTS
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU
bull 600MB of free hard disk space bull 128MB 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
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 8
INSTALLATION SYSTEM REQUIREMENTS
Minimum
Recommended
bull 233MHz or Higher Pentium-Compatible CPU
bull 600MB of free hard disk space bull 128MB 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
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 9
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 Start$Programs$Texas Instruments$Code Composer Studio$Code Composer Studio C6713 DSK Tools$C6713 DSK Diagnostic Utilities
bull The below Screen will appear bull Select$ Start$Select 6713 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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 10
SOFTWARE INSTALLATION You must install the hardware before you install the software on your system The requirements for the operating platform are
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
Respond to the dialog boxes as the installation program runs
The Installation program automatically configures CC Studio IDE for operation with your DSK and creates a CCStudio IDE DSK icon on your desktop To install follow these instructions
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 11
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 Documents for Reference
spru509 $$$$ Code Composer Studio getting started guide
spru189 $$$$ TMS320C6000 CPU amp Instruction set guide spru190 $$$$ TMS320C6000 Peripherals guide slws106d $$$$ codec(TLV320AIC23) Data Manual spru402 $$$$ Programmerrsquos Reference Guide sprs186j $$$$ TMS320C6713 DSP Soft Copy of Documents are available at ctidocspdf
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 12
Procedure to work on Code Composer Studio
1 To create a New Project
Project $ New (SUMpjt)
2 To Create a Source file File $ New
Type the code (Save amp give a name to file Eg sumc)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 13
3 To Add Source files to Project Project $ Add files to Project $ sumc
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 14
4 To Add rts6700lib file amp hellocmd Project $ Add files to Project $rts6700lib Path cCCStudioc6000cgtoolslibrts6700lib Note Select Object amp Library in(ol) in Type of files
Project $ Add files to Project $hellocmd Path ctitutorialdsk6713hello1hellocmd Note Select Linker Command file(cmd) in Type of files
5 To Compile
Project $ Compile File 6 To build or Link
Project $ build Which will create the final executable (out) file(Eg sumout)
7 Procedure to Load and Run program
Load program to DSK File $ Load program $ sum out
8 To execute project
Debug $ Run
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 15
sumc includeltstdiohgt main() int i=30j=40k k=i+j printf(dk) 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 16
DIFFERENCE EQUATION
An Nth order linear constant ndash coefficient difference equation can be represented as N M
ak y(n-k) = br x(n-r) k=0 r=0 If we assume that the system is causal a linear difference equation provides an explicit relationship between the input and outputthis can be seen by rewriting above equation M N
y(n) = bra0 x(n-r) -- aka0 y y(n-k) r=0 k=1 lsquoClsquo Program to Implement Difference Equation include ltstdiohgt includeltmathhgt define FREQ 400 float y[3]=000 float x[3]=000 float z[128]m[128]n[128]p[128] main() int i=0j float a[3]= 007223101444620072231 float b[3]= 1000000-11092290398152 for(i=0ilt128i++) m[i]=sin(2314FREQi24000) for(j=0jlt128j++) x[0]=m[j] y[0] = (a[0] x[0]) +(a[1] x[1] ) +(x[2]a[2]) - (y[1]b[1])- (y[2]b[2]) z[j]=y[0] y[2]=y[1] y[1]=y[0] x[2]=x[1] x[1] = x[0]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 17
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 files DIFF EQ1c to the project using lsquoProject$$$$add files to projectrsquo pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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
To View output graphically Select view $ graph $ time and frequency
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 18
Note To verify the Diffence Equation Observe the output for high frequency and low frequency by changing variable ldquoFREQrdquo in the program
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 19
IMPULSE RESPONSE lsquoCrsquo Program to Implement Impulse response include ltstdiohgt define Order 2 define Len 10 float y[Len]=000sum main() int jk float a[Order+1]=01311 02622 01311 float b[Order+1]=1 -07478 02722 for(j=0jltLenj++) sum=0 for(k=1klt=Orderk++) if((j-k)gt=0) sum=sum+(b[k]y[j-k]) if(jlt=Order) y[j]=a[j]-sum else y[j]=-sum printf(Respose[d] = fnjy[j])
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 20
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 21
lsquoCrsquo PROGRAM TO IMPLEMENT LINEAR CONVOLUTION prg to implement linear convolution includeltstdiohgt define LENGHT1 6 Lenght of ip samples sequence define LENGHT2 4 Lenght of impulse response Co-efficients int x[2LENGHT1-1]=12345600000 Input Signal Samples int h[2LENGHT1-1]=12340000000 Impulse Response Co-efficients int y[LENGHT1+LENGHT2-1] main() int i=0j for(i=0ilt(LENGHT1+LENGHT2-1)i++) y[i]=0 for(j=0jlt=ij++) y[i]+=x[j]h[i-j] for(i=0ilt(LENGHT1+LENGHT2-1)i++) 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 files convc to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
Compile the program using the Project-compile pull down menu or by
clicking the shortcut icon on the left side of program window
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 22
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
To View output graphically Select view $ graph $ time and frequency
ASSEMBLY PROGRAM TO IMPLEMENT LINEAR CONVOLUTION convasm global _main X half 12340000 input1 M=4 H half 12340000 input2 N=4 bss Y142 OUTPUT R=M+N-1 At the end of input sequences pad lsquoMrsquo and lsquoNrsquo no of zerorsquos _main MVKL S1 XA4 MVKH S1 XA4 POINTER TO X MVKL S2 HB4 MVKH S2 HB4 POINTER TO H MVKL S1 YA5 MVKH S1 YA5 POINTER TO Y MVK S2 7B2 R=M+N-1 MOVE THE VALUE OF lsquoRrsquoTO B2 FOR DIFFERENT LENGTH OF IP SEQUENCES ZERO L1 A7 ZERO L1 A3 I=0 LL2 ZERO L1 A2 ZERO L1 A8 J=0 for(i=0iltm+n-1i++) LL1 LDH D1 A4[A8]A6 for(j=0jlt=ij++) MV S2X A8B5 y[i]+=x[j]h[i-j] SUB L2 A3B5B7 LDH D2 B4[B7]B6 NOP 4 MPY M1X A6B6A7 ADD L1 A81A8 ADD L1 A2A7A2 CMPLT L2X B5A3B0 [B0] B S2 LL1 NOP 5 STH D1 A2A5[A3]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 23
ADD L1 A31A3 CMPLT L1X A3B2A2 [A2] B S1 LL2 NOP 5 B B3 NOP 5
Configure the graphical window as shown below
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 24
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 25
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 26
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 10 h[4]=1 1 00 OUT PUT y[4]=3 5 30
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 27
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 cir convpjt
Add the source files Circular ConvolutionC to the project using Project$add files to project pull down menu
Add the linker command file hellocmd
(Path ctitutorialdsk6713hello1hellocmd)
Add the run time support library file rts6700lib (Path ctic6000cgtoolslibrts6700lib)
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 28
TMS320C6713 DSK CODEC(TLV320AIC23) Configuration
Using Board Support Library 10 Unit Objective To configure the codec TLV320AIC23 for a talk through program using the board support library 20 Prerequisites TMS320C6713 DSP Starter Kit PC with Code Composer Studio CRO Audio Source Speakers and Signal Generator 30 Discussion on Fundamentals Refer BSL API Module under help $ contents $ TMS320C6713 DSK 40 Procedure
bull All the Real time implementations covered in the Implementations module follow code Configuration using board support library
bull The board 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 BSL is a fully scalable component of DSPBIOS It does not require the use of other DSPBIOS components to operate
Source Code codecc
Procedure for Real time Programs 1 Connect CRO to the Socket Provided for LINE OUT 2 Connect a Signal Generator to the LINE IN Socket 3 Switch on the Signal Generator with a sine wave of frequency 500 Hz and Vp-p=15v 4 Now Switch on the DSK and Bring Up Code Composer Studio on the PC 5 Create a new project with name codecpjt 6 From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as xyzcdb
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 29
7 Add xyzcdb to the current project 8 Add the given codecc file to the current project which has the main function and
calls all the other necessary routines 9 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 10 Copy files ldquodsk6713hrdquo and ldquodsk6713_aic23hrdquo from CCCStudioC6000dsk6713include
and paste it in current project 11 Build Load and Run the program 12 You can notice the input signal of 500 Hz appearing on the CRO verifying the codec
configuration 13 You can also pass an audio input and hear the output signal through the speakers 14 You can also vary the sampling frequency using the DSK6713_AIC23_setFreq
Function in the codecc file and repeat the above steps
50 Conclusion The codec TLV320AIC23 successfully configured using the board support library and verified
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 30
codecc include xyzcfgh include dsk6713h include dsk6713_aic23h Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) set codec sampling frequency DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_input)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec l_input)) Close the codec DSK6713_AIC23_closeCodec(hCodec)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 31
Advance Discrete Time Filter Design(FIR)
Finite Impulse Response Filter 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 32
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 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 33
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 Low Pass Kaiser filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 34
T2 Matlab generated Coefficients for FIR Low Pass Rectangular
filter FLOWCHART FOR FIR T3 Matlab generated Coefficients for FIR Low Pass Triangular filter
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
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 35
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)) 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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 36
T1 MATLAB generated Coefficients for FIR High Pass Kaiser
filter IMPLEMENTATION OF AN FIR FILTER ALGORITHM TO IMPLEMENT We need to realize an advance FIR filter by implementing its difference equation as per the specifications A direct form I implementation approach is taken (The filter coefficients are taken as ai as generated by the Matlab program) bull bull T2 MATLAB generated Coefficients for FIR High Pass Rectangular filter
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 37
T3 MATLAB generated Coefficients for FIR High Pass Triangular
filter
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 38
FLOW CHART TO IMPLEMENT FIR FILTER
No Yes
Initialize Counter = 0Initialize Output = 0 i = 0
Output += coeff[N-i]val[i]Shift the input value by one
Initialize the DSP Board
Take a new input in lsquodatarsquo from the analog in of codec in lsquod rsquo
Is the loop Cnt = order
Output += coeff[0]data Put the lsquodatarsquo in lsquovalrsquo array
Write the value lsquoOutputrsquo to Analog output of the codec
Poll the ready bit when asserted proceed
Start
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 39
C PROGRAM TO IMPLEMENT FIR FILTER firc include filtercfgh include dsk6713h include dsk6713_aic23h float filter_Coeff[] =0000000-0001591-000242300000000005728 00111390010502-0000000-0018003-0033416-00315050000000 00630100144802022053402624480220534014480200630100000000 -0031505-0033416-0018003-0000000001050200111390005728 0000000-0002423-00015910000000 static short in_buffer[100] DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec Uint32 l_input r_inputl_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(Int16)FIR_FILTER(ampfilter_Coeff l_input)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 40
r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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=29igt0i--) in_buffer[i] = in_buffer[i-1] shuffle the buffer for(i=0ilt31i++) 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 FIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 41
MATLAB GENERATED FREQUENCY RESPONSE High Pass FIR filter(Fc= 800Hz)
Low Pass FIR filter (Fc=1000Hz)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 42
Advance Discrete Time Filter Design(IIR) IIR filter Designing Experiments GENERAL CONSIDERATIONS In the design of frequency selective filters the desired filter characteristics are specified in the frequency domain in terms of the desired magnitude and phase response of the filter In the filter design process we determine the coefficients of a causal IIR filter that closely approximates the desired frequency response specifications IMPLEMENTATION OF DISCRETE-TIME SYSTEMS Discrete time Linear Time-Invariant (LTI) systems can be described completely by constant coefficient linear difference equations Representing a system in terms of constant coefficient linear difference equation is its time domain characterization In the design of a simple 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 TMS320C6713 Architecture and instruction set
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 43
EQUIPMENTS NEEDED Host (PC) with windows(9598MeXPNT2000) TMS320C6713 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 6713Codec using BSL
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 44
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]
P ll f d bit
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 45
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 46
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 47
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 48
lsquoCrsquo PROGRAM TO IMPLEMENT IIR FILTER include xyzcfgh include dsk6713h include dsk6713_aic23h const signed int filter_Coeff[] = 12730-12730127302767-1832421137 HP 2500 31231231232767-2794324367 LP 800 14551455145532767-2314021735 LP 2500 9268-9268926832767-739518367 HP 4000 7215-721572153276750396171 HP 7000 Codec configuration settings DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 3) while(1) Read a sample to the left channel while (DSK6713_AIC23_read(hCodec ampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=IIR_FILTER(ampfilter_Coeff l_input) r_output=l_output Send a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 49
while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) 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 int temp=0 temp = (short int)x1 Copy input to temp x[0] = (signed int) temp Copy input to x[stages][0] temp = ( (int)h[0] x[0]) B0 x(n) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[1] x[1]) B12 x(n-1) temp += ( (int)h[2] x[2]) B2 x(n-2) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)h[4] y[1]) A12 y(n-1) temp -= ( (int)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] = 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 (templtlt2)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 50
PROCEDURE
Switch on the DSP board Open the Code Composer Studio Create a new project
Project $ New (File Name pjt Eg IIRpjt) Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the given above C source file to the current project (remove codecc source file from the project if you have already added)
Connect the speaker jack to the input of the CRO Build the program Load the generated object file(out) on to Target board Run the program Observe the waveform that appears on the CRO screen
Vary the frequency on function generator to see the response of filter
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 51
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 52
Adaptive Filters
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 53
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 54
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 55
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 56
C PROGRAM TO IMPLEMENT NOISE CANCELLATION include xyzcfgh include dsk6713h include dsk6713_aic23h define beta 1E-12 rate of convergence define N 30 short int adaptive_filter(short int short int ) float delay[N] float w[N] unionunsigned int uint short channel[2] AIC23_data DSK6713_AIC23_Config config = 0x0017 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume 0x0017 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume 0x00d8 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume 0x00d8 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume 0x0011 4 DSK6713_AIC23_ANAPATH Analog audio path control 0x0000 5 DSK6713_AIC23_DIGPATH Digital audio path control 0x0000 6 DSK6713_AIC23_POWERDOWN Power down control 0x0043 7 DSK6713_AIC23_DIGIF Digital audio interface format 0x0081 8 DSK6713_AIC23_SAMPLERATE Sample rate control 0x0001 9 DSK6713_AIC23_DIGACT Digital interface activation main() - Main code routine initializes BSL and generates tone void main() DSK6713_AIC23_CodecHandle hCodec int l_input r_input int l_output r_output Initialize the board support library must be called first DSK6713_init() Start the codec hCodec = DSK6713_AIC23_openCodec(0 ampconfig) DSK6713_AIC23_setFreq(hCodec 1) while(1) Read a sample to the left channel
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 57
while (DSK6713_AIC23_read(hCodecampl_input)) Read a sample to the right channel while (DSK6713_AIC23_read(hCodec ampr_input)) l_output=(short int)adaptive_filter(l_inputr_input) r_output=l_output Send a sample to the left channel while (DSK6713_AIC23_write(hCodec l_output)) Send a sample to the right channel while (DSK6713_AIC23_write(hCodec r_output)) Close the codec DSK6713_AIC23_closeCodec(hCodec) short int adaptive_filter(short l_input1short r_input1) ISR short ioutputT float yn=0 E=0 dplusn=0 desired=0 noise=0 for (T = 0 T lt 30 T++) w[T] = 0 init buffer for weights delay[T] = 0 init buffer for delay samples desired = l_input1 noise = r_input1 dplusn = desired + noise desired+noise delay[0] = noise noise as input to adapt FIR for (i = 0 i lt N i++) to calculate out of adapt FIR yn += (w[i] delay[i]) output of adaptive filter E = (desired + noise) - yn error signal=(d+n)-yn for (i = N-1 i gt= 0 i--) to update weights and delays w[i] = w[i] + betaEdelay[i] update weights delay[i] = delay[i-1] update delay samples output=((short)E) error signal as overall output output=((short)dplusn)output (desired+noise) overall output result return(output)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 58
PROCEDURE
Switch on the DSP board Open the Code Composer Studio
Create a new project
Project $ New (File Name pjt Eg noisecancellationpjt)
Initialize on board codec
Note ldquoKindly refer the Topic Configuration of 6713 Codec using BSLrdquo
Add the above C source file to the current project (remove codecc source file from the project if you have already added)
Desired Signal $$$$ 400 Hz
Noise $$$$ 30 KHz Input a desired sinusoidal signal into the Left channel and Noise signal of 3KHz into the Right channel
Build the project Load the generated object file(out) on to Target board
Run the program
Observe the waveform that appears on the CRO screen Verify that the 3 KHz
noise signal is being cancelled gradually
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 59
SPECTROGRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 60
Spectrogram with RTDX using MATLAB This version of project makes use of RTDX with MATLAB for transferring data from the DSK to the PC host This section introduces configuration file(CDB) file and RTDX with MATLAB This project uses source program spectrogram_rtdx_mtlc that runs on the DSK which computes 256 point FFT and enables an RTDX output channel to writesend the resulting FFT data to the PC running MATLAB for finding the spectrogram A total of N2 (128 points )are sent The (CDB) configuration file is used to set interrupt INT11 From this configuration file select InputOutput $$$$ RTDX Right click on properties and change the RTDX buffer size to 8200 Within CCS select tools $$$$ RTDX $$$$ Configure to set the host buffer size to 2048(from 1024) An input signal is read in blocks of 256 samples Each block of data is then multiplied with a hamming window of length 256 points The FFT of the windowed data is calculated and squared Half of the resulting FFT of each block of 256 points is then transferred to the PC running MATLAB to find the specrtrogram
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 61
Spectrogram_rtdx_mtlc Time-Frequency analysis of signals Using RTDX-MATLAB include dsk6713_aic23h codec-DSK support file Uint32 fs=DSK6713_AIC23_FREQ_8KHZ set sampling rate include ltrtdxhgt RTDX support file include ltmathhgt include hammingcof Hamming window coefficients define PTS 256 of points for FFT define PI 314159265358979 typedef struct float realimag COMPLEX void FFT(COMPLEX Y int n) FFT prototype float iobuffer[PTS]iobuffer1[PTS]a[PTS] input and output buffer float x1[PTS] intermediate buffer short i general purpose index variable int j kl curr_block = 0 index variables 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 RTDX_CreateOutputChannel(ochan) create output channel C6x-gtPC main() for (i = 0 iltPTS i++) set up twiddle constants in w w[i]real = cos(2PIi5120) Re component of twiddle constants w[i]imag =-sin(2PIi5120) Im component of twiddle constants comm_intr() init DSK codec McBSP while(RTDX_isOutputEnabled(ampochan)) wait for PC to enable RTDX puts(nn Waiting ) while waiting for(l=0llt256l++) a[l]=cos(23141500l8000) for(k=0klt5000k++) infinite loop while (flag == 0) wait until iobuffer is full flag = 0 reset flag for (i = 0 i lt PTS i++) swap buffers iobuffer1[i]=iobuffer[i]+a[i]
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 62
samples[i]real=h[i]iobuffer1[i] multiply by Hamming window coeffs iobuffer1[i] = x1[i] process frame to iobuffer for (i = 0 i lt PTS i++) samples[i]imag = 00 imag components = 0 FFT(samplesPTS) call C-coded FFT function for (i = 0 i lt PTS i++) compute square of FFT magnitude x1[i] = (samples[i]realsamples[i]real + samples[i]imagsamples[i]imag)16 FFT data scaling RTDX_write(ampochan x1 sizeof(x1)2) send 128 samples to PC end of infinite loop end of main interrupt void c_int11() ISR output_sample((short)(iobuffer[buffercount])) out from iobuffer iobuffer[buffercount++]=(short)(input_sample()) input to iobuffer if (buffercount gt= PTS) if iobuffer full buffercount = 0 reinit buffercount flag = 1 reset flag
FFTc C callable FFT function in C define PTS 256 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 do num_stages +=1 i = i2 while (i=N) leg_diff = N2 difference between upperamplower legs step = 512N step between values in twiddleh for (i = 0i lt num_stages i++) for N-point FFT index = 0
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 63
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 64
Spectrogram_RTDXm For spectrogram plot using RTDX with MATLAB clc ccsboardinfo board info cc=ccsdsp(boardnum0) set up CCS object reset(cc) reset board visible(cc1) for CCS window enable(ccrtdx) enable RTDX if ~isenabled(ccrtdx) error(RTDX is not enabled) end ccrtdxset(timeout50) set 50sec timeout for RTDX open(ccspectrogram1pjt) open CCS project load(ccdebugspectrogram1out) load executable file run(cc) run program configure(ccrtdx20481) configure one RTDX channel open(ccrtdxochanr) open output channel pause(3) wait for RTDX channel to open enable(ccrtdxochan) enable channel from DSK isenabled(ccrtdxochan) M = 256 window size N = round(M2) B = 128 No of blocks (128) fs = 8000 sampling rate t=(1B)(Mfs) spectrogram axes generation f=((0(M-1)2)(M-1))fs
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 65
set(gcfDoubleBufferon) y = ones(NB) column = 1 set(gcaNextPlotadd) axes_handle = get(gcfCurrentAxes) set(get(axes_handleXLabel)StringTime (s)) set(get(axes_handleYLabel)StringFrequency (Hz)) set(get(axes_handleTitle)Stringfontnametimesbf Real-Time Spectrogram) set(gcaXLim [0 4096]) set(gcaYLim [0 4000]) set(gcaXLimModemanual) set(gcaYLimModemanual) for i = 132768 w=readmsg(ccrtdxochansingle) read FFT data from DSK w=double(w(1N)) y( column) = w imagesc(tfdB(y)) plot spectrogram column = mod(column B) + 1 end halt(cc) halt processor close(ccrtdxochan) close channel clear cc clear object
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 66
Procedure 15 Create a new project with name spectrogrampjt 16 Open ldquospectrogramcdbrdquo from given CD and save it in your new
project folder 17 Copy the following files from the CD to your new project folder
1) c6713dskinit c 2) FFTc 3) spectrogram_rtdx_mtlc 4) c6713dskinit h 5) hammingcof 6) spectrogram_RTDXm
18 Add ldquospectrogramcdbrdquo ldquoc6713dskinitcrdquo and ldquospectrogram_rtdx_mtlcrdquo to the
current project 19 Add the library file dsk6713bsllib to the current project
Path $ CCCStudioC6000dsk6713libdsk6713bsllib 5 Set the following compiler options Select Project $$$$ Build options Select the following for compiler option with Basic ( for category)
(1) c671xmv6710 (for target version) (2) Full symbolic debug (for Generate Debug info) (3) Speed most critical(for Opt Speed vs Size) (4) None (for Opt Level and Program Level Opt)
Select The Preprocessor Category and Type for Define Symbolsd CHIP_6713 and from Feedback category select for Interlisting OPT C and ASM-s 6 Open ldquospectrogramcdbrdquo select InputOutput $RTDX Right-click on properties
and change RTDX buffer size to 8200 Within CCS select Tools $ RTDX $ configure to set the host buffer size to 2048 (from1024)
7 Build project 7 Close CCS 8 Open MATLAB and Run spectrogram_RTDXm within MATLAB CCS will enable RTDX and will load and run the COFF(out) executable file Then MATLAB will plot the spectrogram of an input signal
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 67
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 68
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 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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 69
5 Compute (N 2) eight sample DFTs from the four sample DFTs
See Shuffled Inputs
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
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
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 70
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 71
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 72
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 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++)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 73
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 74
Input
Output
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 75
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 Project$add files to project pull down menu
Add the linker command file hellocmdrdquo Add the rts file ldquorts6700librdquo
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 76
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 77
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 78
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) samples[i]real=00
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 79
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)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 80
int index step indexstep through twiddle constant 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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 81
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 Project$add files to project pull down menu
Add the linker command file ldquohellocmdrdquo Add the rts file rts6700lib
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 Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 82
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 83
MINI PROJECT (DISCRETE COSINE TRANSFORM)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 84
The Discrete Cosine Transform Introduction
The discrete cosine transform (DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with respect to the images visual quality) The DCT is similar to the discrete Fourier transform it transforms a signal or image from the spatial domain to the frequency domain With an input image A the coefficients for the output image B are
The input image is N2 pixels wide by N1 pixels high A(ij) is the intensity of the pixel in row i and column j B(k1k2) is the DCT coefficient in row k1 and column k2 of the DCT matrix All DCT multiplications are real This lowers the number of required multiplications as compared to the discrete Fourier transform The DCT input is an 8 by 8 array of integers This array contains each pixels gray scale level 8 bit pixels have levels from 0 to 255 The output array of DCT coefficients contains integers these can range from -1024 to 1023 For most images much of the signal energy lies at low frequencies these appear in the upper left corner of the DCT The lower right values represent higher frequencies and are often small - small enough to be neglected with little visible distortion
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 85
IMPLEMENTATION OF DCT
DCT-based codecs use a two-dimensional version of the transform The 2-D DCT and its inverse (IDCT) of an N x N block are shown below
2-D DCT
2-D IDCT
One of the properties of the 2-D DCT is that it is separable meaning that it can be separated into a pair of 1-D DCTs To obtain the 2-D DCT of a block a 1-D DCT is first performed on the rows of the block then a 1-D DCT is performed on the columns of the resulting block
The same applies to the IDCT This process is illustrated below
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
y
N
x Nvy
NuxyxfvCuC
NvuF ππ
sumsumminus
=
minus
=
++=1
0
1
0]
2)12(cos[]
2)12(cos[)()()(2)(
N
v
N
u Nvy
NuxvuFvCuC
Nyxf ππ
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 86
Precalculate the DCT coefficients and scale them
Note that the values in the decimal format does not include the factor [Sqrt(2n)=gtsqrt(28)=gt12] The reason for this is that dividing the coefficients by 2 before converting to Q12 may result in some loss in precision More precision can be obtained by ignoring this division and then multiplying by 211 (instead of 212 ) to convert to Q12 The reason for using Q12 instead of Q15 (as one would expect) is as follows Referring to Equation [3] we notice that the DCT calculation involves a summation of N terms Looking at the DCT coefficients in Figure 1 we observe that the terms entering in the summation may be close to 1 Thus such summation may cause overflows To avoid this each term must be scaled down by 1N For N=8 this can be achieved by working in Q12 format instead of Q15 format
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 87
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 imagepjtrdquo
From the File Menu new DSPBIOS Configuration select dsk6713cdb and save it as image1cdb and add it to the project
Add the source files mainc rdquodctcrdquo and ldquoidctcrdquo in the project using Project$add files to project pull down menu
Add the linker command file MY_LNK_CMDcmd to your project Copy the header files ldquoscenaryhrdquo and ldquodcthrdquo and paste it in your project folder 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 image utility
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 88
Mainc include ltstdiohgt include ltstdlibhgt include dcth include scenaryh Header file containing input image as a 1D array pragma DATA_SECTION (image_inext_sdram) pragma DATA_SECTION (image_outext_sdram) 1D array to hold output image unsigned char image_out[IMAGE_SIZE] 1D array to hold the current block short block[BLOCK_SIZE] Q12 DCT coefficients (actual coefficient x 2^12 ) const short coe[8][8]= 4096 4096 4096 4096 4096 4096 4096 4096 5681 4816 3218 1130 -1130 -3218 -4816 -5681 5352 2217 -2217 -5352 -5352 -2217 2217 5352 4816 -1130 -5681 -3218 3218 5681 1130 -4816 4096 -4096 -4096 4096 4096 -4096 -4096 4096 3218 -5681 1130 4816 -4816 -1130 5681 -3218 2217 -5352 5352 -2217 -2217 5352 -5352 2217 1130 -3218 4816 -5681 5681 -4816 3218 -1130 extern void dct(void) extern void idct(void) void main() int ix Perform block by block processing for(i=0iltIMAGE_SIZEi+=BLOCK_SIZE) Get the block from the input image for(x=0xltBLOCK_SIZE++x) block[x] = (short) image_in[i+x] Perform DCT on this block dct() Perform IDCT on this block idct()
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 89
Store block to output image for(x=0xltBLOCK_SIZE++x) if(block[x]lt0) image_out[i+x]=(unsigned char) (-block[x]) Quick fix for errors occuring due to negative a values occuring after IDCT else image_out[i+x]=(unsigned char) block[x] for () Wait
dctc dctc Function to perform a 8 point 2D DCT DCT is performed using direct matrix multiplication include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void dct(void) int ijxy int value[8] Perform 1D DCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y]=0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 90
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D DCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[y][x]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
idctc idctc performs a 8 point 2D Inverse DCT function include dcth extern unsigned char image_in[IMAGE_SIZE] extern unsigned char image_out[IMAGE_SIZE] extern short block[BLOCK_SIZE] extern const short coe[8][8] void idct(void) int ijxy int value[8] Perform 1D IDCT on the columns for(j=0jlt8j++) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[j+(x8)]) for(y=0ylt8++y)
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 91
block[j+(y8)] = (short)(value[y]gtgt12) Perform 1D IDCT on the resulting rows for(i=0ilt64i+=8) for(y=0ylt8++y) value[y] = 0 for(x=0xlt8++x) value[y] += (int)(coe[x][y]block[i+x]) for(y=0ylt8++y) block[i+y] = (short)(value[y]gtgt15)
MY_LNK_CMDcmd -l imagecfgcmd SECTIONS mydata gt SDRAM mycode gt SDRAM ext_sdram gt SDRAM
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-
Cranes Software Int Ltd TI-Solutions
Copyright copy 2005 CSIL 92
image_in
image_out
- TMS320C6713 DSK
- Package Contents
- TMS320C6713 DSP Features
- INSTALLATION
- DSK HARDWARE INSTALLATION
- Circular Convolution
- Steps for Cyclic Convolution
- 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])
- TMS320C6713 DSK CODEC(TLV320AIC23) Configuration Using Board Support Library
- 30 Discussion on Fundamentals
- Advance Discrete Time Filter Design(FIR)
- Advance Discrete Time Filter Design(IIR)
- IIR filter Designing Experiments
-
- F1 Flowchart for implementing IIR filter
-
- The DFT Equation
-
- Twiddle Factor
- Why the FFT
-
- FFT Algorithm
- The Discrete Cosine Transform
-
- Introduction
-