19
DSP C5000 DSP C5000 Chapter 16 Chapter 16 Adaptive Filter Adaptive Filter Implementation Implementation Copyright © 2003 Texas Instruments. All rights reserve Copyright © 2003 Texas Instruments. All rights reserve

DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Embed Size (px)

Citation preview

Page 1: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

DSP C5000DSP C5000

Chapter 16Chapter 16

Adaptive Filter ImplementationAdaptive Filter Implementation

Copyright © 2003 Texas Instruments. All rights reserved.Copyright © 2003 Texas Instruments. All rights reserved.

Page 2: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 2

OutlineOutline

Adaptive filters and LMS algorithmAdaptive filters and LMS algorithm

Implementation of FIR filters on C54xImplementation of FIR filters on C54x

Implementation of FIR filters on C55xImplementation of FIR filters on C55x

Page 3: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 3

Generalities of Adaptive FiltersGeneralities of Adaptive Filters The coefficients of Adaptive filters are The coefficients of Adaptive filters are

variable with time in order to optimize variable with time in order to optimize a given criterion.a given criterion.

The most commonly used algorithm to The most commonly used algorithm to adapt the coefficients is the adapt the coefficients is the LMSLMS (Least (Least Mean Square) algorithm.Mean Square) algorithm.

Most adaptive filters are implemented Most adaptive filters are implemented as FIR filters, because they are as FIR filters, because they are inherently stable.inherently stable.

They are widely used in digital They are widely used in digital communications: communications: Echo cancellation, equalizers ...Echo cancellation, equalizers ...

Page 4: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 4

Diagram and Notations of Adaptive FiltersDiagram and Notations of Adaptive Filters

Filter with variable

coefficients

Calculation for adaption of

coefficents

e(n)

r(n)= reference signal

y(n)

-

+ x(n)

New set of coefficients

Error signal

Page 5: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 5

2

1

0

.

Criterion min .

2 ( ) 2 2 .

n n n

n

N

n n i n n i k n k n iki

e r y

J E e

JE e x E r x b E x x

b

Mean Square Error SolutionMean Square Error Solution1

0FIR filter: .

N

n i n ii

y b x

Optimum : x xrR b r

Page 6: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 6

Gradient AlgorithmGradient Algorithm Gradient of criterion J vs b:Gradient of criterion J vs b:

.2 n n i

i

JE e x

b

Gradient algorithm: iterative solutionGradient algorithm: iterative solution

0, 1 ( 1) ( ) .i i n n ii N b n b n E e x

Converges if: Converges if: max

2.

AdaptAdaptatation stepion step

Max Max Eigenvalue Eigenvalue of Rof Rxx

Page 7: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 7

Stochastic Gradient Algorithm: LMSStochastic Gradient Algorithm: LMS

The mean values E(e(n)x(n-i)) are not The mean values E(e(n)x(n-i)) are not known. known. In the stochastic gradient algorithm, they In the stochastic gradient algorithm, they

are replaced by e(n)x(n-i).are replaced by e(n)x(n-i). The algorithm converges if the adaptation The algorithm converges if the adaptation

step is small enough.step is small enough. Algorithm named: LMS (Least Mean Algorithm named: LMS (Least Mean

Square) or Widrow algorithm:Square) or Widrow algorithm:

0, 1 ( 1) ( ) .i i n n ii N b n b n e x

Page 8: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 8

LMS AlgorithmLMS Algorithm For each sample, the LMS algorithm:For each sample, the LMS algorithm:

Filters the input using bFilters the input using bii

Updates the bUpdates the bii coefficients. coefficients.

eenn**xxnn eenn**xxnn--11 eenn**xxnn--NN++11

Input xn

rrnn

eenn

-- yynn

b0

z-1 z-1 z-1

b1

xn xn-1 xn-N+1

Reference

ei = en*xn-i

bN-1

Page 9: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 9

LMS Algorithm at Each Sample TimeLMS Algorithm at Each Sample Time

FIR Filtering equation:FIR Filtering equation:

Coefficient updating equation:Coefficient updating equation:

1

0( ) .

N

n i n ii

y b n x

0, 1 ( 1) ( ) .i i n n ii N b n b n e x

1 1With: .n n ne r y

Page 10: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 10

Fixed Point ImplementationFixed Point Implementation

When LMS adaptive filter is When LMS adaptive filter is implemented on a fixed point DSP:implemented on a fixed point DSP: The precision of calculation is important:The precision of calculation is important:

If rnd(eIf rnd(eii*x*xn-in-i) is smaller than the used precision, ) is smaller than the used precision, no adaptno adaptatation is performed.ion is performed.

The precision of convergence depends The precision of convergence depends on:on: AdaptAdaptatation step ion step : the largest : the largest , the fastest , the fastest

convergence but the worst precision.convergence but the worst precision. The number of coefficients N: the residual The number of coefficients N: the residual

error is proportional to N. error is proportional to N. When N is too large, it may be worth When N is too large, it may be worth

considering using block adaptive filtering.considering using block adaptive filtering.

Page 11: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 11

LMS stepsLMS steps

Each time iteration (only once):Each time iteration (only once): Calculates error eCalculates error enn =r =rnn-y-ynn

Scale error by adScale error by adaappttation step ation step : e’: e’nn= = eenn..

Each time iteration, for each coefficient:Each time iteration, for each coefficient: Multiply error with signal: eMultiply error with signal: ei i = e’= e’nnxxn-in-i

Multiply xMultiply xn-in-ibbii and accumulate and accumulate Calculate new coefficients: newbCalculate new coefficients: newbi i = b= bii+e+eii

Update coefficients: bUpdate coefficients: bi i = newb= newbii..

Page 12: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 12

