基于FPGA设计的kalman滤波器——QMJ( 二 )


由图2和图3仿真波形对比,可以看出本设计的滤波,将会在FPGA中实现良好的滤波效果 。
图6 FPGA处理后的数据五、的滤波现所到达的硬件指标:
1)端口32个
2)寄存器663个
程序综合后生成的报告如下图:
图7 程序综合报告六、注意事项
【基于FPGA设计的kalman滤波器——QMJ】1)程序对txt文件大量数据的读写,使用 r e a d m e m b 、 、 、fopen和$命令 。
2)除法器计算需要34个时钟周期 。
3)程序的待输入信号数据必须进过归一化处理 。
七、程序讲解
下面所展示程序,仅是摘取出来的主要程序 。
1,下面的程序中,第1,2,3,4状态,实现预测方程中计算协方差矩阵预测值过程中3个矩阵相乘 。第35,36,37,38状态,实现更新方程中用增益计算协方差矩阵的修正值 。并在第42个时钟,将当前时刻计算得到的状态矩阵和协方差矩阵赋值到预测方程中上一时刻的状态矩阵和协方差矩阵 。, ,, 是上一时刻协方差矩阵元素 。P2_1,P2_2,P2_3,P2_4是当前时刻修正后协方差矩阵元素 。
always @(posedge clk)begin count<=count+1'b1;case(count) 1: beginrow1=F1;row2=F2;col11<=P_int1;col21<=P_int4;col12<=P_int2;col22<=P_int5;state<=S0;end2: beginrow1<=F4;row2<=F5;col11<=P_int1;col21<=P_int4;col12<=P_int2;col22<=P_int5;state<=S1;end3: beginrow1<=P1_Part1_1;row2<=P1_Part1_2;col11<=F1;col21<=F2;col12<=F4;col22<=F5;state<=S3; end4: beginrow1<=P1_Part1_4;row2<=P1_Part1_5;col11<=F1;col21<=F2;col12<=F4;col22<=F5;state<=S4;end35: beginrow1<=K_1;row2<=F4;col11<=P_int1;col21<=P_int4;col12<=P_int2;col22<=P_int5; state<=S6;end36: beginrow1<=K_2;row2<=F4;col11<=P_int1;col21<=P_int4;col12<=P_int2;col22<=P_int5;state<=S7;end37: beginrow1<=P2_Part1_1;row2<=P2_Part1_2;col11<=F1;col21<=F2;col12<=F4;col22<=F5;state<=S8;end 38:beginrow1<=P2_Part1_4;row2<=P2_Part1_5;col11<=F1;col21<=F2;col12<=F4;col22<=F5;state<=S9;end default:beginif (count==6'd42)beginx_int1<=x_est1;x_int2<=x_est2; P_int1<=P_out1;P_int2<=P_out2;P_int4<=P_out4;P_int5<=P_out5;count<=0;endendendcaseend always @(prod1 or prod2 or prod3)begincase(state)S0: beginP1_Part1_1<=prod1;P1_Part1_2<=prod2;endS1:beginP1_Part1_4<=prod1;P1_Part1_5<=prod2;endS3:beginP1_Part2_1<=prod1;P1_Part2_2<=prod2;end S4:beginP1_Part2_4<=prod1;P1_Part2_5<=prod2;endS6:beginP2_Part1_1<=prod1;P2_Part1_2<=prod2;end S7:beginP2_Part1_4<=prod1;P2_Part1_5<=prod2;end S8:beginP2_Part2_1<=prod1;P2_Part2_2<=prod2; end S9:beginP2_Part2_4<=prod1;P2_Part2_5<=prod2;enddefault:n<=1'd1;endcaseend always @ (posedge clk) beginP1_1 <= P1_Part2_1+GxQxGT;P1_2 <= P1_Part2_2+GxQxGT; P1_4 <= P1_Part2_4+GxQxGT;P1_5 <= P1_Part2_5+GxQxGT;endassignP2_1 = P1_1-P2_Part2_1;assignP2_2 = P1_2-P2_Part2_2;assignP2_4 = P1_4-P2_Part2_4;assignP2_5 = P1_5-P2_Part2_5;
2,下面的的程序,用来向除法器赋值进行计算增益 。第一个除法器的被除数是由协方差矩阵的和乘以状态转移矩阵的F5元素组成,第二个除法器的被除数由协方差矩阵的提供 。
always @(posedge clk) beginif (count == 1) begini_start <= 1;i_dividend <=P_int1+P4T;i_divisor <=S;endif (count == 2) begini_start <= 0;endendalways @(posedge clk) beginif (count == 1) begini_start2 <= 1;i_dividend2 <=P_int4;i_divisor2 <=S;endif (count == 2) begini_start2 <= 0;endend
3,下面的程序,将对乘法器进行赋值,用来计算预测方程中状态矩阵的预测值,以及更新方程中状态矩阵的修正值 。上一时刻的状态矩阵由,组成 。当前时刻的状态矩阵修正值是,组成 。
assignxin1=x_int1;assignxin2=x_int2;assignK1=K_1;assignK2=K_2;assignK3=P_int1;assignK4=P_int4;assignzdiff=z_diff;always @(posedge clk)//20beginx_prod1 <= xprod11;x_prod2