我已经实现了如下粒子滤波器:
系统模型:
X=x+t*cos(theta)*V;
y=y+t*sin(theta)*V;
theta= theta+omega*t;
其中V,omega分别是速度和角速度。此外,观察结果包括从盒子左上角的距离的嘈杂版本。
然而,我不确定我的代码是否正确。(粒子彼此之间的距离正在增加),任何人都可以帮助我吗?
第二次:我想在matlab中显示我想跟踪的对象,但我尝试了不同的方法,但仍然不成功。你能不能帮助我解决这个问题。
%#######################################################
clc;
clear all;
close all;
N=400; % numebr of Particles
T=100; % Time Steps
x0=zeros(1,N);
theta0=zeros(1,N);
y0=zeros(1,N);
v=5;
omega=pi/4;
%%
% x theta, y and Omega and V
particle=zeros(3,N);
w = ones(T,N); % Importance weights.
resamplingScheme=1;
for t=2:T
%% Prediction Steps
for p=1:N
v_noisy=v+rand*.5;
omega_nosiy=omega*.2;
particle(1,p)=x0(p)+t*v_noisy*cosd(theta0(p));
particle(2,p)=y0(p)+t*v_noisy*sind(theta0(p));
particle(3,p)=theta0(p)+omega_nosiy*t;
end
%% IMPORTANCE WEIGHTS:
for p=1:N
distance=sqrt( particle(1,p)^2+ particle(2,p)^2);
if distance< 4 || distance > 25
distance = .7;
else
distance=.3;
end
w(t,p) =distance;
end
w(t,:) = w(t,:)./sum(w(t,:)); % Normalise the weights.
%% SELECTION STEP:
if resamplingScheme == 1
outIndex = residualR(1:N,w(t,:)'); % Residual resampling.
elseif resamplingScheme == 2
outIndex = systematicR(1:N,w(t,:)'); % Systematic resampling.
else
outIndex = multinomialR(1:N,w(t,:)'); % Multinomial resampling.
end;
x0=particle(1,outIndex);
y0=particle(2,outIndex);
theta0=particle(3,outIndex);
clf;
hold on;
plot(x0,y0,'gx');
refresh;
drawnow;
end
答案 0 :(得分:2)
无论如何,你应该对这段代码进行矢量化。使用这些嵌套的 for 循环,您将获得巨大的性能提升。通常,在像Matlab这样的解释性语言中,除非绝对必须,否则不应使用 for 命令。尝试这样的事情:
distance = sqrt(particle(1,:).^2 + particle(2,:).^2);
outOfBounds = distance < 4 | distance > 25; % note use of vectorized | operator instead of scalar || operator
w(t,outOfBounds) = 0.7;
w(t,~outOfBounds) = 0.3;