Lab 5 Report: Đại học Duy Tân Khoa Điện - Điện tử

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Đại học Duy Tân

Khoa Điện – Điện tử

Digital Signal Processing

LAB 5 REPORT
Generate and calculate DFT of a periodic signal
Using TMS320C5515 eZDSPTM USB Stick
Lab 5 report 1

Table of Contents
Table of Contents ........................................................... 1

Abstract .......................................................................... 2

Introduction .................................................................... 3

Matlab Implementation ................................................. 4

C5515 Implementation ................................................... 7

Software generation ..................................................... 16

Compare the results and Conclusions .......................... 18


Lab 5 report 2

Abstract
This report outlines steps to generate and calculate
128 - DFT of a given signal using TMS320C5515
eZDSPTM USB Stick Development Tool. The report
includes a Matlab-based demonstration and
hardware-based programming. The two outcomes
are also analyzed and compared.
Lab 5 report 3

Introduction
In mathematics, the discrete Fourier transform (DFT) is a specific kind of Fourier transform,
used in Fourier analysis. It transforms one function into another, which is called the
frequency domain representation, or simply the DFT, of the original function (which is often
a function in the time domain). In DFT, both input and output are discrete (i.e. discrete in
time domain and frequency domain).

The input to the DFT is a finite sequence of real or complex numbers , making the DFT ideal
for processing information stored in computers.

The sequence of N complex numbers is transformed into an N-periodic


sequence of complex numbers:

The DFT can be computed efficiently in practice using a fast Fourier transform (FFT)
algorithm.

FFT algorithms are so commonly employed to compute DFTs that the term "FFT" is often
used to mean "DFT" in colloquial settings. Formally, there is a clear distinction: "DFT" refers
to a mathematical transformation or function, regardless of how it is computed, whereas
"FFT" refers to a specific family of algorithms for computing DFTs.

In this lab session, we will generate and calculate 128-DFT of the following signal:

x(t)=-2 cos(400πt) + cos(200πt) - 1.5cos(600πt)

Using Matlab, TMS320C5515 eZDSPTM USB Stick Development Tool. The signal is also
generated by VIRTIN Multi-Instrument 3.2. Then, the results of Matlab and C5515 are
compared.
Lab 5 report 4

Matlab implementation

I)FLOW CHART

Start

Generate x(t)

Sketch x(t)

Take 128-DFT

Sketch X(f)

Save signal data

End
Lab 5 report 5

II)MATLAB CODES
clc;
close all;
clear all;

% generate 128 signal samples with Fs=1000Hz


t=0:0.001:0.127;
x=-2*cos(400*pi*t) + cos(200*pi*t) - 1.5*cos(600*pi*t);

%sketch the signal


figure(1);
plot(t,x);
ylabel('Amplitude');
xlabel('Time');

%take 128-FFT of the given signal


X=fft(x,128);
X=fftshift(X);

%sketch the FFT


f = (-64:63)*(1000/128);
figure(2);
stem(f,abs(X));
ylabel('Amplitude');
xlabel('Frequency');

%store the signal data


samples = round(x);
temp = zeros(1,256);
for i = 0:127
temp(1+2*i) = samples(1+i);
end
samples = temp;
id = fopen('D:\samples3.bin','wb');
fwrite(id, samples, 'int16');
fclose(id);
Lab 5 report 6

We have the follwong results:

Signal result:

FFT result:

The main frequencies ( 100 Hz, 200 Hz, 300 Hz) are shown correctly.
Lab 5 report 7

C5515 implementation
I)FLOW CHART

Start

Generate x(t)

Sketch x(t)

Take 128-DFT

Sketch X(f)

Save signal data

End
Lab 5 report 8

II)SOURCE CODES

1. Signal generation

Flowchart:

Start

set Fs=48Khz

set f1,f2,f3

calculate x(t) samples in double

multiply 1333 to the samples for greater-than-1 values

Convert them into intergers

Play the signal

End

We only modify the aic3204_tone_headphone module:

#include "stdio.h"
#include "usbstk5515.h"
extern Int16 AIC3204_rset( Uint16 regnum, Uint16 regval);
#define XmitL 0x10
#define XmitR 0x20

#define Fs 48000
#define F1 200
#define F2 100
#define F3 300

#define PI 3.141592654

