├── README.md └── particlefilter.py /README.md: -------------------------------------------------------------------------------- 1 | particlefilter 2 | ============== 3 | 4 | Simple Particle filter implementation in Python 5 | -------------------------------------------------------------------------------- /particlefilter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import numpy as np 4 | import random 5 | 6 | 7 | class Particle: 8 | def __init__(self,weight,state): 9 | self.weight = weight 10 | self.state=state 11 | 12 | 13 | 14 | class ParticleFilter: 15 | 16 | def __init__(self, numParticles, transMatrix, obsMatrix): 17 | self.particles = [] 18 | self.dim,self.dim=transMatrix.shape 19 | self.numParticles=numParticles 20 | self.transMatrix=transMatrix 21 | self.obsMatrix=obsMatrix 22 | 23 | for i in xrange(numParticles): 24 | self.particles.append( Particle(1/numParticles,random.randint(0,self.dim-1)) ) 25 | #print self.particles[-1].state 26 | 27 | for i in range(10): 28 | self.predict() 29 | self.update([0]) 30 | #self.resample() 31 | self.resample() 32 | for x in range(len(self.particles)): print self.particles[x].state, 33 | 34 | 35 | def predict(self): 36 | 37 | #for x in range(len(self.particles)): print self.particles[x].state," ", 38 | 39 | temp = [] 40 | temp = self.particles 41 | 42 | for i in range(self.numParticles): 43 | currentState=self.particles[i].state 44 | rnd=random.random() 45 | 46 | for j in range(self.dim): 47 | if rnd