updated pf_filter.m

This commit is contained in:
matlabbe
2015-06-16 08:31:38 -04:00
parent 53f7719655
commit bc18d4bf7d

View File

@@ -1,17 +1,24 @@
function filtered = pf_filter(x, nParticles, noise, lambda)
particles = zeros(nParticles,1) ;
weights = zeros(nParticles,1);
particles = ones(nParticles,1)*x(1) ;
weights = ones(nParticles,1);
filtered=zeros(1,length(x));
for i = 1:length(x);
for j = 1:nParticles
rn = sqrt(-2.0*log(rand))*cos(2*pi*rand); % randn c++
particles(j) = particles(j) + noise*rn ;
dist = abs(particles(j) - x(i));
weights(j) = exp(-lambda*dist);
noisyP = particles(j) + noise*rn ;
dist = abs(noisyP - x(i));
tmp = exp(-lambda*dist);
if isfinite(tmp) && tmp > 0
particles(j) = noisyP;
weights(j) = tmp;
end
end
weights = weights ./(sum(weights(:)));
if sum(weights(:)) > 0
weights = weights ./sum(weights(:));
end
filtered(i) = weights'*particles;
particles = pf_resample(particles, weights);
end