1. Experiment
3A - Quantization of Sinusoid Signals
2. Experiment
3B - Quantization of Speech Signals
3. Experiment
3C - Overflow and Saturation Arithmetic
4. Experiment
3D - Quantization of Coefficients
5. Experiment
3C - Function Approximation in Fixed-point Number Representation
The purpose of the
experiments in this section are to learn input quantization effects and to
determine the proper fixed-point representation for a fixed-point DSP system.
Experiment 3B - Quantization of Speech Signals
Speech is one of most used
signals in DSP applications, from cell phone to MP3 player. To understand the
quztization effects to the speech signals, this experiment uses a digitized
speech file timit1.asc
as input for demonstration. The function exp3b.c for this experiment is
listed below.
Table E3-2 List of C function for
Experiment 3B.
/* ---------------------------------------------------------------
exp3b.c
Quantization effect
1. Use timit1.asc as input file
2. Add the format line to timit1.asc for CCS probe point
Example:
1651 2 c4 1 1
^ ^ ^ ^ ^
| | | | |_ one data a time
| | | |___ data page
| | |_____ variable address (it may change in your case)
| |________ deciaml formated data
|____________ magic number
3. Set probe points on each C statement inside the for loop
4. Connect input file, timit1.asc to the 1st statement
5. Connect 4 output files to the 4 statements
6. After obtained output files, remove the format header from each file
------------------------------------------------------------------- */
#define FILELENGTH 27956 /* # of sample of input file timit1.asc */
int indata,out16,out12,out8,out4;
void main(void)
{
int i;
for(i = 0; i < FILELENGTH; i++)
{
out16 = indata&0xffff; /* Direct output to simulate a 16-bit A/D */
out12 = indata&0xfff0; /* Direct output to simulate a 12-bit A/D */
out8 = indata&0xff00; /* Direct output to simulate an 8-bit A/D */
out4 = indata&0xf000; /* Direct output to simulate a 4-bit A/D */
}
}
To conduct Experiment 3B,
following these steps:
1. Create
the project, exp3b and include
the linker command file exp3.cmd ,
the function exp3b() .
2. Use
rts55.lib for main() function initialization and
build the project.
3. Build
the project. Debug is needed.
4. Use
the File I/O capability of the CCS to connect the speech data file timit1.asc
to your project using probe points.
5. After
quantizing the speech file to 12-bit, 8-bit, and 4-bit, using File I/O to save
the quantized speech files to disk. Then, using MATLAB or other programs to
listen to the original speech file and comparing it with three quantized speech
files at 12, 8, and 4 bits.
Back to Top
Experiment 3C - Overflow and Saturation Arithmetic
Overflow may occur in
fixed-point DSP system when the number is larger than the register or memory
can handle. In this experiment, we use assembly routine ovf_sat.asm
to demonstrate the overflow and overflow handling. The function exp3c.c for this experiment and the assembly routine ovf_sat.asm are listed in Table E3-3 and Table
E3-4, respectively.
Table E3-3 List of C function for
Experiment 3C.
/* --------------------------------------------
exp3c.c
Understand overflow
----------------------------------------------- */
#define BUF_SIZE 40
int sineTable[BUF_SIZE]={
0x0000,0x000f,0x001e,0x002d,0x003a,0x0046,0x0050,0x0059,
0x005f,0x0062,0x0063,0x0062,0x005f,0x0059,0x0050,0x0046,
0x003a,0x002d,0x001e,0x000f,0x0000,0xfff1,0xffe2,0xffd3,
0xffc6,0xffba,0xffb0,0xffa7,0xffa1,0xff9e,0xff9d,0xff9e,
0xffa1,0xffa7,0xffb0,0xffba,0xffc6,0xffd3,0xffe2,0xfff1};
extern int ovftest(int, int *);
void main()
{
int ovrflow_flag;
int *ptr=sineTable;
while(1)
{
ovrflow_flag=0;
ovrflow_flag=ovftest(ovrflow_flag, ptr);
if (ovrflow_flag != 0)
ovrflow_flag=ovftest(ovrflow_flag, ptr);
}
}
Table E3-4 List of the overfolw and
saturation demo routine for Experiment 3C.
;
; ovf_sat.asm
;
; perform add and sub with and without overflow protection
;
.def _ovftest
.bss buff,(0x100)
.bss buff1,(0x100)
;
; Code start
;
_ovftest
bclr SATD ; Clear saturation bit if set
xcc start,T0!=#0 ; If T0!=0, set saturation bit
bset SATD
start
pshboth XAR5 ; Save XAR5
mov #0,AC0
amov #buff,XAR5 ; Set buffer pointer
rpt #0x100-1 ; Clear buff
mov AC0,*AR5+
amov #buff1,XAR5 ; Set buffer pointer
rpt #0x100-1 ; Clear buff1
mov AC0,*AR5+
mov #0x80-1,BRC0 ; Initialize loop counts for addition
amov #buff+0x80,XAR5 ; Initialize buffer pointer
rptblocal add_loop_end-1
add #0x140<<#16,AC0 ; Use upper AC0 as a ramp up counter
mov hi(AC0),*AR5+ ; Save the counter to buffer
add_loop_end
mov #0x80-1,BRC0 ; Initialize loop counts for subtraction
mov #0,AC0
amov #buff+0x7f,XAR5 ; Initialize buffer pointer
rptblocal sub_loop_end-1
sub #0x140<<#16,AC0 ; Use upper AC0 as a ramp down counter
mov hi(AC0),*AR5- ; Save the counter to buffer
sub_loop_end
mov #0x100-1,BRC0 ; Initialize loop counts for sinewave
amov #buff1,XAR5 ; Initialize buffer pointer
mov mmap(@AR0),BSA01; Initialize base register
mov #40,BK03 ; Set buffer as size 40
mov #20,AR0 ; Start with an offset of 20 samples
bset AR0LC ; Active circular buffer
rptblocal sine_loop_end-1
mov *ar0+<<#16,AC0 ; Get sine value into high AC0
sfts AC0,#9 ; Scale the sine value
mov hi(AC0),*AR5+ ; Save scaled value
sine_loop_end
mov #0,T0 ; Return 0 if no overflow
xcc set_ovf_flag,overflow(AC0)
mov #1,T0 ; Return 1 if overflow detected
set_ovf_flag
bclr AR0LC ; Reset circilar buffer bit
bclr SATD ; Reset saturation bit
popboth XAR5 ; Restore AR5
ret
.end
To conduct Experiment 3C,
following these steps:
1. Create
the project, exp3c and include
the linker command file exp3.cmd ,
the function exp3c() , and
assembly routine ovf_sat.asm .
2. Use
rts55.lib for main() function initialization and
build the project.
3. Build
the project. Debug is needed.
4. Use
the CCS graphic function to view the ramp counter and sinewave. See Figure E3-2
for reference.
(a)
Without saturation protection - overflow causes the data errors
(b)
with saturation protection - the data saturated when overflow occurs
Figure
E3-2 Examples of overflow and saturation.
Back to Top
Experiment 3D - Quantization of Coefficients
Filters are widely used for
DSP applications. Due to quantization, the filter in fixed-point representation
will no longer the same as the floating-point filter obtained from most filter
design packages. In this experiment, we use assembly routine exp3d_iir.asm
to show the quantization effects to filter coefficients. More filter experiments
will be presented later in Chapter 5 and Chapter 6. The function exp3d.c
for this experiment and the assembly routine exp3d_iir.asm are listed in Table
E3-5 and Table E3-6, respectively.
Table E3-5 List of C function for
Experiment 3D.
/* -------------------------------------------
exp3d.c
Quantization effect to an IIR filter
---------------------------------------------- */
#define FILELENGTH 27956 /* # of sample of input file timit1.asc */
extern int iir4(int);
extern void init_iir4(void);
int indata,outdata;
void main(void)
{
int i;
init_iir4();
for (i = 0; i < FILELENGTH; i++)
outdata = iir4(indata);
}
Table E3-6 List of the IIR filter
routine for Experiment 3D.
;----------------------------------------------
;
; exp3d_IIR.asm
;
; 4th Order IIR filter
;
;----------------------------------------------
MASK .set 0xFFFF
.def _iir4
.def _init_iir4
;
; Original coefficients 0-1800Hz 4th order IIR LPF
; with sampling frequency 8000Hz
;
; int b[5]={0.0072, 0.00287, 0.0431, 0.0287, 0.0072};
; int a[5]={1.0000, -2.16860,2.0097,-0.8766, 0.1505};
;
.data
; Q13 formatted coefficients
;
coeff ; b0, b1, b2, b3, b4
.word 0x003B&MASK, 0x00EB&MASK
.word 0x0161&MASK, 0x00EB&MASK, 0x003B&MASK
; -a1, -a2, -a3, -a4
.word 0x4564&MASK, -0x404F&MASK
.word 0x1C0D&MASK, -0x04D1&MASK
.bss x,5 ; x delay line
.bss y,4 ; y delay line
.text
_init_iir4
pshboth XAR5
amov #x,XAR5
rpt #4
mov #0,*AR5+
amov #y,XAR5
rpt #3
mov #0,*AR5+
popboth XAR5
ret
;
; 4th Order IIR filter
; Entry T0 = sample
; Exit T0 = filtered sample
;
_iir4
pshboth XAR5
pshboth XAR6
bset SATD
bset SXM
amov #x,XAR5
amov #y,XAR6
amov #coeff,XCDP
bset FRCT
|| mov T0,*AR5 ; x[0] = indata
;
; perform IIR filter
;
mpym *AR5+,*CDP+,AC0 ; AC0=x[0]*bn[0]
|| rpt #3 ; i=1,2,3,4
macm *AR5+,*CDP+,AC0 ; AC0+=x[i]*bn[i]
rpt #3 ; i=0,1,2,3
macm *AR6+,*CDP+,AC0 ; AC0+=y[i]*an[i]
amov #y+2,XAR5
amov #y+3,XAR6
sfts AC0,#2 ; Scale to Q15 format
|| rpt #2
mov *AR5-,*AR6- ; Update y[]
mov hi(AC0),*AR6
|| mov hi(AC0),T0 ; return y[0] in T0
amov #x+3,XAR5
amov #x+4,XAR6
bclr FRCT
|| rpt #3
mov *AR5-,*AR6- ; Update x[]
popboth XAR6
popboth XAR5
bclr SXM
bclr SATD
|| ret
.end
To conduct Experiment 3D,
following these steps:
1. Create
the project, exp3d and include
the linker command file exp3.cmd ,
the function exp3d() , and
assembly routine exp3d_iir.asm .
2. Use
rts55.lib for main() function initialization and
build the project.
3. Build
the project. Debug is needed.
4. Interface
the project with a signal source, such as the speech file timit1.asc . Process the data and save
the filter output.
5. Listen
the IIR filte output file generated with different coefficient wordlengths.
Back to Top
Experiment 3E - Function Approximation in Fixed-point Number
Representation
For DSP applications, we
often use Q-format to represent fractional numbers. We have introduced
quantization and overflow protection in previous experiments. In this
experiment, we will present another useful method - polynomial approximation
with sinusoid function example to further study and understand the fixed-point
arithmetic operation and overflow control. The function exp3e.c for this experiment and
the assembly routine sine_cos.asm
are listed in Table E3-7 and Table E3-8, respectively.
Table E3-7 List of C function for
Experiment 3E.
/* ---------------------------------
exp3e.c
Fixed-point representaion
------------------------------------ */
extern void sine_cos(int, int *);
const int theta[16]={
0x9556,0xa000,0xaaab,0xc000, /* -150, -135, -120, -90 */
0xd555,0xe000,0xeaab,0xffff, /* -60, -45, -30, -0 */
0x1555,0x2000,0x2aaa,0x4000, /* 30, 45, 60, 90 */
0x5555,0x6000,0x6aaa,0x7fff}; /* 120, 135, 150, 180 */
int result_buf[32];
int Wn_buf[2];
void main()
{
int *result, *Wn;
int i;
for (i=0; i<32; i++)
result_buf[i]=0;
result = result_buf;
Wn = Wn_buf;
/* 3rd quadrant angles */
sine_cos(theta[0], Wn); /* -150 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[1], Wn); /* -135 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[2], Wn); /* -120 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[3], Wn); /* -90 */
*result++ = *Wn++;
*result++ = *Wn--;
/* 4th quadrant angles */
sine_cos(theta[4], Wn); /* -60 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[5], Wn); /* -45 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[6], Wn); /* -30 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[7], Wn); /* -0 */
*result++ = *Wn++;
*result++ = *Wn--;
/* 1st quadrant angles */
sine_cos(theta[8], Wn); /* 30 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[9], Wn); /* 45 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[10], Wn); /* 60 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[11], Wn); /* 90 */
*result++ = *Wn++;
*result++ = *Wn--;
/* 2nd quadrant angles */
sine_cos(theta[12], Wn); /* 120 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[13], Wn); /* 135 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[14], Wn); /* 150 */
*result++ = *Wn++;
*result++ = *Wn--;
sine_cos(theta[15], Wn); /* 180 */
*result++ = *Wn++;
*result++ = *Wn--;
}
Table E3-8 List of the sine-cosine generator
for Experiment 3E.
;
; sine_cos: 16-bit sine(x) and cos(x) approximation function
;
; Entry: T0 = x, in the range of [-pi=0x8000 pi=0x7fff]
; AR0 -> Pointer
; Return: None
; Update:
; AR0->[0] = cos(x) = d0+d1*x+d2*x^2+d3*x^3+d4*x^4+d5*x^5
; AR0->[1] = sin(x) = c1*x+c2*x^2+c3*x^3+c4*x^4+c5*x^5
;
; 36 cycles
;
.def _sine_cos
;
; Approximation coefficients in Q12 (4096) format
;
.data
coeff ; Sine appoximation coefficients
.word 0x3240 ; c1 = 3.140625
.word 0x0053 ; c2 = 0.02026367
.word 0xaacc ; c3 = -5.325196
.word 0x08b7 ; c4 = 0.54467780
.word 0x1cce ; c5 = 1.80029300
; Cosine appoximation coefficients
.word 0x1000 ; d0 = 1.0000
.word 0xfff8 ; d1 = -0.001922133
.word 0xb199 ; d2 = -4.90014738
.word 0xfbc3 ; d3 = -0.2648921
.word 0x50ba ; d4 = 5.0454103
.word 0xe332 ; d5 = -1.800293
;
; Function starts
;
.text
_sine_cos
pshboth XAR5 ; Save AR5
amov #14,AR5
btstp AR5,T0 ; Test bit 15 and 14
;
; Start cos(x)
;
amov #coeff+10,XAR5 ; Pointer to the end of coefficients
xcc _neg_x,TC1
neg T0 ; Negate if bit 14 is set
_neg_x
and #0x7fff,T0 ; Mask out sign bit
mov *AR5-<<#16,AC0 ; AC0 = d5
|| bset SATD ; Set Saturate bit
mov *AR5-<<#16,AC1 ; AC1 = d4
|| bset FRCT ; Set up fractional bit
mac AC0,T0,AC1 ; AC1 = (d5*x+c4)
|| mov *AR5-<<#16,AC0 ; AC0 = d3
mac AC1,T0,AC0 ; AC0 = (d5*x^2+d4*x+d3)
|| mov *AR5-<<#16,AC1 ; AC1 = d2
mac AC0,T0,AC1 ; AC1 = (d5*x^3+d4*x^2+d3*x+d2)
|| mov *AR5-<<#16,AC0 ; AC0 = d1
mac AC1,T0,AC0 ; AC0 = (d5*x^4+d4*x^3+d3*x^2+d2*x+d1)
|| mov *AR5-<<#16,AC1 ; AC1 = d0
macr AC0,T0,AC1 ; AC1 = (d5*x^4+d4*x^3+d3*x^2+d2*x+d1)*x+d0
|| xcc _neg_result1,TC2
neg AC1
_neg_result1
mov *AR5-<<#16,AC0 ; AC0 = c5
|| xcc _neg_result2,TC1
neg AC1
_neg_result2
mov hi(saturate(AC1<<#3)),*AR0+ ; return cos(x) in Q15
;
; Start sin(x) computation
;
mov *AR5-<<#16,AC1 ; AC1 = c4
mac AC0,T0,AC1 ; AC1 = (c5*x+c4)
|| mov *AR5-<<#16,AC0 ; AC0 = c3
mac AC1,T0,AC0 ; AC0 = (c5*x^2+c4*x+c3)
|| mov *AR5-<<#16,AC1 ; AC1 = c2
mac AC0,T0,AC1 ; AC1 = (c5*x^3+c4*x^2+c3*x+c2)
|| mov *AR5-<<#16,AC0 ; AC0 = c1
mac AC1,T0,AC0 ; AC0 = (c5*x^4+c4*x^3+c3*x^2+c2*x+c1)
|| popboth XAR5 ; Restore AR5
mpyr T0,AC0,AC1 ; AC1 = (c5*x^4+c4*x^3+c3*x^2+c2*x+c1)*x
|| xcc _neg_result3,TC2
neg AC1
_neg_result3
mov hi(saturate(AC1<<#3)),*AR0- ; return sin(x) in Q15
|| bclr FRCT ; Reset fractional bit
bclr SATD ; Reset saturate bit
ret
.end
To conduct Experiment 3D,
following these steps:
1. Create
the project, exp3e and include
the linker command file exp3.cmd ,
the function exp3e() , and
assembly routine sine_cos.asm .
2. Use
rts55.lib for main() function initialization and
build the project.
3. Build
the project. Debug is needed.
4. Calculate
the values of sin(q ) and cos(q ) functions for 0<q <2p .
Back to Top