摘 要:自適應(yīng)濾波器用于實(shí)現(xiàn)對(duì)未知系統(tǒng)的建模,用Matlab中的Simulink對(duì)LMS算法的實(shí)現(xiàn)方法進(jìn)行仿真,在FPGA中實(shí)現(xiàn)了LMS算法及其建模,并對(duì)FPGA設(shè)計(jì)的系統(tǒng)建模結(jié)果采用Matlab軟件仿真,以增強(qiáng)Quartus的仿真功能,從而得到完整且直觀的仿真結(jié)果。這種系統(tǒng)建模所采用的仿真、實(shí)現(xiàn)和驗(yàn)證方法同樣適用于消除寬帶信號(hào)中的窄帶干擾,實(shí)現(xiàn)自適應(yīng)譜線增強(qiáng)以及自適應(yīng)均衡等,具有一定通用性。
關(guān)鍵詞:FPGA;自適應(yīng)濾波;LMS算法;系統(tǒng)建模;Matlab仿真
中圖分類號(hào):TN713文獻(xiàn)標(biāo)識(shí)碼:A
文章編號(hào):1004-373X(2010)02-076-04
System Modeling Based on FPGA and LMS Arithmetic
LIU Yan
(The 802 Research Institute,Shanghai Academy of Spaceflight Technology,Shanghai,200090,China)
Abstract:The realization method of LMS arithmetic is simulated by SIMULINK in Matlab,the LMS arithmetic is realized by FPGA,and system modeling by self_adaptive filter and FPGA,the emulational function of Quartus are enhanced by Matlab,and the result is rounded and intuitionistic.The method of emulation,realization and validation to the system modeling is the same with the elimination to narrow_band disturb in wideband,the realization to self_adaptive equalization etc,and it has a certain universal sence.
Keywords:FPGA;self_adaptive filter;LMS arithmetic;system modeling;Matlab simulation
0 引 言
自適應(yīng)濾波器用于系統(tǒng)建模是其重要應(yīng)用之一,即自適應(yīng)濾波器作為估計(jì)未知系統(tǒng)特性的模型,對(duì)于自適應(yīng)濾波器,IIR和FIR兩種形式都可以考慮,而FIR濾波器是實(shí)際應(yīng)用較廣泛的。FIR濾波器只有可調(diào)的零點(diǎn),因此它沒有IIR因兼有可調(diào)的零點(diǎn)和極點(diǎn)而帶來(lái)的不穩(wěn)定問題,另外,LMS計(jì)算量小,比較容易進(jìn)行硬件實(shí)現(xiàn),所以這里所設(shè)計(jì)的自適應(yīng)濾波器是在FIR的基礎(chǔ)上構(gòu)建的LMS自適應(yīng)濾波器[1,2]。
1 自適應(yīng)濾波器實(shí)現(xiàn)及系數(shù)調(diào)整LMS算法
自適應(yīng)濾波器的結(jié)構(gòu)是具有可調(diào)系數(shù)的直接型或格型FIR濾波器,因此,系數(shù)調(diào)節(jié)準(zhǔn)則必須優(yōu)化且可實(shí)現(xiàn)。
LMS(最小均方)算法是個(gè)最陡下降方法,自適應(yīng)濾波器按照如下LMS系數(shù)遞推公式不斷修正濾波器系數(shù),使得待估計(jì)未知系統(tǒng)的輸出與自適應(yīng)濾波器之差的均方值達(dá)到最小[1,3]:
hn(k)=hn-1(k)+Δ*e(n)*x(n-k)
式中:hn(k)為此時(shí)刻的第k個(gè)抽頭的系數(shù);hn-1(k)為前一時(shí)刻的第k個(gè)抽頭的系數(shù);Δ為調(diào)節(jié)算法的步進(jìn);誤差e(n)=d(n)-x(n),其中d(n)為待估計(jì)未知系統(tǒng)的輸出,x(n)為輸入信號(hào);x(n-k)為輸入延遲。
2 LMS算法實(shí)現(xiàn)方法的仿真
根據(jù)自適應(yīng)濾波器實(shí)現(xiàn)及系數(shù)調(diào)整LMS算法的原理,用格型濾波實(shí)現(xiàn)濾波單元,格型濾波單元的實(shí)現(xiàn)如圖1所示,將多個(gè)格型濾波單元級(jí)聯(lián)實(shí)現(xiàn)高階濾波。
圖1 格型濾波單元的實(shí)現(xiàn)
當(dāng)輸入信號(hào)為迭加了隨機(jī)信號(hào)的正弦波,8個(gè)格型濾波單元級(jí)聯(lián)時(shí),LMS算法的Simulink仿真見圖2。
圖2 LMS算法的輸入和輸出
從圖2的仿真結(jié)果可以看出,該LMS算法實(shí)現(xiàn)了高階濾波,也即圖1所示的格型濾波單元結(jié)構(gòu)可用于實(shí)現(xiàn)LMS算法。
3 系統(tǒng)實(shí)現(xiàn)方法
用系數(shù)按LMS算法變化的8階FIR對(duì)未知的FIR系統(tǒng)進(jìn)行建模的系統(tǒng)框圖如圖3所示。
圖3 用FPGA和LMS算法實(shí)現(xiàn)系統(tǒng)建模的框圖
圖3中,未知FIR系統(tǒng)(只知道輸出不知道參數(shù)),自適應(yīng)FIR濾波器為圖1所示的格型結(jié)構(gòu),具有四個(gè)可調(diào)節(jié)參數(shù)h1,h2,h3,h4,在FPGA中用原理圖實(shí)現(xiàn),F(xiàn)IR設(shè)計(jì)方法在許多資料中有詳細(xì)介紹,這里不再贅述[4-7]。LMS算法在FPGA中采用原理圖和VHDL相結(jié)合的方法實(shí)現(xiàn),下面對(duì)LMS算法模塊做重點(diǎn)介紹。
用LMS算法計(jì)算權(quán)值系數(shù)的頂層框圖如4所示,其基本結(jié)構(gòu)為圖1所示的格型濾波結(jié)構(gòu),該模塊以待估計(jì)未知系統(tǒng)的輸出與自適應(yīng)濾波器之差為輸入信號(hào),輸出為不斷修正的濾波器系數(shù)h1,h2,h3,h4,同時(shí)作為自適應(yīng)FIR濾波器的可調(diào)節(jié)系數(shù)。
為了計(jì)算方便,設(shè)調(diào)節(jié)算法的步進(jìn)Δ=1/1 024,這樣只需要對(duì)待估計(jì)未知系統(tǒng)的輸出與自適應(yīng)濾波器之差進(jìn)行移位運(yùn)算,即可以實(shí)現(xiàn)與步進(jìn)Δ的乘法,從而避免了待估計(jì)未知系統(tǒng)的輸出與自適應(yīng)濾波器之差與小數(shù)(步進(jìn)Δ)做乘法的運(yùn)算,減少了乘法運(yùn)算產(chǎn)生的舍入誤差且節(jié)約了大量的FPGA資源,提高了計(jì)算速度[2]。
各主要模塊的VHDL語(yǔ)言描述如下[8-10]:
(1) 移位寄存器模塊
library IEEE;
use IEEE.STD_LOGIC-1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity shift is
port (
clk:in std_logic;
datain:in std_logic_vector(11 downto 0);
A,B,C,D:out std_logic_vector(11 downto 0));
end shift;
architecture Behavioral of shift is
signal temp1:std_logic_vector(11 downto 0);
signal temp2:std_logic_vector(11 downto 0);
signal temp3:std_logic_vector(11 downto 0);
signal temp4:std_logic_vector(11 downto 0);
begin
process(clk)
begin
if clk′event and clk=′1′ then
temp1(11 downto 0)<=datain(11 downto 0);
temp2(11 downto 0)<=temp1(11 downto 0);
temp3(11 downto 0)<=temp2(11 downto 0);
temp4(11 downto 0)<=temp3(11 downto 0);
end if;
end process;
A<=temp1;
B<=temp2;
C<=temp3;
D<=temp4;
end Behavioral;
圖4 用LMS算法計(jì)算權(quán)值系數(shù)的頂層框圖
(2) 移位乘法器模塊
library IEEE;
use IEEE.STD_LOGIC-1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity shift10 is
port (
clk:in std_logic;
datain:in std_logic_vector(11 downto 0);
dataout:out std_logic_vector(11 downto 0));
end shift10;
architecture Behavioral of shift10 is
__右移10位(除以1 024),即步長(zhǎng)為1/1 024
begin
process(clk)
begin
if clk′event and clk=′1′ then
dataout(1 downto 0)<=datain(11 downto 10);
dataout(11 downto 2)<=\"0000000000\";
end if;
end process;
end Behavioral;
(3) 累加器模塊
library ieee;
use ieee.std_logic-1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity jia is
port( clk:in std_logic;
h_temp:in std_logic_vector(11 downto 0);
fankui:inout std_logic_vector(11 downto 0);
adder:inout std_logic_vector(11 downto 0);
h:out std_logic_vector(11 downto 0));
end entity jia;
architecture behave of jia is
signal temp:std_logic_vector(11 downto 0);
begin
process(clk)
begin
temp<=h_temp;
if clk′event and clk=′1′ then
adder<=temp+fankui;
fankui<=adder;
end if;
h<=adder;
end process;
end architecture behave;
4 實(shí)驗(yàn)和仿真結(jié)果
將Quartus對(duì)FPGA頂層系統(tǒng)建模文件的仿真結(jié)果另存為.TBL文件,用Matlab讀出該仿真結(jié)果,需要注意的是對(duì)FPGA生成的.TBL文件的最后一位要改成數(shù)字,最前面段英文要?jiǎng)h除,其Matlab程序如下:
clc;
clear all;
fid=fopen(′top.tbl′,′r′);%讀2位16進(jìn)制數(shù)
s=fscanf(fid,′%s′,1);%文件讀指針指向第一個(gè)字符
i=1;
N=3;%輸出的16進(jìn)制數(shù)據(jù)位數(shù)
while feof(fid)~=1
s1=s(1,1:length(s)-1);%s1為時(shí)間字符,該字符最后一位為>,必須除去(即-1)
time=str2num(s1);%將CHAR轉(zhuǎn)為NUM
clk_temp=fscanf(fid,′%s′,1);%指向下一個(gè)符號(hào)
clr_temp=fscanf(fid,′%s′,1);%指向下一個(gè)符號(hào)
in_temp=fscanf(fid,′%s′,1);%指向下一個(gè)符號(hào)
equal=fscanf(fid,′%s′,1);%跳過一個(gè)符號(hào),s指向\"=\"
test=mod(time,500); %以500為周期對(duì)數(shù)據(jù)
取樣,剔除野值
fir8out_temp=fscanf(fid,′%s′,1);%fid指向輸出第一個(gè)數(shù)據(jù)
fir8lms_temp=fscanf(fid,′%s′,1);%fid指向輸出第二個(gè)數(shù)據(jù)
if (equal== ′=′)(test==0)%剔除周期以外的野值,并確認(rèn)數(shù)據(jù)之前為\"=\"
fir8out(i)=hex2dec(fir8out_temp(1,1:N));%將16進(jìn)制數(shù)轉(zhuǎn)為10進(jìn)制數(shù)
fir8out_jilu(i)=fir8out(i);
if fir8out(i)>=(2^(4*N-1))%負(fù)數(shù)補(bǔ)碼轉(zhuǎn)為10進(jìn)制
fir8out(i)=-(2^(4*N))+fir8out(i);
end
fir8lms(i)=hex2dec(fir8lms_temp(1,1:N));%將16進(jìn)制數(shù)轉(zhuǎn)為10進(jìn)制數(shù)
fir8lms_jilu(i)=fir8lms(i);
if fir8lms(i)>=(2^(4*N-1))%負(fù)數(shù)補(bǔ)碼轉(zhuǎn)為10進(jìn)制
fir8lms(i)=-(2^(4*N))+fir8lms(i);
end
tt(i)=time/1000;%t單位ns,轉(zhuǎn)換為μs
clk(i)=str2num(clk_temp);
in(i)=hex2dec(in_temp(1,1:N));%將16進(jìn)制數(shù)轉(zhuǎn)為10進(jìn)制數(shù)
if in(i)>=(2^(4*N-1))%負(fù)數(shù)補(bǔ)碼轉(zhuǎn)為10進(jìn)制
in(i)=-(2^(4*N))+in(i);
end
error=fir8out_jilu_fir8lms_jilu;
i=i+1;
end
s=fscanf(fid,′%s′,1);%指向下一行第一個(gè)符號(hào)
end
fclose(fid);
subplot(5,1,1),plot(clk,′k′);grid on;
subplot(5,1,2),plot(in,′k′);grid on;%sin+noise
subplot(5,1,4),plot(fir8out,′k′);grid on;
subplot(5,1,3),plot(fir8lms,′k′);grid on;
subplot(5,1,5),plot(error,′k′);grid on;
值得注意的是,為解決結(jié)果數(shù)據(jù)毛刺太多而引起的.TBL文件過大使以上Matlab程序讀出數(shù)據(jù)速度太慢的問題,可以在Quartus仿真時(shí)用功能仿真代替時(shí)序仿真。具體方法是:在波形文件.VWF的Assignments菜單中選擇FUNCTIONAL,確定后再Processing菜單中選擇Generate Functional Simulation Netlist生成功能仿真網(wǎng)表后再開始仿真,這樣產(chǎn)生的結(jié)果數(shù)據(jù)基本沒有野值產(chǎn)生的毛刺。
用以上程序讀出的時(shí)鐘、輸入信號(hào)、未知系統(tǒng)輸出、自適應(yīng)濾波器的輸出及誤差信號(hào)如圖5所示,其中,輸入信號(hào)同樣為迭加了隨機(jī)信號(hào)的正弦波。
圖5 時(shí)鐘、輸入、未知系統(tǒng)輸出、
自適應(yīng)濾波器的輸出及誤差信號(hào)
從圖5的結(jié)果可以看出,自適應(yīng)濾波器在經(jīng)過開始的一段振蕩后,輸出逐漸與未知系統(tǒng)輸出一致,二者的誤差逐漸減小到接近零,即實(shí)現(xiàn)了無(wú)差跟蹤,可見,該自適應(yīng)濾波器實(shí)現(xiàn)了對(duì)未知系統(tǒng)的跟蹤和建模,且有相當(dāng)快的收斂速度。
5 結(jié) 語(yǔ)
本文用Matlab中的Simulink對(duì)LMS算法的實(shí)現(xiàn)方法進(jìn)行了仿真,并在FPGA中實(shí)現(xiàn)了LMS算法,進(jìn)而實(shí)現(xiàn)了在FPGA中用自適應(yīng)濾波器對(duì)未知系統(tǒng)的建模,并對(duì)FPGA設(shè)計(jì)的系統(tǒng)建模結(jié)果用Matlab軟件仿真以增強(qiáng)Quartus的仿真功能,從而得到完整而且直觀的仿真結(jié)果,這種系統(tǒng)建模所采用的仿真、實(shí)現(xiàn)和驗(yàn)證方法同樣適用于消除寬帶信號(hào)中的窄帶干擾、實(shí)現(xiàn)自適應(yīng)譜線增強(qiáng)以及自適應(yīng)均衡等[1],具有一定的通用性。
參考文獻(xiàn)
[1]陳懷琛.數(shù)字信號(hào)處理及其Matlab實(shí)現(xiàn)[M].北京:電子工業(yè)出版社,1998.
[2]楊躍忠,闕沛文,李亮.自適應(yīng)LMS濾波器在FPGA中的實(shí)現(xiàn)[J].微計(jì)算機(jī)信息,2006,22(11):158 -160.
[3]潘松.EDA技術(shù)使用教程[M].北京:科學(xué)出版社,1999.
[4]孫耀奇,高火濤,熊超,等.基于Matlab和FPGA的FIR數(shù)字濾波器設(shè)計(jì)及實(shí)現(xiàn)[J].現(xiàn)代電子技術(shù),2008,31(11):89-92.
[5]郭繼昌,向暉,滕建輔,等.基于FPGA的FIR濾波器的實(shí)現(xiàn)[J].電子技術(shù)應(yīng)用,2000,26(5):60-62.
[6]李明緯,黃世震.應(yīng)用分布式算法在FPGA平臺(tái)實(shí)現(xiàn)FIR低通濾波器[J].中國(guó)集成電路,2007,20(2):90-92.
[7]陳炳權(quán).基于FPGA的FIR濾波器FFT算法與DA算法實(shí)現(xiàn)[J].襄樊學(xué)院學(xué)報(bào),2005,22(2):54-56.
[8]宋立業(yè),王景勝,彭繼慎.自適應(yīng)濾波器的算法研究及DSP仿真實(shí)現(xiàn)[J].現(xiàn)代電子技術(shù),2008,31(5):112-114.
[9]胡廣書.數(shù)字信號(hào)處理[M].北京:清華大學(xué)出版社,2003.
[10]褚振勇,翁木云.FPGA設(shè)計(jì)及應(yīng)用[M].西安:西安電子科技大學(xué)出版社,2002.
作者簡(jiǎn)介 劉 艷 女,1976年出生,陜西寶雞人,碩士研究生,助理工程師。研究方向?yàn)閿?shù)字信號(hào)處理。