#include "math.h"
/* ------------------------------------------------------------------------ *
* *
Lab 5 report 9

* AIC3204 Tone *
* Output a 1 kHz tone through the HEADPHONE jack *
* *
* ------------------------------------------------------------------------ */
Int16 aic3204_tone_headphone( )
{
double * signal;
short * ptsig;
int x;
int nsample = (Fs) / F2;

signal = (double*) malloc( nsample * sizeof(double));

for ( x = 0; x < nsample; x++)


{
signal[x] = 1333*( -2*cos (2*PI*x*F1/Fs) + cos(2*PI*x*F2/Fs) -
1.5*cos(2*PI*x*F3/Fs) );
}
ptsig = (short*) malloc( nsample * sizeof(short));
for ( x = 0; x < nsample; x++)
{
ptsig[x] = (short) signal[x];
}

/* Configure AIC3204 */
AIC3204_rset( 0, 0 ); // Select page 0
AIC3204_rset( 1, 1 ); // Reset codec
AIC3204_rset( 0, 1 ); // Select page 1
AIC3204_rset( 1, 8 ); // Disable crude AVDD generation from DVDD
AIC3204_rset( 2, 1 ); // Enable Analog Blocks, use LDO power
AIC3204_rset( 0, 0 );
/* PLL and Clocks config and Power Up */
AIC3204_rset( 27, 0x0d ); // BCLK and WCLK are set as o/p; AIC3204(Master)
AIC3204_rset( 28, 0x00 ); // Data ofset = 0
AIC3204_rset( 4, 3 ); // PLL setting: PLLCLK <- MCLK, CODEC_CLKIN <-PLL
CLK
AIC3204_rset( 6, 7 ); // PLL setting: J=7
AIC3204_rset( 7, 0x06 ); // PLL setting: HI_BYTE(D=1680)
AIC3204_rset( 8, 0x90 ); // PLL setting: LO_BYTE(D=1680)
AIC3204_rset( 30, 0x88 ); // For 32 bit clocks per frame in Master mode ONLY
// BCLK=DAC_CLK/N =(12288000/8) = 1.536MHz = 32*fs
AIC3204_rset( 5, 0x91 ); // PLL setting: Power up PLL, P=1 and R=1
AIC3204_rset( 13, 0 ); // Hi_Byte(DOSR) for DOSR = 128 decimal or 0x0080
DAC oversamppling
AIC3204_rset( 14, 0x80 ); // Lo_Byte(DOSR) for DOSR = 128 decimal or 0x0080
AIC3204_rset( 20, 0x80 ); // AOSR for AOSR = 128 decimal or 0x0080 for
decimation filters 1 to 6
AIC3204_rset( 11, 0x82 ); // Power up NDAC and set NDAC value to 2
AIC3204_rset( 12, 0x87 ); // Power up MDAC and set MDAC value to 7
AIC3204_rset( 18, 0x87 ); // Power up NADC and set NADC value to 7
AIC3204_rset( 19, 0x82 ); // Power up MADC and set MADC value to 2
/* DAC ROUTING and Power Up */
AIC3204_rset( 0, 1 ); // Select page 1
AIC3204_rset( 0x0c, 8 ); // LDAC AFIR routed to HPL
AIC3204_rset( 0x0d, 8 ); // RDAC AFIR routed to HPR
AIC3204_rset( 0, 0 ); // Select page 0
AIC3204_rset( 64, 2 ); // Left vol=right vol
Lab 5 report 10

AIC3204_rset( 65, 0); // Left DAC gain to 0dB VOL; Right tracks Left
AIC3204_rset( 63, 0xd4 ); // Power up left,right data paths and set channel
AIC3204_rset( 0, 1 ); // Select page 1
AIC3204_rset( 0x10, 0x00 );// Unmute HPL , 0dB gain
AIC3204_rset( 0x11, 0x00 );// Unmute HPR , 0dB gain
AIC3204_rset( 9, 0x30 ); // Power up HPL,HPR
AIC3204_rset( 0, 0 ); // Select page 0
USBSTK5515_wait( 100 ); // wait
/* ADC ROUTING and Power Up */
AIC3204_rset( 0, 1 ); // Select page 1
AIC3204_rset( 0x34, 0x30 );// STEREO 1 Jack
// IN2_L to LADC_P through 40 kohm
AIC3204_rset( 0x37, 0x30 );// IN2_R to RADC_P through 40 kohmm
AIC3204_rset( 0x36, 3 ); // CM_1 (common mode) to LADC_M through 40 kohm
AIC3204_rset( 0x39, 0xc0 );// CM_1 (common mode) to RADC_M through 40 kohm
AIC3204_rset( 0x3b, 0 ); // MIC_PGA_L unmute
AIC3204_rset( 0x3c, 0 ); // MIC_PGA_R unmute
AIC3204_rset( 0, 0 ); // Select page 0
AIC3204_rset( 0x51, 0xc0 );// Powerup Left and Right ADC
AIC3204_rset( 0x52, 0 ); // Unmute Left and Right ADC

AIC3204_rset( 0, 0 );
USBSTK5515_wait( 200 ); // Wait

/* I2S settings */
I2S0_SRGR = 0x0;
I2S0_CR = 0x8010; // 16-bit word, slave, enable I2C
I2S0_ICMR = 0x3f; // Enable interrupts

/* Play Tone */
for ( i = 0 ; i < 1000 ; i++ )
{
for ( j = 0 ; j < 1000 ; j++ )
{
for ( sample = 0 ; sample < nsample ; sample++ )
{
while((XmitR & I2S0_IR) == 0); // Wait for transmit
interrupt to be pending

I2S0_W0_MSW_W = (ptsig[sample]) ; // 16 bit left channel transmit


audio data
I2S0_W1_MSW_W = (ptsig[sample]) ; // 16 bit right channel
transmit audio data
}
}
}
/* Disable I2S */
I2S0_CR = 0x00;

return 0;
}
Lab 5 report 11

