반응형
안녕하세요! ✨
앞서 Day 26에서는 Particle Filter 개념을 학습했습니다.
오늘은 실제로 Particle Filter를 Python으로 구현해보고, 간단한 위치 추정 실습을 진행해보겠습니다.
🔍 Particle Filter 구현 목표
- 1D 또는 2D 환경에서 차량의 위치를 추정
- GPS와 같은 noisy 관측값을 기반으로 상태 보정
- 여러 개의 입자를 활용해 분포 기반 추정을 수행
🧑💻 Python 코드 예제
python
import numpy as np
import matplotlib.pyplot as plt
# 파라미터 설정
N = 1000 # 입자 개수
T = 20 # 시뮬레이션 스텝
true_pos = 0.0
true_vel = 1.0
# 초기 입자 (무작위)
particles = np.random.normal(0, 5, N)
weights = np.ones(N) / N
positions = []
for t in range(T):
# 실제 이동
true_pos += true_vel + np.random.normal(0, 0.2)
z = true_pos + np.random.normal(0, 2.0) # 관측값 (노이즈 포함)
# 1. 예측 단계
particles += np.random.normal(true_vel, 0.5, N)
# 2. 업데이트 단계
weights *= np.exp(- (particles - z)**2 / (2 * 2.0**2))
weights += 1.e-300 # underflow 방지
weights /= np.sum(weights)
# 3. 리샘플링
indices = np.random.choice(range(N), size=N, p=weights)
particles = particles[indices]
weights.fill(1.0 / N)
# 추정값 기록
estimate = np.mean(particles)
positions.append((true_pos, z, estimate))
# 결과 시각화
true_vals, obs, est = zip(*positions)
plt.plot(true_vals, label="True Position")
plt.plot(obs, 'r.', alpha=0.5, label="Measurements")
plt.plot(est, 'g-', label="PF Estimate")
plt.legend()
plt.title("Particle Filter Simulation")
plt.show()
import matplotlib.pyplot as plt
# 파라미터 설정
N = 1000 # 입자 개수
T = 20 # 시뮬레이션 스텝
true_pos = 0.0
true_vel = 1.0
# 초기 입자 (무작위)
particles = np.random.normal(0, 5, N)
weights = np.ones(N) / N
positions = []
for t in range(T):
# 실제 이동
true_pos += true_vel + np.random.normal(0, 0.2)
z = true_pos + np.random.normal(0, 2.0) # 관측값 (노이즈 포함)
# 1. 예측 단계
particles += np.random.normal(true_vel, 0.5, N)
# 2. 업데이트 단계
weights *= np.exp(- (particles - z)**2 / (2 * 2.0**2))
weights += 1.e-300 # underflow 방지
weights /= np.sum(weights)
# 3. 리샘플링
indices = np.random.choice(range(N), size=N, p=weights)
particles = particles[indices]
weights.fill(1.0 / N)
# 추정값 기록
estimate = np.mean(particles)
positions.append((true_pos, z, estimate))
# 결과 시각화
true_vals, obs, est = zip(*positions)
plt.plot(true_vals, label="True Position")
plt.plot(obs, 'r.', alpha=0.5, label="Measurements")
plt.plot(est, 'g-', label="PF Estimate")
plt.legend()
plt.title("Particle Filter Simulation")
plt.show()
📊 결과 해석
- 빨간 점: 센서 관측값(GPS 등) → 노이즈 많음
- 파란선: 실제 위치 → Ground Truth
- 초록선: Particle Filter 추정값 → 점점 실제 위치와 근접
👉 Particle Filter가 잡음 많은 관측에서도 강력한 추정 성능을 발휘함을 확인할 수 있습니다.
🚘 자율주행 활용
- SLAM (Simultaneous Localization and Mapping)
- GPS 불안정 지역에서의 차량 위치 추정
- 비선형·비가우시안 동역학 시스템 처리
📌 정리
- Particle Filter는 여러 개의 입자를 이용해 상태를 추정
- Python으로 간단히 구현 가능하며, 시각화로 원리 이해 용이
- 자율주행, 로봇, 드론 위치 추정에 활용 가치 높음
반응형
🚀 다음 글 예고
다음 글에서는 [Day 28: LiDAR + Camera 융합 (Feature matching)] 을 다룹니다.
LiDAR 점군과 카메라 영상 데이터를 융합하여 특징 매칭 기반의 센서 융합을 실습해보겠습니다.
✅ 오늘 글이 도움이 되셨다면 좋아요와 공유 부탁드립니다.
반응형
'자율주행' 카테고리의 다른 글
📡 Day 29: Radar + LiDAR 융합 실습 (0) | 2025.09.25 |
---|---|
📷 Day 28: LiDAR + Camera 융합 (Feature matching) (0) | 2025.09.24 |
🎯 Day 26: Particle Filter 개념 학습 (0) | 2025.09.22 |
🔗 Day 25: 센서 융합 (LiDAR + IMU) 실습 (0) | 2025.09.21 |
🔄 Day 24: EKF로 비선형 상태 추정 실습 (0) | 2025.09.20 |