自适应滤波
10.利用DSP 实现自适应滤波
自适应滤波不仅能够选择信号,而且能够控制信号的特性。自适应滤波器具有跟踪信号和噪声变化的能力,它的系数能够被一种自适应算法所修改。利用DSP 可以实时地对信号进行自适应滤波。DSP 利用直接存储器访问方式DMA 采集数据时不打扰CPU ,因此CPU 可以对信号进行实时地滤波。本设计要求利用DSP 的DMA 方式进行信号采集和信号输出,同时对外部输入的信号进行数字滤波。
1.设计要求及目标
基本部分:
(1)对DMA 进行初始化;
(2)对A/D、D/A进行初始化;
(3)编写DMA 通道传输程序,实现数据实时采集和实时地输出;
(1)设计子自适应滤波算法,或调用DSPLIB 中的自适应函数,实现对信号的自适应滤
波。
发挥部分:
(1)滤波后信号实时输出的同时,将数据存放在数据文件中;
(2)利用自适应滤波实现语音信号回波对消。
2.设计思路
首先利用DSP 的DMA 方式对外部信号进行实时采集,外部模拟信号先进行A/D转换,利用MCBSP 的接收寄存器接收数据。编写自适应滤波算法程序,或调用DSPLIB 中的lms 滤波函数,对信号进行自适应滤波。滤波后信号存放数据区满发出中断,请求信号输出。利用DMA 方式将传输输出数据,经D/A转换后输出。
3.要求完成的任务
(1)编写C 语言程序,并在CCS 集成开发环境下调试通过;
(2)利用信号发生器产生输入信号,经DSP 运算后正确地在示波器上显示;
(3)按要求撰写设计报告。
4.参考文献
[1] 高海林 钱满义 DSP 技术及其应用 自编讲义,2005
[2] 谭浩强 C 程序设计(第二版)清华大学出版社,2003
[3] Paulo S.R.Diniz 著,刘郁林等译. 自适应滤波算法与实现(第二版). 电子工业出版社2004
[4] TMS320C54x DSP CPU and Peripherals. Texas Instrument Inc,2001.
[5] TMS320C54x DSP Applications and Guide. Texas Instrument Inc.2001.
dlms
Function Descriptions 4-39
Adaptive Delayed LMS Filter
Function ushort oflag = dlms (DATA *x, DATA *h, DATA *r, DATA *des, DATA *dbuffer,DATA step, ushort nh, ushort nx) (defined in dlms.asm)
Arguments x[nx] Pointer to input vector of size nx
h[nh] Pointer to filter coefficient vector of size nh.
_ h is stored in reversed order : h(n−1) ... h(0) where h[n] is at the lowest memory address. _ Memory alignment: h is a circular buffer and must start in a k-bit boundary(that is, the k LSBs of the starting address must be zeros) where k = log2(nh)
r[nx] Pointer to output data vector of size nx. r can be equal to
x. des[nx] Pointer to expected output array
dbuffer[nh+2] Pointer to the delay buffer structure.
The delay buffer is a structure comprised of an index register and a circular buffer of length nh + 1. The index register is the index into the circular buffer of the oldest data sample. nh Number of filter coefficients. Filter order = nh − 1.nh >= 3
nx Length of input and output data vectors
oflag Overflow flag.
_ If oflag = 1, a 32-bit overflow has occurred
_ If oflag = 0, a 32-bit overflow has not occurred Description Adaptive delayed least-mean-square (LMS) FIR filter using coefficients stored in vector h. Coefficients are updated after each sample based on the LMS algorithm and using a constant step = 2*μ. The real data input is stored in vector dbuffer. The filter output result is stored in vector r .
LMS algorithm uses the previous error and the previous sample (delayed) to
take advantage of the C55x LMS instruction.
dlms
The delay buffer used is the same delay buffer used for other functions in the C55x DSP
Library. There is one more data location in the circular delay buffer than there are coefficients. Other C55x DSP Library functions use this delay buffer to accommodate use of the dual-MAC architecture. In the DLMS function,we make use of the additional delay slot to allow coefficient updating as well as FIR calculation without a need to update the circular buffer in the interim operations. The FIR output calculation is based on x(i) through x(i−nh+1). The coefficient update for a delayed LMS is based on x(i−1) through x(i−nh). Therefore, by having a delay buffer of nh+1, we can perform all calculations with the given delay buffer containing delay values of x(i) through x(i−nh). If the delay buffer was of length nh, the oldest data sample, x(i−nh), would need to be updated with the newest data sample, x(i), sometime after the calculation of the first coefficient update term, but before the calculation of the last FIR term.
Algorithm FIR portion
Overflow Handling Methodology No scaling implemented for overflow prevention. Special Requirements Minimum of 2 input and desired data samples. Minimum of 2 coefficients
Implementation Notes
_ Delayed version implemented to take advantage of the C55x LMS instruction. _ Effect of using delayed error signal on convergence minimum:
For reference, the following is the algorithm for the regular LMS (nondelayed): FIR portion
r [j ] _
nh _1
k _0 _
h [k ] * x [i _k ] 0_ i _ nx _1
Adaptation using the current error and the current sample:
e (i ) _ des (i ) _r (i )
h k (i _1) _ h k (i ) _2 * _ * e (i ) * x (i _k )
dlmsfast
Function Descriptions 4-41
Example See examples/dlms subdirectory
Benchmarks (preliminary)
Cycles † Core: nx * (7 + 2*(nh − 1)) = nx * (5 + 2 * nh)
Overhead: 26
Code size
(in bytes)
122
† Assumes all data is in on-chip dual-access RAM and that there is no bus conflict due to twiddle table reads and instruction fetches (provided linker command file reflects those conditions).
7.2 Adaptive Filtering (LMS)
Some applications for adaptive FIR (finite impulse response) and IIR (infinite impulse response) filtering include echo and acoustic noise cancellation. In
these applications, an adaptive filter tracks changing conditions in the environment. Although in theory, both FIR and IIR structures can be used as adaptive
filters, stability problems and the local optimum points of IIR filters makes them less attractive for this use. Therefore, FIR filters are typically used for practical adaptive filter applications. The least mean square (LMS), local block-repeat, and parallel instructions on the C55x DSP can be used to efficiently implement adaptive filters. The block diagram of an adaptive FIR filter is shown in
Figure 7−2.
Figure 7−2. Adaptive FIR Filter Implemented With the
Least-Mean-Squares (LMS) Algorithm
y(n)
x(n)
z −1 z −1 z −1
LMS
b 0 b 1 b N−1
_ _
Desired
response
d(n)
+
−
+ +
+
Two common algorithms employed for least mean squares adaptation are the
non-delayed LMS and the delayed LMS algorithm. When compared to nondelayed LMS, the more widely used delayed LMS algorithm has the advantage
of greater computational efficiency at the expense of slightly relaxed convergence properties. Therefore, section 7.2.1 describes only the delayed LMS
algorithm.
Adaptive Filtering (LMS)
Application-Specific Instructions 7-7
7.2.1 Delayed LMS Algorithm
In the delayed LMS, the convolution is performed to compute the output of the adaptive filter:
____ _
___
___ _
___ _ _____
where
y = Filter output
n = Sample index
k = Delay index
N = Number of filter taps
b k = Adaptive coefficient
x = Filter input data value
The value of the error is computed and stored to be used in the next invocation: $___ _ %________
where
e = Error
d = Desired response
y = Actual response (filter output)
The coefficients are updated based on an error value computed in the previous invocation of the algorithm ( is the conversion constant):
___ _____ _ ___ ________$_ ______ _______
The delayed LMS algorithm can be implemented with the LMS instruction— lms(Xmem, Ymem, ACx, ACy)—which performs a multiply-and-accumulate (MAC) operation and, in parallel, an addition with rounding:
ACy = ACy + (Xmem * Ymem),
ACx = rnd(ACx + (Xmem
Adaptive Filtering (LMS)
7-8
The input operands of the multiplier are the content of data memory operand Xmem, sign extended to 17 bits, and the content of data memory operand Ymem, sign extended to 17 bits. One possible implementation would assign the following roles to the operands of the LMS instruction:
Operand(s) Description
Xmem This operand points to the coefficient array.
Ymem This operand points to the data array.
ACx ACx is one of the four accumulators (AC0−AC3). ACx is used to
update the coefficients.
ACy ACy is one of the four accumulators (AC0−AC3) but is not the
same accumulator as ACx. ACy holds the output of the FIR filter.
An efficient implementation of the delayed LMS algorithm is available in the C55x DSP function library (see Chapter 8). NO TAG shows the kernel of this implementation.
Adaptive Filtering (LMS)
Application-Specific Instructions 7-9
Example 7−2. Delayed LMS Implementation of an Adaptive Filter ; ar_data: index in the delay buffer
; ar_input: pointer to input vector
; ar_coef: pointer to coefficient vector
StartSample:
; Clear AC0 for initial error term
MOV #0, AC1
||RPTBLOCAL OuterLoop−1
MOV *ar_input+, *ar_data+ ;copy input −> state(0) ; Place error term in T3
MOV HI(AC1), T3
; Place first update term in AC0
;...while clearing FIR value
MPYM *ar_data+, T3, AC0
||MOV #0, AC1
;AC0 = update coef
;AC1 = start of FIR output
LMS *ar_coef, *ar_data, AC0, AC1
||RPTLOCAL InnerLoop−1
MOV HI(AC0), *ar_coef+
||MPYM *ar_data+, T3, AC0
;AC0 = update coef
;AC1 = update of FIR output
LMS *ar_coef, *ar_data, AC0, AC1
InnerLoop:
; Store Calculated Output
MOV HI(AC0), *ar_coef+
||MOV rnd(HI(AC1)), *ar_output+
; AC2 is error amount
; Point to oldest data sample
SUB AC1, *ar_des+
||AMAR *ar_data+
; Place updated mu_error term in AC1
AMPYMR T_step, AC2, AC1
OuterLoop:
1. 找一个说话的语音,加噪,然后再同时滤回声和高频噪声
2. FFT 加闪烁 或 变调加闪烁
AIC32采样率48K ,每点间间隔1/48000,期望输出噪声频率8000,sin 函数法失败,采用查表法