摘 要:自適應濾波器用于實現對未知系統的建模,用Matlab中的Simulink對LMS算法的實現方法進行仿真,在FPGA中實現了LMS算法及其建模,并對FPGA設計的系統建模結果采用Matlab軟件仿真,以增強Quartus的仿真功能,從而得到完整且直觀的仿真結果。這種系統建模所采用的仿真、實現和驗證方法同樣適用于消除寬帶信號中的窄帶干擾,實現自適應譜線增強以及自適應均衡等,具有一定通用性。
關鍵詞:FPGA;自適應濾波;LMS算法;系統建模;Matlab仿真
中圖分類號:TN713文獻標識碼:A
文章編號: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 引 言
自適應濾波器用于系統建模是其重要應用之一,即自適應濾波器作為估計未知系統特性的模型,對于自適應濾波器,IIR和FIR兩種形式都可以考慮,而FIR濾波器是實際應用較廣泛的。FIR濾波器只有可調的零點,因此它沒有IIR因兼有可調的零點和極點而帶來的不穩定問題,另外,LMS計算量小,比較容易進行硬件實現,所以這里所設計的自適應濾波器是在FIR的基礎上構建的LMS自適應濾波器[1,2]。
1 自適應濾波器實現及系數調整LMS算法
自適應濾波器的結構是具有可調系數的直接型或格型FIR濾波器,因此,系數調節準則必須優化且可實現。
LMS(最小均方)算法是個最陡下降方法,自適應濾波器按照如下LMS系數遞推公式不斷修正濾波器系數,使得待估計未知系統的輸出與自適應濾波器之差的均方值達到最小[1,3]:
hn(k)=hn-1(k)+Δ*e(n)*x(n-k)
式中:hn(k)為此時刻的第k個抽頭的系數;hn-1(k)為前一時刻的第k個抽頭的系數;Δ為調節算法的步進;誤差e(n)=d(n)-x(n),其中d(n)為待估計未知系統的輸出,x(n)為輸入信號;x(n-k)為輸入延遲。
2 LMS算法實現方法的仿真
根據自適應濾波器實現及系數調整LMS算法的原理,用格型濾波實現濾波單元,格型濾波單元的實現如圖1所示,將多個格型濾波單元級聯實現高階濾波。
圖1 格型濾波單元的實現
當輸入信號為迭加了隨機信號的正弦波,8個格型濾波單元級聯時,LMS算法的Simulink仿真見圖2。
圖2 LMS算法的輸入和輸出
從圖2的仿真結果可以看出,該LMS算法實現了高階濾波,也即圖1所示的格型濾波單元結構可用于實現LMS算法。
3 系統實現方法
用系數按LMS算法變化的8階FIR對未知的FIR系統進行建模的系統框圖如圖3所示。
圖3 用FPGA和LMS算法實現系統建模的框圖
圖3中,未知FIR系統(只知道輸出不知道參數),自適應FIR濾波器為圖1所示的格型結構,具有四個可調節參數h1,h2,h3,h4,在FPGA中用原理圖實現,FIR設計方法在許多資料中有詳細介紹,這里不再贅述[4-7]。LMS算法在FPGA中采用原理圖和VHDL相結合的方法實現,下面對LMS算法模塊做重點介紹。
用LMS算法計算權值系數的頂層框圖如4所示,其基本結構為圖1所示的格型濾波結構,該模塊以待估計未知系統的輸出與自適應濾波器之差為輸入信號,輸出為不斷修正的濾波器系數h1,h2,h3,h4,同時作為自適應FIR濾波器的可調節系數。
為了計算方便,設調節算法的步進Δ=1/1 024,這樣只需要對待估計未知系統的輸出與自適應濾波器之差進行移位運算,即可以實現與步進Δ的乘法,從而避免了待估計未知系統的輸出與自適應濾波器之差與小數(步進Δ)做乘法的運算,減少了乘法運算產生的舍入誤差且節約了大量的FPGA資源,提高了計算速度[2]。
各主要模塊的VHDL語言描述如下[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算法計算權值系數的頂層框圖
(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),即步長為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 實驗和仿真結果
將Quartus對FPGA頂層系統建模文件的仿真結果另存為.TBL文件,用Matlab讀出該仿真結果,需要注意的是對FPGA生成的.TBL文件的最后一位要改成數字,最前面段英文要刪除,其Matlab程序如下:
clc;
clear all;
fid=fopen(′top.tbl′,′r′);%讀2位16進制數
s=fscanf(fid,′%s′,1);%文件讀指針指向第一個字符
i=1;
N=3;%輸出的16進制數據位數
while feof(fid)~=1
s1=s(1,1:length(s)-1);%s1為時間字符,該字符最后一位為>,必須除去(即-1)
time=str2num(s1);%將CHAR轉為NUM
clk_temp=fscanf(fid,′%s′,1);%指向下一個符號
clr_temp=fscanf(fid,′%s′,1);%指向下一個符號
in_temp=fscanf(fid,′%s′,1);%指向下一個符號
equal=fscanf(fid,′%s′,1);%跳過一個符號,s指向\"=\"
test=mod(time,500); %以500為周期對數據
取樣,剔除野值
fir8out_temp=fscanf(fid,′%s′,1);%fid指向輸出第一個數據
fir8lms_temp=fscanf(fid,′%s′,1);%fid指向輸出第二個數據
if (equal== ′=′)(test==0)%剔除周期以外的野值,并確認數據之前為\"=\"
fir8out(i)=hex2dec(fir8out_temp(1,1:N));%將16進制數轉為10進制數
fir8out_jilu(i)=fir8out(i);
if fir8out(i)>=(2^(4*N-1))%負數補碼轉為10進制
fir8out(i)=-(2^(4*N))+fir8out(i);
end
fir8lms(i)=hex2dec(fir8lms_temp(1,1:N));%將16進制數轉為10進制數
fir8lms_jilu(i)=fir8lms(i);
if fir8lms(i)>=(2^(4*N-1))%負數補碼轉為10進制
fir8lms(i)=-(2^(4*N))+fir8lms(i);
end
tt(i)=time/1000;%t單位ns,轉換為μs
clk(i)=str2num(clk_temp);
in(i)=hex2dec(in_temp(1,1:N));%將16進制數轉為10進制數
if in(i)>=(2^(4*N-1))%負數補碼轉為10進制
in(i)=-(2^(4*N))+in(i);
end
error=fir8out_jilu_fir8lms_jilu;
i=i+1;
end
s=fscanf(fid,′%s′,1);%指向下一行第一個符號
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;
值得注意的是,為解決結果數據毛刺太多而引起的.TBL文件過大使以上Matlab程序讀出數據速度太慢的問題,可以在Quartus仿真時用功能仿真代替時序仿真。具體方法是:在波形文件.VWF的Assignments菜單中選擇FUNCTIONAL,確定后再Processing菜單中選擇Generate Functional Simulation Netlist生成功能仿真網表后再開始仿真,這樣產生的結果數據基本沒有野值產生的毛刺。
用以上程序讀出的時鐘、輸入信號、未知系統輸出、自適應濾波器的輸出及誤差信號如圖5所示,其中,輸入信號同樣為迭加了隨機信號的正弦波。
圖5 時鐘、輸入、未知系統輸出、
自適應濾波器的輸出及誤差信號
從圖5的結果可以看出,自適應濾波器在經過開始的一段振蕩后,輸出逐漸與未知系統輸出一致,二者的誤差逐漸減小到接近零,即實現了無差跟蹤,可見,該自適應濾波器實現了對未知系統的跟蹤和建模,且有相當快的收斂速度。
5 結 語
本文用Matlab中的Simulink對LMS算法的實現方法進行了仿真,并在FPGA中實現了LMS算法,進而實現了在FPGA中用自適應濾波器對未知系統的建模,并對FPGA設計的系統建模結果用Matlab軟件仿真以增強Quartus的仿真功能,從而得到完整而且直觀的仿真結果,這種系統建模所采用的仿真、實現和驗證方法同樣適用于消除寬帶信號中的窄帶干擾、實現自適應譜線增強以及自適應均衡等[1],具有一定的通用性。
參考文獻
[1]陳懷琛.數字信號處理及其Matlab實現[M].北京:電子工業出版社,1998.
[2]楊躍忠,闕沛文,李亮.自適應LMS濾波器在FPGA中的實現[J].微計算機信息,2006,22(11):158 -160.
[3]潘松.EDA技術使用教程[M].北京:科學出版社,1999.
[4]孫耀奇,高火濤,熊超,等.基于Matlab和FPGA的FIR數字濾波器設計及實現[J].現代電子技術,2008,31(11):89-92.
[5]郭繼昌,向暉,滕建輔,等.基于FPGA的FIR濾波器的實現[J].電子技術應用,2000,26(5):60-62.
[6]李明緯,黃世震.應用分布式算法在FPGA平臺實現FIR低通濾波器[J].中國集成電路,2007,20(2):90-92.
[7]陳炳權.基于FPGA的FIR濾波器FFT算法與DA算法實現[J].襄樊學院學報,2005,22(2):54-56.
[8]宋立業,王景勝,彭繼慎.自適應濾波器的算法研究及DSP仿真實現[J].現代電子技術,2008,31(5):112-114.
[9]胡廣書.數字信號處理[M].北京:清華大學出版社,2003.
[10]褚振勇,翁木云.FPGA設計及應用[M].西安:西安電子科技大學出版社,2002.
作者簡介 劉 艷 女,1976年出生,陜西寶雞人,碩士研究生,助理工程師。研究方向為數字信號處理。