Skip to content

Commit 5b2f86d

Browse files
authored
Merge pull request AtsushiSakai#245 from goktug97/pf_resample
change PF resampling function to match with the book
2 parents 49ce57d + b2d65f2 commit 5b2f86d

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

Localization/particle_filter/particle_filter.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ def pf_localization(px, pw, z, u):
128128
xEst = px.dot(pw.T)
129129
PEst = calc_covariance(xEst, px, pw)
130130

131-
px, pw = re_sampling(px, pw)
132-
131+
N_eff = 1.0 / (pw.dot(pw.T))[0, 0] # Effective particle number
132+
if N_eff < NTh:
133+
px, pw = re_sampling(px, pw)
133134
return xEst, PEst, px, pw
134135

135136

@@ -138,21 +139,18 @@ def re_sampling(px, pw):
138139
low variance re-sampling
139140
"""
140141

141-
N_eff = 1.0 / (pw.dot(pw.T))[0, 0] # Effective particle number
142-
if N_eff < NTh:
143-
w_cum = np.cumsum(pw)
144-
base = np.cumsum(pw * 0.0 + 1 / NP) - 1 / NP
145-
re_sample_id = base + np.random.rand(base.shape[0]) / NP
146-
147-
indexes = []
148-
ind = 0
149-
for ip in range(NP):
150-
while re_sample_id[ip] > w_cum[ind]:
151-
ind += 1
152-
indexes.append(ind)
153-
154-
px = px[:, indexes]
155-
pw = np.zeros((1, NP)) + 1.0 / NP # init weight
142+
w_cum = np.cumsum(pw)
143+
base = np.arange(0.0, 1.0, 1/NP)
144+
re_sample_id = base + np.random.uniform(0, 1/NP)
145+
indexes = []
146+
ind = 0
147+
for ip in range(NP):
148+
while re_sample_id[ip] > w_cum[ind]:
149+
ind += 1
150+
indexes.append(ind)
151+
152+
px = px[:, indexes]
153+
pw = np.zeros((1, NP)) + 1.0 / NP # init weight
156154

157155
return px, pw
158156

0 commit comments

Comments
 (0)