The fllowing result is shown on Oscilliscope( Soundcard Scope):

Signal Sketch:

It has the similar shape of the wave generated by Matlab.


Lab 5 report 12

Frequency domain sketch:

The main frequencies are also clearly shown on the graph.


Lab 5 report 13

2. FFT implementation:
Flow chart:

Start

Load the samples (generated by Matlab)

calculate 128-FFT

Save the result

Show the result on


Matlab

End

Source code:
#include <stdio.h>
#include <math.h>
#include <tms320.h>
#include <dsplib.h>
#include "usbstk5515.h"
#define NX 128

Uint16 ImportFile( Uint32 pixel, Uint16 *p_buffer_data )


{
FILE *fp ;
Uint16 data , pdata[2] ;
Uint32 i ;
fp = fopen ("D:\\samples3.bin","rb") ;
if ( fp == (FILE*)NULL)
{
printf(" Error : can't open file_in \n") ;
return 1 ;// check error
}
for( i = 0 ; i < pixel; i++ ){
fread(pdata, 1, 2, fp);
data = *(pdata) | ((*(pdata+1) ) << 8);
p_buffer_data[i] = data ;
}
Lab 5 report 14

fclose ( fp ) ;
return 0 ;
}

Uint16 ExportFile( Uint32 pixel, Uint16 *p_buffer_data )


{
FILE *fp ;
Uint32 i ;
fp = fopen ("D:\\FFT4.bin","wb" ) ;
if ( fp == (FILE*) NULL)
{
printf(" Error : can't open file_in \n") ;

return 1 ;// check error


}
for (i = 0; i < pixel; i++ )
{
fputc(p_buffer_data[i] & 0xFF, fp);
fputc(p_buffer_data[i] >> 8, fp);
}
fclose(fp) ;
return 0 ;
}

void main()
{
int i;
DATA x[2*NX], *px = x ;
printf(" Import samples3.bin \n ") ;
ImportFile( 2*NX , (Uint16*)px ) ;
printf(" Processing \n ") ;
cfft(x,NX, NOSCALE);
cbrev(x,x,NX);
printf(" Export FFT4.bin \n ") ;
ExportFile(2 * NX, (Uint16*)px ) ;
printf(" Done \n ") ;
}

After the FFT is calculated, matlab used this data to sketch FFT using the following codes:

clc;
close all;
clear all;
id=fopen('D:\FFT4.bin','rb');
fft_board = fread(id,256,'int16','ieee-be');
fclose(id);
fft_board = fft_board';
new = zeros(1,128);
for k=0:127
new(k+1) = fft_board(1+2*k) + 1i*fft_board(2+2*k);
%new(k+1) = fft_board(1+2*k) + 0*fft_board(2+2*k);
end
f = (-64:63)*(1000/128);
f1 = (-128:127)*(1000/256);
abs_new = abs(fftshift(new));
figure(1);
stem(f,abs_new)
Lab 5 report 15

The result is:

Software generation
Using Multitones function of the Signal Generator in VIRTIN Multi-Instrument 3.2, we
generated the above signal:
Lab 5 report 16

However, when we looped back the signal using C5515, the shape is changed like this:
Lab 5 report 17

Even so, the main frequencies are still the same:

Therefore, we suppose there must be changes in the amplitudes of the frequencies.


Lab 5 report 18

Compare the results and Conclusions


Here are the waves given by C5515 and Matlab (respectively) :

As previously stated, they have the same shape.


Lab 5 report 19

Here are the 128-FFT done by C5515 and Matlab( respectively) :

They are almost the same.


Lab 5 report 20

To sum up, in this lab session, we have learnt to create samples of a given signal with a
given Fs. This gives us a better understanding of how an arbitrary signal is sampled.
The results, however, is not identical, especially the shape of the signal when we use C5515 to
sample the given signal. This phenonmena, in our opinion, is the effect of the sampling frequency
and some “invisible” filters which have changed the amplitudes of the frequencies.

We also learned that the generated signal voltage cannot reach 3V ( with our C5515, the signal is
bent when the value exceed ~2.5 V). We then figure out a certain number to multiple with the
given signal to maintain the right shape of the signal sketched on oscilloscope.

The sampling rate is also important. With different sampling rates, we got different results, even the
main frequencies are changed. The reason could be with inappropriate sampling frequency, a sine
wave is sampled at different positions in its different cycles.
Lab 5 report 21

References
[1] Texas Instruments, C5515 eZDSP USB Stick Development Tool description and features,
http://www.ti.com/tool/tmdx5515ezdsp, retrieved on February, 27th 2014.
[2] sensorasia, TMS320C5515_eZdip_USB stick.MP4,
http://www.youtube.com/watch?v=ZFnvH1iZoY8, retrieved on February, 27th 2014.
[3] Sophocles J. Orfanidis, Introduction to Signal Processing, Pearson Education, .Inc, New
Jersey, 2009.
Illustrating images of C5515 eZDSP USB Stick Development Tool are taken from
http://www.ti.com.

All source codes in this report are taken from the usbstk5515_v1 library associated with
C5515 eZDSP USB Stick Development Tool, provided by Spectrum Digital Inc..

You might also like