Tài liệu DSP phòng thí nghiệm thử nghiệm bằng cách sử dụng C và DSK TMS320C31 (P7) - Pdf 10

ț Adaptive structures
ț The least mean square (LMS) algorithm
ț Programming examples using C and TMS320C3x code
Adaptive filters are best used in cases where signal conditions or system para-
meters are slowly changing and the filter is to be adjusted to compensate for this
change. The least mean square (LMS) criterion is a search algorithm that can be
used to provide the strategy for adjusting the filter coefficients. Programming
examples are included to give a basic intuitive understanding of adaptive filters.
7.1 INTRODUCTION
In conventional FIR and IIR digital filters, it is assumed that the process para-
meters to determine the filter characteristics are known. They may vary with
time, but the nature of the variation is assumed to be known. In many practical
problems, there may be a large uncertainty in some parameters because of inad-
equate prior test data about the process. Some parameters might be expected to
change with time, but the exact nature of the change is not predictable. In such
cases, it is highly desirable to design the filter to be self-learning, so that it can
adapt itself to the situation at hand.
The coefficients of an adaptive filter are adjusted to compensate for changes
in input signal, output signal, or system parameters. Instead of being rigid, an
adaptive system can learn the signal characteristics and track slow changes. An
adaptive filter can be very useful when there is uncertainty about the character-
istics of a signal or when these characteristics change.
Figure 7.1 shows a basic adaptive filter structure in which the adaptive fil-
ter’s output y is compared with a desired signal d to yield an error signal e,
which is fed back to the adaptive filter. The coefficients of the adaptive filter are
195
7
Adaptive Filters
Digital Signal Processing: Laboratory Experiments Using C and the TMS320C31 DSK
Rulph Chassaing
Copyright © 1999 John Wiley & Sons, Inc.

(n)], where E represents the expected value. Since there are k weights or co-
efficients, a gradient of the mean squared error function is required. An esti-
mate can be found instead using the gradient of e
2
(n), yielding
w
k
(n + 1) = w
k
(n) + 2␤e(n)x(n – k) k = 0, 1, , N – 1 (7.3)
which represents the LMS algorithm [1–3]. Equation (7.3) provides a simple
but powerful and efficient means of updating the weights, or coefficients, with-
out the need for averaging or differentiating, and will be used for implementing
adaptive filters.
196
Adaptive Filters
FIGURE 7.1 Basic adaptive filter structure.
The input to the adaptive filter is x(n), and the rate of convergence and accu-
racy of the adaptation process (adaptive step size) is ␤.
For each specific time n, each coefficient, or weight, w
k
(n) is updated or re-
placed by a new coefficient, based on (7.3), unless the error signal e(n) is zero.
After the filter’s output y(n), the error signal e(n) and each of the coefficients
w
k
(n) are updated for a specific time n, a new sample is acquired (from an
ADC) and the adaptation process is repeated for a different time. Note that from
(7.3), the weights are not updated when e(n) becomes zero.
The linear adaptive combiner is one of the most useful adaptive filter struc-

scheme, the adaptive filter models the unkown system.
3. Additional structures have been implemented such as:
a) Notch with two weights, which can be used to notch or cancel/reduce a si-
nusoidal noise signal. This structure has only two weights or coefficients,
and is illustrated later with a programming example.
b) Adaptive predictor, which can provide an estimate of an input. This struc-
ture is illustrated later with three programming examples.
c) Adaptive channel equalization, used in a modem to reduce channel distor-
tion resulting from the high speed of data transmission over telephone
channels.
The LMS is well suited for a number of applications, including adaptive
echo and noise cancellation, equalization, and prediction.
Other variants of the LMS algorithm have been employed, such as the sign-
error LMS, the sign-data LMS, and the sign-sign LMS.
1. For the sign-error LMS algorithm, (7.3) becomes
w
k
(n + 1) = w
k
(n) + ␤sgn[e(n)]x(n – k) (7.4)
where sgn is the signum function,
1 if u м 0
sgn(u) =
Ά
(7.5)
–1 if u < 0
198
Adaptive Filters
FIGURE 7.3 Adaptive filter structure for system identification.
2. For the sign-data LMS algorithm, (7.3) becomes

