I've implemented the particle filter as follow:
System Model:
X=x+t*cos(theta)*V;
y=y+t*sin(theta)*V;
theta= theta+omega*t;
in which V, omega are velocity and angle velocity respectively. In addition, the observations consist of a noisy version of the distance from the left upper corner of the box.
However, I am not sure my code is correct.( distance of particle from each other is increasing), can any body please help me about that?
second: I want to show the object which I want track in the matlab, but I've tried different approaches, still unsuccessful. would you plz help me about this part as well.
%#######################################################
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
No matter what, you should vectorize this code. You'll take a huge performance hit using these nested for loops. In general, in an interpreted language like Matlab, you should never use a for command unless you absolutely have to. Try something like this:
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;