您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > LMS算法matlab实现
LMS算法function[yn,W,en]=LMS(xn,dn,M,mu,itr)%LMS(LeastMeanSqure)算法%输入参数:%xn输入的信号序列(列向量)%dn所期望的响应序列(列向量)%M滤波器的阶数(标量)%mu收敛因子(步长)(标量)要求大于0,小于xn的相关矩阵最大特征值的倒数%itr迭代次数(标量)默认为xn的长度,Mitrlength(xn)%输出参数:%W滤波器的权值矩阵(矩阵)%大小为Mxitr,%en误差序列(itrx1)(列向量)%yn实际输出序列(列向量)%参数个数必须为4个或5个ifnargin==4%4个时递归迭代的次数为xn的长度itr=length(xn);elseifnargin==5%5个时满足Mitrlength(xn)ifitrlength(xn)|itrMerror('迭代次数过大或过小!');endelseerror('请检查输入参数的个数!');end%初始化参数en=zeros(itr,1);%误差序列,en(k)表示第k次迭代时预期输出与实际输入的误差W=zeros(M,itr);%每一行代表一个加权参量,每一列代表-次迭代,初始为0%迭代计算fork=M:itr%第k次迭代x=xn(k:-1:k-M+1);%滤波器M个抽头的输入y=W(:,k-1).'*x;%滤波器的输出en(k)=dn(k)-y;%第k次迭代的误差%滤波器权值计算的迭代式W(:,k)=W(:,k-1)+2*mu*en(k)*x;end%求最优时滤波器的输出序列yn=inf*ones(size(xn));fork=M:length(xn)x=xn(k:-1:k-M+1);yn(k)=W(:,end).'*x;end调用LMS算法%functionmain()closeall%周期信号的产生t=0:99;xs=10*sin(0.5*t);figure;subplot(2,1,1);plot(t,xs);grid;ylabel('幅值');title('it{输入周期性信号}');%噪声信号的产生randn('state',sum(100*clock));xn=randn(1,100);subplot(2,1,2);plot(t,xn);grid;ylabel('幅值');xlabel('时间');title('it{随机噪声信号}');%信号滤波xn=xs+xn;xn=xn.';%输入信号序列dn=xs.';%预期结果序列M=20;%滤波器的阶数rho_max=max(eig(xn*xn.'));%输入信号相关矩阵的最大特征值mu=rand()*(1/rho_max);%收敛因子0mu1/rho[yn,W,en]=LMS(xn,dn,M,mu);%绘制滤波器输入信号figure;subplot(2,1,1);plot(t,xn);grid;ylabel('幅值');xlabel('时间');title('it{滤波器输入信号}');%绘制自适应滤波器输出信号subplot(2,1,2);plot(t,yn);grid;ylabel('幅值');xlabel('时间');title('it{自适应滤波器输出信号}');%绘制自适应滤波器输出信号,预期输出信号和两者的误差figureplot(t,yn,'b',t,dn,'g',t,dn-yn,'r');grid;legend('自适应滤波器输出','预期输出','误差');ylabel('幅值');xlabel('时间');title('it{自适应滤波器}');运行结果LMS算法DSP程序#includemath.h#definePI3.1415926#defineCoeff16//定义阶数16阶#definenum1024//点数intFIRLMS(int*nx,float*nh,intnError,intnCoeffNum);//LMS函数声明floath[Coeff],fU;//h单位脉冲响应fu---2U的值intx[num],y[num],z[num];//x输入信号,y输出,z误差信号main(){inti,out;out=0;fU=0.0005;for(i=0;iCoeff;i++){h[i]=0;//初始化噪声信号}for(i=0;inum;i++){x[i]=256*sin(i*2*PI/34);//初始化输入信号y[i]=z[i]=0;//初始化输出信号误差信号}for(i=Coeff+1;inum;i++){out=FIRLMS(x+i,h,out-x[i-1],Coeff);//设置断点,调用LMS函数y[i]=out;z[i]=y[i]-x[i];}exit(0);//推出}intFIRLMS(int*nx,float*nh,intnError,intnCoeffNum){inti,r;floatfWork;r=0;for(i=0;inCoeffNum;i++){fWork=nx[i]*nError*fU;//h(n+1)=h(n)+2u*e(n)*x(n)nh[i]+=fWork;r+=(nx[i-i]*nh[i]);}r/=128;returnr;}运行结果音频采集程序#includecsl.h#includecsl_chip.h#includecsl_i2c.h#includecsl_pll.h#includecsl_mcbsp.h#includecsl_emif.h#includecsl_emifBhal.h#includestdio.h#include5502_FLASH.h#includeE2PROM_Function.h#includeCODEC.h#undefCODEC_ADDR#defineCODEC_ADDR0x1AUint16SourData1[65536]={0};Uint16SourData2[65536]={0};Uint16SourData3[65536]={0};Uint16OutData1[65536]={0};Uint16OutData2[65536]={0};Uint16OutData3[65536]={0};#pragmaDATA_SECTION(SourData1,.Audio_in_data1);#pragmaDATA_SECTION(SourData2,.Audio_in_data2);#pragmaDATA_SECTION(SourData3,.Audio_in_data3);#pragmaDATA_SECTION(OutData1,.Audio_out_data1);#pragmaDATA_SECTION(OutData2,.Audio_out_data2);#pragmaDATA_SECTION(OutData3,.Audio_out_data3);//定义McBSP的句柄MCBSP_HandlehMcbsp;/*------------------------------------------------------------------------------------*/////FUNCTION:MAIN///*------------------------------------------------------------------------------------*/voidmain(void){Uint16DataTempLeft=0;//暂存采样数据Uint16DataTempRight=0;//InitializeCSLlibrary-ThisisREQUIRED!!!CSL_init();#ifTESTCOMMAND==1TestCommand=AUDIOTRY;//试听#endif#ifTESTCOMMAND==2TestCommand=AUDIOCOPY;//录音并回放#endif//Themainfrequencyofsystemis240MHz//该频率是为了设置IIC模块的需要设置的,为了使用I2C_setup函数PLL_setFreq(1,0xC,0,1,3,3,0);//EMIF初始化Emif_Config();//OpenMcBSPport1andgetaMcBSPtypehandlehMcbsp=MCBSP_open(MCBSP_PORT1,MCBSP_OPEN_RESET);//ConfigMcBSPport1byusepreviouslydefinedstructureMcbsp_Config(hMcbsp);//I2C初始化I2C_cofig();//CODEC寄存器初始化inti_AIC();/*------------------------------------------------------------------------------------*///ReceivetheADCoutputdataofCODEC//ThenoutputthereceiveddatatoDACofCODEC/*------------------------------------------------------------------------------------*/while(1){/*左通路数据*/while(!MCBSP_rrdy(hMcbsp)){};DataTempLeft=MCBSP_read16(hMcbsp);/*右通路数据*/while(!MCBSP_rrdy(hMcbsp)){};DataTempRight=MCBSP_read16(hMcbsp);/*左声道耳机输出*/while(!MCBSP_xrdy(hMcbsp)){};MCBSP_write16(hMcbsp,DataTempLeft);/*右声道耳机输出*/while(!MCBSP_xrdy(hMcbsp)){};MCBSP_write16(hMcbsp,DataTempRight);}}
本文标题:LMS算法matlab实现
链接地址:https://www.777doc.com/doc-5106047 .html