The LMS algorithm has been quite useful in adaptive equalizers, telephone
cancellers, and so forth. Other methods such as the recursive least squares
(RLS) algorithm [4], can offer faster convergence than the basic LMS but at the
expense of more computations. The RLS is based on starting with the optimal
solution and then using each input sample to update the impulse response in or-
der to maintain that optimality. The right step size and direction are defined over
each time sample.
Adaptive algorithms for restoring signal properties can also be found in [4].
Such algorithms become useful when an appropriate reference signal is not
available. The filter is adapted in such a way as to restore some property of the
signal lost before reaching the adaptive filter. Instead of the desired waveform
as a template, as in the LMS or RLS algorithms, this property is used for the
adaptation of the filter. When the desired signal is available, the conventional
approach such as the LMS can be used; otherwise a priori knowledge about the
signal is used.
7.3 PROGRAMMING EXAMPLES USING C AND
TMS320C3x CODE
The following programming examples illustrate adaptive filtering using the
least mean square (LMS) algorithm. It is instructive to read the first example
7.2 Adaptive Structures 199
even if you have only a limited knowledge of C, since it illustrates the steps in
the adaptive process.
Example 7.1 Adaptive Filter Using C Code Compiled With
Borland C/C++
This example applies the LMS algorithm using a C-coded program compiled
with Borland C/C++. It illustrates the following steps for the adaptation process
using the adaptive structure in Figure 7.1:
1. Obtain a new sample for each, the desired signal d and the reference input
to the adaptive filter x, which represents a noise signal.
2. Calculate the adaptive FIR filter’s output y, applying (7.1) as in Chapter 4

of 31. Execute this program, enter a ␤ value of 0.01, and verify the results in
Figure 7.6. Note that the output converges to the desired cosine signal. Press F2
to execute this program again with a different beta value.
200
Adaptive Filters
//ADAPTC.C - ADAPTATION USING LMS WITHOUT THE TI COMPILER
#include <stdio.h>
#include <math.h>
#define beta 0.01 //convergence rate
#define N 21 //order of filter
#define NS 40 //number of samples
#define Fs 8000 //sampling frequency
#define pi 3.1415926
#define DESIRED 2*cos(2*pi*T*1000/Fs) //desired signal
#define NOISE sin(2*pi*T*1000/Fs) //noise signal
main()
{
long I, T;
double D, Y, E;
double W[N+1] = {0.0};
double X[N+1] = {0.0};
FILE *desired, *Y_out, *error;
desired = fopen (“DESIRED”, “w++”); //file for desired samples
Y_out = fopen (“Y_OUT”, “w++”); //file for output samples
error = fopen (“ERROR”, “w++”); //file for error samples
for (T = 0; T < NS; T++) //start adaptive algorithm
{
X[0] = NOISE; //new noise sample
D = DESIRED; //desired signal
Y = 0; //filter’output set to zero

Hz. The addition of these two signals is achieved in the program with DPLUSN
for each sample period.
2. The reference input to the adaptive FIR filter is a cosine function with a
frequency of 312 Hz specified by REFNOISE. The adaptation step or rate of
convergence is set to 1.5 × 10
–8
, the number of coefficients to 30, and the num-
ber of output samples to 128.
3. The output of the adaptive FIR filter y is calculated using the convolution
equation (7.1), and converges to the additive noise signal with a frequency of
312 Hz. When this happens, the “error” signal e, calculated from (7.2), ap-
proaches the desired signal d with a frequency of 1 kHz. This error signal is the
overall output of the adaptive filter structure, and is the difference between the
adaptive filter’s output y and the primary input consisting of the desired signal
with additive noise.
In the previous example, the overall output was the adaptive filter’s output.
In that case, the filter’s output converged to the desired signal. For the structure
in this example, the overall output is the error signal and not the adaptive filter’s
output.
This program was compiled with the TMS320 assembly language floating-
point tools, and the executable COFF file is on the accompanying disk. Down-
load and run it on the DSK.
The output can be saved into the file fname with the debugger command
save fname,0x809d00,128,L
which saves the 128 output samples stored in memory starting at the address
809d00 into the file fname, in ASCII Long format. Note that the desired sig-
nal with additive noise samples in DPLUSN are stored in memory starting at the
address 809d80, and can be saved also into a different file with the debugger
save command.
Figure 7.8 shows a plot of the output converging to the 1-kHz desired sine

Delay[T] = 0.0;
}
for (T=0; T < NS; T++) /*# of output samples */
{
Delay[0] = REFNOISE; /*adaptive filter’s input*/
DPLUSN = DESIRED + ADDNOISE; /*desired + noise, d+n */
Y = 0;
for (I = 0; I < N; I++)
Y += (W[I] * Delay[I]); /*adaptive filter output */
E = DPLUSN - Y; /*error signal */
for (I = N; I > 0; I—)
{
W[I] = W[I] + (beta*E*Delay[I]); /*update weights */
if (I != 0)
Delay[I] = Delay[I-1]; /*update samples */
}
*IO_OUTPUT++ = E; /*overall output E */
*IO_INPUT++ = DPLUSN; /* store d + n */
}
}
FIGURE 7.7 Adaptive filter program for sinusoidal noise cancellation using data move
(ADAPTDMV.C).
7.3 Programming Examples Using C and TMS320C3x Code 205
FIGURE 7.8 Plot of overall output of adaptive filter structure converging to 1-kHz desired
signal.
FIGURE 7.9 Output frequency response of adaptive filter structure showing reduction of
312-Hz additive sinusoidal noise.
Examine the effects of different values for the adaptation rate ␤ and for the
number of weights or coefficients.
Example 7.3 Adaptive Predictor Using C Code

