Kalman Filter

🔥 Vibe Prompt

"Track a vehicle's position and velocity from noisy GPS (σ=30m) and IMU (σ=20m). Compare raw vs filtered."

import numpy as np

class KalmanFilter:
    def __init__(self):
        self.x = np.array([[0], [0], [0], [0]])  # pos_x, pos_y, vel_x, vel_y
        self.P = np.eye(4) * 100
        self.F = np.array([[1,0,1,0],[0,1,0,1],[0,0,1,0],[0,0,0,1]])
        self.H = np.array([[1,0,0,0],[0,1,0,0]])
        self.R = np.eye(2) * 30  # GPS noise
        self.Q = np.eye(4) * 0.1
    
    def predict(self):
        self.x = self.F @ self.x
        self.P = self.F @ self.P @ self.F.T + self.Q
    
    def update(self, z):
        y = z - self.H @ self.x
        S = self.H @ self.P @ self.H.T + self.R
        K = self.P @ self.H.T @ np.linalg.inv(S)
        self.x = self.x + K @ y
        self.P = (np.eye(4) - K @ self.H) @ self.P

kf = KalmanFilter()
true_pos = np.array([100, 100])
true_vel = np.array([5, 3])

for t in range(20):
    true_pos = true_pos + true_vel  # True movement
    gps = true_pos + np.random.normal(0, 30, 2)  # Noisy GPS
    
    kf.predict()
    kf.update(gps)
    
    error_raw = np.linalg.norm(gps - true_pos)
    error_filt = np.linalg.norm(kf.x[:2].flatten() - true_pos)
    
    print(f"t={t}: raw_error={error_raw:.1f}, filt_error={error_filt:.1f}")

Summary

| Aspect | Detail | |--------|--------| | State | What we're tracking (position, velocity, etc.) | | Predict | Motion model (F) + process noise (Q) | | Update | Measurement (z) + measurement noise (R) | | Kalman Gain (K) | Balances trust between prediction and measurement | | Extensions | EKF (non-linear), UKF (unscented), particle filter (non-Gaussian) |

Applications

| Domain | Application | |--------|-------------| | 🛰️ GPS/IMU | Drone, self-driving car, smartphone position | | 🚀 Aerospace | Rocket guidance, satellite orbit determination | | 🤖 Robotics | SLAM (Simultaneous Localization And Mapping) | | 📈 Finance | Hidden Markov models for regime detection | | 🫀 Medical | ECG denoising, blood glucose monitoring |

Gradient Descent Course Complete! 🎉

  • ✅ Gradient Descent Basics
  • ✅ Momentum & Adam
  • ✅ FFT / DFT
  • ✅ PID Control
  • ✅ Kalmanum & Adam\n- ✅ FFT / DFT\n- ✅ PID Control\n- ✅ Kalman Filter"}]}

Key Points

  • Understand the core concepts thoroughly
  • Practice with hands-on code examples
  • Apply knowledge to real-world problems
  • Review and reinforce through exercises

Further Learning

  • Official documentation
  • Open source projects on GitHub
  • Community forums and discussions
  • Related courses and tutorials


為什麼要學卡爾曼濾波器?

卡爾曼濾波器 是 algorithm-gradient-descent 課程的核心章節之一。

在真實世界中

卡爾曼濾波器 並不是課本上的理論——它在真實的軟體開發中頻繁出現。無論你是正在接案、準備面試,還是想要提升自己的技術深度,理解這個主題都能讓你直接受益。

你將從本章獲得

  • 🎯 完整的知識體系:從核心原理到實作細節,條理分明
  • 💻 可運行的程式碼:每段程式碼都是完整的,可直接執行
  • 🔍 除錯技巧:常見錯誤的分析與解決方案
  • 🚀 下一步指引:學完後該往哪個方向繼續深入

銜接下一章

本章為你建立了 卡爾曼濾波器 的完整知識基礎。下一章將在此基礎上,帶你探索更進階的真實世界應用場景——你將學會如何將本章所學應用到更複雜的問題中。

解鎖完整教學內容

本章為付費內容。加入專案即可解鎖超過 5000 字的深度解析,包含 10 個以上神級 Prompt 與真實 Source Code 範例!