|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
在.m文件中如何将F=[1 1;0 1]中的第二个数设为参数,写成F=[1 ST;0 1]或F=[1 par1;0 1]在VC中都调用失败,不要参数一可以成功,全部代码如下:
(因为是毕业设计,恳请大家帮忙,导师要求将这个设为参数在VC中为其赋值,QQ:350892132 谢谢!)
%本程序基于P68(卡尔曼滤波与维纳滤波)
function kalman1(par1,par2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%_____________________________________________________________
%变量的设置
ST=par1;%存储采样周期
s=50;%控制采样数据点
XG=zeros(2,s);%存储最佳估计值
P=zeros(2,2);%滤波误差方差
Z=zeros(1,s);%存储观测值
Z1=zeros(1,s);%存储观测值
K=zeros(2,s);%存储最优滤波增益
XY=zeros(2,1);%存储最优预测值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%____________________________________________________________
%常量的初始化
F=[1 ST;0 1];%状态转移矩阵
H=[1 0]; %观测矩阵
XG(:,1)=[2.5 2]';%初始化最佳估计值
P(:,:)=[10 0;0 10];%初始化滤波误差方差
T=[0.001 0;0 0.001];%
B=[0 0]';%
%K(:,1)=[0.5 0.3]';%最优滤波增益初始化
E=0;%差值XY(2,2)=XG(:,1);%;%
XY(:,1)=XG(:,1);%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%____________________________________________________________
%产生观测值
t=zeros(50);
l=zeros(50);
Zs=par2;
for k=1:s
l(k)=1+1.5*k; % 用于存储理论值
t(k)=Zs*randn; % 用于存储模拟观测误差
Z(1,k)=1+1.5*k+t(k);%模拟观测值(为了与第58行对应统一,所以此处设为k+1)
Z1(1,k)=Z(1,k);
end
%Z1(1,20)=-100;
%Z1(1,30)=150;
%Z1(1,40)=-50;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%____________________________________________________________
%产生噪声
Q=zeros(1,s);%存储观测误差方差矩阵
R=zeros(1,s);%存储输入控制方差矩阵
for k=1:s
R(:,k)=Zs*Zs;%观测误差方差矩阵
Q(:,k)=0.1;%输入控制方差矩阵
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%____________________________________________________________
%滤波过程
for k=1:s
Kp=F*K(:,k);%
XY(:,:)=F*XY(:,:)+Kp*E;%最优预测值(注意此处的设计)
E=Z(1,k)-H*XY(:,:);%差值
PP=F*P(:,:)*F'+T*Q(:,k)*T';% 预测误差协方差
K(:,k+1)=PP*H'*inv(H*PP*H'+R(k));%最优滤波增益
P=(eye(2)-K(:,k+1)*H)*PP;%滤波误差方差
XG(:,k+1)=F*XG(:,k)+K(:,k+1)*E;%滤波值
p11(k)=P(1,1);%存储滤波方差的变化(位置)
p22(k)=P(2,2);%存储滤波方差的变化(速度)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%____________________________________________________________
%显示设置
m=zeros(1,s);%存储横坐标
y=zeros(1,s);%存储观测值
z=zeros(1,s);%存储滤波值
z1=zeros(1,s);
v=zeros(1,s);
for i=1:s
m(i)=i;%产生横坐标
y(1,i)=Z1(1,i);%得到观测值
z(1,i)=XG(1,i+1);%得到滤波值
v(1,i)=XG(2,i+1);
z1(1,i)=0.15;
end
%%%%%%%%%%%______________________________________________________
%%%%%%%%%%%%%信噪比的变化对滤波的影响
Zp=0;%用于存储理论值的平均值
for i=1:s
Zp=l(i)+Zp;
end
Zp=Zp/s;%理论值的平均值
Zx=0;%用于存储信号
for i=1:s
Zx=(l(i)-Zp)*(l(i)-Zp)+Zx;
end
Zx=Zx/s;%信号
xzb=Zx/(Zs*Zs) %信噪比
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%综合显示
subplot(221);
plot(m,y,'k',m,z,'r',m,l,'b');
xlabel('时间(Time)/秒(Sec)'),ylabel('观测值(Measurement)')
subplot(222);
plot(m,z,'r');
xlabel('时间(Time)/秒(Sec)'),ylabel('滤波值(Estimates)')
axis([0,50,-10,100]);
subplot(223);
plot(m,v,'k',m,z1,'r');
xlabel('时间(Time)/秒(Sec)'),ylabel('速度(V)/秒(Sec)');
S=zeros(50);
O=zeros(50);
for i=1:s
S(i)=y(i)-l(i);%存储观测误差
O(i)=z(i)-l(i);%存储滤波误差
end
% subplot(234);
% plot(m,S,'k');
% xlabel('时间(Time)/秒(Sec)'),ylabel('测量误差(Measurement Error)');
subplot(224);
plot(m,O,'M');
xlabel('时间(Time)/秒(Sec)'),ylabel('滤波误差(Estimate Eror)');
[ 本帖最后由 feng0830 于 2009-4-22 21:21 编辑 ] |
|