Implementing LMS Algorithm on C54xImplementing LMS Algorithm on C54x

LMSLMS specific instruction to realize: specific instruction to realize: Filter and coefficient updating.Filter and coefficient updating. B = B + (bB = B + (bii*x*xn-in-i)); ; A = rnd(eA = rnd(eii+b+bii)) Rounding is important because Rounding is important because may be very may be very

small.small.

Filter and coefficients updating equations:Filter and coefficients updating equations: To be done at each sample time n:To be done at each sample time n:

1

0( ) .

N

n i n ii

y b n x

0, 1 ( 1) ( ) .i i n n ii N b n b n e x

1 1.n n ne r y

Page 13: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 13

LMS instructionLMS instruction

With LMS instruction, LMS FIR:With LMS instruction, LMS FIR: 2N Cycles per tap.2N Cycles per tap.

LMS Xmem, YmemLMS Xmem, Ymem (A) + (Xmem)<<16+2(A) + (Xmem)<<16+21515 AA (B) + (Xmem) x (Ymem) (B) + (Xmem) x (Ymem) B B Uses both ACCUs A and B. Uses both ACCUs A and B. This instruction does not modify T.This instruction does not modify T.

Xmem points on bXmem points on bii, Ymem on x, Ymem on xn-in-i

Data x are stored in a circular buffer.Data x are stored in a circular buffer.

Page 14: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 14

Example of LMS adaptive filter on C54xExample of LMS adaptive filter on C54x

.mmregs .global adr_debut_dat, adr_fin_dat .global adr_debut_coef, adr_fin_coef

N .set 32 adr_debut_dat .usect "buf_data", N adr_debut_coef .usect "buf_coef", N

Defines 2 sections for data and coefficientsDefines 2 sections for data and coefficients

Data Memory

Data Memory

adr_debut_coef AR3

h(0)adr_debut_dat

AR2

h(1) x(n-i)h(2)

Circular buffer

Page 15: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 15

Implementing LMS Algorithm on C54xImplementing LMS Algorithm on C54x

InitializationsInitializations

.text * Initialization of BK, AR0,FRCT

STM #N, BK STM #1, AR0

SSBX FRCT * Initialisation of AR2, AR3, AR0 STM #(adr_debut_dat),AR2 STM #(adr_fin_coef),AR3 STM #1, AR0

Page 16: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 16

Implementing LMS Algorithm on C54xImplementing LMS Algorithm on C54x LMS adaptive filter program:LMS adaptive filter program:

* after calculation of error e = r-y * load T with e

LD erreur, T * initialization of B LD #0,B * Calculation of y(n) and update of coef

STM #N-2, BRC MPY *AR2, A LMS *AR3, *AR2+ RPTB end-1 ST A,*AR3+0%

||MPY *AR2, A LMS *AR3, *AR2+

end STH A,*AR3 STH B, *yn

Page 17: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 17

Implementating LMS algorithm on C55xImplementating LMS algorithm on C55x

LMSLMS specific instruction to realize: specific instruction to realize: Filter and coefficicent updating.Filter and coefficicent updating. ACy = ACy + (bACy = ACy + (bii*x*xn-in-i)); ; ACx = rnd(eACx = rnd(eii+b+bii)) Rounding is important because Rounding is important because may be very may be very

small.small.

Filter and coefficients updating equations:Filter and coefficients updating equations: To be done at each sample time n:To be done at each sample time n:

1

0( ) .

N

n i n ii

y b n x

0, 1 ( 1) ( ) .i i n n ii N b n b n e x

1 1.n n ne r y

Page 18: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 18

… .asg AR3, Coefs .asg AR4, Data MOV @DeltaE,T3 || MOV #(N-2),BRC0 MPYM *Data,T3,AC0 || MOV #0,AC1 MOV port(@X_new),*Data LMS *Coefs,*Data,AC0,AC1 || RPTBLOCAL e1 MOV HI(AC0),*Coefs+ || MPYM *Data+,T3,AC0 e1: LMS *Coefs,*Data,AC0,AC1 MOV HI(AC0),*Coefs || MOV HI(AC1),*Result+

Example of LMS adaptive filter on C55XExample of LMS adaptive filter on C55XPre-calculate Pre-calculate *e(n) ... *e(n) ... . . AR3 pts to coeff table: a[n]AR3 pts to coeff table: a[n]AR4 pts to data table: x[n]AR4 pts to data table: x[n]

T3 holds error step amountT3 holds error step amount... w... while loading BRCOhile loading BRCOAC0=error*oldestsample: x(n-N)AC0=error*oldestsample: x(n-N)……while clearing AC1 (run FIR)while clearing AC1 (run FIR)Overwrite x(n-N) with x(n)Overwrite x(n-N) with x(n)Start FIR, update oldest coef…Start FIR, update oldest coef…

… … and start repeat blockand start repeat blockStore update coefficient ...Store update coefficient ......while calc. next update term...while calc. next update termCalc FIR, update coefficientCalc FIR, update coefficientStore final coefficient...Store final coefficient...……while storing FIR outputwhile storing FIR output

Page 19: DSP C5000 Chapter 16 Adaptive Filter Implementation Copyright © 2003 Texas Instruments. All rights reserved

Copyright © 2003 Texas Instruments. All rights reserved.

ESIEE, Slide 19

Follow on Activities Follow on Activities

Application 10 for TMS320C5416 DSKApplication 10 for TMS320C5416 DSK Implements a guitar tuner using an Implements a guitar tuner using an

adaptive filter. Here the desired note is adaptive filter. Here the desired note is used as the reference. The LEDs on the used as the reference. The LEDs on the DSK indicate when the guitar is tune. DSK indicate when the guitar is tune.