{
W[T] = 0.0;
Delay[T] = 0.0;
}
for (T=0; T < NS; T++) /*# of output samples */
{
xin = inp/1000; /*input between 1 and -1 */
if (ys >= xin) /*is signal rising or falling */
x = acos(xin); /*signal is falling */
else /*otherwise */
x=asin(xin)-(pi/2); /*signal is rising */
x = x - (shift); /*shift */
Delay[0]=cos(x); /*shifted output=filter’s input*/
D = inp; /*input data */
Y1 = 0; /*init output */
ys = xin; /*store input value */
for (I=0; I <N; I++) /*for N coefficients */
Y1+=W[I]*Delay[I]; /*adaptive filter output */
E = D - Y1; /*error signal */
for (I=N; I>0; I—)
{
W[I]=W[I]+(beta*E*Delay[I]); /*update weights */
if (I != 0)
Delay[I] = Delay[I-1]; /*update delays */
}
*IO_OUTPUT++ = Y1; /*overall output */
}
}
FIGURE 7.11 (continued)
A shifting technique is employed within the program to obtain a delay of

#define inp 1000*sin(2*pi*T*1000/Fs) /*input */
#include “scdat” /*table for asin, acos */
#include “math.h”
main()
{
int I, J, T, Y;
double E, yo, xin, out_data;
double W[N+1];
double Delay[N+1];
volatile int *IO_OUTPUT = (volatile int*) 0x809d00;
yo=0;
for (T=0; T < N; T++)
{
W[T] = 0.0;
Delay[T] = 0.0;
}
for (T=0; T < NS; T++) /*# of output samples */
{
xin = inp/1000; /*scale for range between 1 and -1*/
Y = ((xin)+1)*100; /*step up array between 0 and 200 */
if (yo > xin) /*is signal falling or rising */
Delay[0] = yc[Y]; /*signal is falling, acos domain */
else /*otherwise */
Delay[0] = ys[Y]; /*signal is rising, asin domain */
out_data = 0; /*init filter output to zero */
yo = xin; /*store input */
for (I=0; I<=N; I++)
out_data +=(W[I]*Delay[I]); /*filter output */
E = xin - out_data; /*error signal */
for (J=N; J > 0; J—)

1
(n)x
1
(n)
210
Adaptive Filters
FIGURE 7.14 Output of adaptive predictor (with table lookup procedure) converging to de-
sired 1-kHz input signal.
FIGURE 7.15 Adaptive notch structure with two weights.
+ w
2
(n)x
2
(n). The error signal e(n) is the difference between the primary input
signal and the adaptive filter’s output, or e(n) = (d + n) – y(n).
Figure 7.16 shows a listing of the program NOTCH2W.ASM, which imple-
ments the two-weight adaptive notch filter structure. Consider the following.
7.3 Programming Examples Using C and TMS320C3x Code 211
;NOTCH2W.ASM - ADAPTIVE NOTCH FILTER WITH TWO WEIGHTS
.start “.text”,0x809900 ;starting address for text
.start “.data”,0x809C00 ;starting address for data
.include “dplusna” ;data file d+n (1000+312 Hz)
.include “cos312a” ;input data x1(n)
.include “sin312a” ;input data x2(n)
DPN_ADDR .word DPLUSN ;d+n sine 100 + 312 Hz
COS_ADDR .word COS312 ;start address of x1(n)
SIN_ADDR .word SIN312 ;start address of x2(n)
OUT_ADDR .word 0x809802 ;output address
SC_ADDR .word SC ;address for sine+cosine samples
WN_ADDR .word COEFF ;address of coefficient w(N-1)

STF R3,*AR1++% ;store sine sample in SC buffer
LDF *AR3++,R4 ;input d+n=sin1000 + sin312->R4
LDI @WN_ADDR,AR0 ;w(N-1) address -> AR0
CALL FILT ;call FIR subroutine FILT
SUBF3 R0,R4,R0 ;error = DPN - Y -> R0
FIX R0,R1 ;convert R0 to integer -> R1
STI R1,*AR4++ ;store error as output
MPYF @BETA,R0 ;R0=error function=beta*error
STF R0,*AR6 ;store error function
LDI @WN_ADDR,AR0 ;w(N-1) address -> AR0
CALL ADAPT ;call adaptation routine
SUBI 1,R5 ;decrement loop counter
BNZ LOOP ;repeat for next sample
WAIT BR WAIT ;wait
;FIR FILTER SUBROUTINE
FILT MPYF3 *AR0++,*AR1++%,R0 ;w1(n)*x1(n) = y1(n) -> R0
LDF 0,R2 ;R2 = 0
MPYF3 *AR0++,*AR1++%,R0 ;w2(n)*x2(n) = y2(n) -> R0
|| ADDF3 R0,R2,R2 ;R2 = y1(n)
ADDF3 R0,R2,R0 ;y1(n)+y2(n) = y(n) -> R0
RETSU ;return from subroutine
;ADAPTATION SUBROUTINE
ADAPT MPYF3 *AR6,*AR1++%,R0 ;error function*x1(n)-> R0
LDF *AR0,R3 ;w1(n) -> R3
MPYF3 *AR6,*AR1++%,R0 ;error function*x2(n)-> R0
|| ADDF3 R3,R0,R2 ;w1(n)+error function*x1(n)->R2
LDF *+AR0,R3 ;w2(n) -> R3
|| STF R2,*AR0++ ;w1(n+1)=w1(n)+error function*x1(n)
ADDF3 R3,R0,R2 ;w2(n)+error function*x2(n)->R2
STF R2,*AR0 ;w2(n+1)=w2(n)+error function*x2(n)

error signal.
5. The filter and adaptation routines are separated in the program in order to
make it easier to follow the program flow. For faster execution, these routines
can be included where they are called. This would eliminate the CALL and
RETS instructions.
6. The LMS algorithm is implemented with equations (7.1)–(7.3) by calcu-
lating first the adaptive filter’s output y(n), followed by the error signal e(n), and
then the two coefficients w
1
(n) and w
2
(n).
The filter subroutine finds y(n) = w
1
(n)x
1
(n) + w
2
(n)x
2
(n), where the coeffi-
cients or weights w
1
(n) and w
2
(n) represent the weights at time n. Chapter 4
contains many examples for implementing FIR filters using TMS320C3x code.
In this case, there are only two coefficients and two input samples. The first in-
put sample to the adaptive FIR filter is the cosine sample x
1

1
(n) from the first multiply instruction.
11. The instruction
LDF *+AR0,R3
loads the second weight w
2
(n) into R3. Note that AR0 is preincremented with-
out modification to the memory address of the second weight.
12. The instruction
|| STF R2,*AR0++
stores the updated weight R2 = w
1
(n + 1) in the memory address specified by
AR0. Then, AR0 is postincremented to point at the address of the second
weight. The second ADDF3 instruction is similar to the first one and updates the
second weight w
2
(n + 1).
For each time n, the preceding steps are repeated. New cosine and sine sam-
ples are acquired, as well as (d + n) samples. For example, the adaptive filter’s
output y(n) is calculated with the newly acquired cosine and sine samples and
the previously updated weights. The error signal is then calculated and stored as
output.
The 128 output samples can be retrieved from memory and saved into a file
n2w with the debugger command
save n2w,0x809802,128,l
The adaptive filter’s output y(n) converges to the additive 312-Hz interfer-
ence noise n. The “error” signal e(n), which is the overall output of this adaptive
filter structure, becomes the desired 1-kHz sine signal d. Figure 7.17 shows a
plot of the output error signal converging to the desired 1-kHz sine signal d. Re-

2. A total of 128 output samples are obtained starting at memory address
7.3 Programming Examples Using C and TMS320C3x Code 215
FIGURE 7.17 Output of adaptive notch filter converging to desired 1-kHz sine signal.
216
Adaptive Filters
;ADAPTP.ASM - ADAPTIVE PREDICTOR
.start “.text”,0x809900 ;starting address for text
.start “.data”,0x809C00 ;starting address for data
.include “sin312a” ;data for sine of 312 Hz
.include “hcos312a” ;data for 1/2 cosine 312 Hz
.data ;data section
D_ADDR .word SIN312 ;desired signal address
HC_ADDR .word HCOS312 ;addr of input to adapt filter
OUT_ADDR .word 0x809802 ;output address
XB_ADDR .word XN+LENGTH-1 ;bottom addr of circular buffer
WN_ADDR .word COEFF ;coefficient address
ERF_ADDR .word ERR_FUNC ;address of error function
ERR_FUNC .float 0 ;init ERR FUNC to zero
BETA .float 1.0E-8 ;rate of adaptation
LENGTH .set 50 ;FIR filter length
NSAMPLE .set 128 ;number of output samples
COEFF .float 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.float 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.brstart “XN_BUFF”,64 ;align samples buffer
XN .sect “XN_BUFF” ;circ buffer for filter samples
.loop LENGTH ;buffer size for samples
.float 0 ;init samples to zero
.endloop ;end of loop
.entry BEGIN ;start of code
.text ;text section

MPYF @BETA,R0 ;R0=ERR FUNC=beta*error
STF R0,*AR6 ;store error function
LDI LENGTH-2,RC ;reset repeat counter
LDI @WN_ADDR,AR0 ;w(N-1) address -> AR0
CALL ADAPT ;call adaptation routine
SUBI 1,R5 ;decrement loop counter
BNZ LOOP ;repeat for next sample
WAIT BR WAIT ;wait
;FIR FILTER SUBROUTINE
FILT LDF 0,R2 ;R2 = 0
RPTS LENGTH-1 ;repeat LENGTH-1 times
MPYF3 *AR0++,*AR1++%,R0 ;w(N-1-i)*x(n-(N-1-i))
|| ADDF3 R0,R2,R2 ;accumulate
ADDF3 R0,R2,R0 ;add last product y(n)->R0
RETSU ;return from subroutine
;ADAPTATION SUBROUTINE
ADAPT MPYF3 *AR6,*AR1++%,R0 ;ERR FUNC*x(n-(N-1))-> R0
LDF *AR0,R3 ;w(N-1) -> R3
RPTB LOOP_END ;repeat LENGTH-2 times
MPYF3 *AR6,*AR1++%,R0 ;ERR FUNC*x(n-(N-1-i))->R0
|| ADDF3 R3,R0,R2 ;w(N-1-i)+ERR FUNC*x(n-(N-1-i))
LOOP_END LDF *+AR0(1),R3 ;load subsequent H(k) -> R3
|| STF R2,*AR0++ ;store/update coefficient
ADDF3 R3,R0,R2 ;w(n+1)=w(n)+ERR FUNC*x(n)
STF R2,*AR0 ;store/update last coefficient
RETSU ;return from subroutine
sine input signal. Reduce the adaptation rate to 10
–10
and verify a slower rate of
adaptation to the 312-Hz sine signal.

NOISE_ADDR .word NOISE+LENGTH-1 ;last address of noise samples
WN_ADDR .word COEFF ;address of coefficients w(N-1)
ERF_ADDR .word ERR_FUNC ;address of error function
ERR_FUNC .float 0 ;initialize error function
BETA .float 2.5E-12 ;rate of adaptation constant
LENGTH .set 50 ;set filter length
COEFF: ;buffer for coefficients
.loop LENGTH ;loop length times
.float 0 ;init coefficients to zero
.endloop ;end of loop
.brstart “XN_BUFF”,128 ;align buffer for noise samples
NOISE .sect “XN_BUFF” ;section for input noise samples
.loop LENGTH ;loop length times
.float 0 ;initialize noise samples
.endloop ;end of loop
.entry BEGIN ;start of code
.text ;assemble into text section
BEGIN LDP WN_ADDR ;init to data page 128
CALL AICSET ;initialize AIC
LDI @ERF_ADDR,AR6 ;error function address ->AR6
LDI LENGTH,BK ;filter length ->BK
LDI @WN_ADDR,AR0 ;coefficient address w(N-1) ->AR0
LDI @NOISE_ADDR,AR1 ;last noise sample address ->AR1
LOOP CALL IOAUX ;get noise sample from AUX IN
FLOAT R6,R3 ;transfer noise sample into R3
STF R3,*AR1++% ;store noise sample-> circ buffer
LDI @WN_ADDR,AR0 ;w(N-1) coefficients address->AR0
FILT LDF 0,R2 ;R2 = 0
RPTS LENGTH-1 ;next 2 instr (LENGTH-1) times
MPYF3 *AR0++,*AR1++%,R0 ;w(N-1-i)*x(n-(N-1-i))


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status