docs & refactor: update fixed-point integer lfilter specification, variable naming, A0 normalization docs, and 32-bit MCU C implementation

This commit is contained in:
pchang718
2026-05-17 14:21:02 +08:00
parent e9dba45626
commit c4b4a4ced0
3 changed files with 302 additions and 8 deletions
+7 -7
View File
@@ -82,23 +82,23 @@ def integer_lfilter(b_int, a_int, x_float, shift_in, shift_out, shift_b, shift_a
round_offset_out = (1 << (out_shift - 1)) if (use_round and out_shift > 0) else 0
for n in range(len(x_int)):
acc = 0
sum_b = 0
# Feedforward: 前饋完全不位移,結果為 Q_{in + b}
for i in range(nb):
if n - i >= 0:
acc += b_int[i] * x_int[n - i]
sum_b += b_int[i] * x_int[n - i]
# Feedback: 歷史紀錄為 Q_{in + b},係數為 Q_a,乘積為 Q_{in + b + a}
fb_sum = 0
sum_a = 0
for j in range(1, na):
if n - j >= 0:
fb_sum += a_int[j] * y_hist[n - j]
sum_a += a_int[j] * y_hist[n - j]
# Feedback 縮放:除以 A0 (即 >> shift_a),結果退回 Q_{in + b} 完美對齊
# Feedback 縮放:前提 A0 = 1 (或者代表放大了 Q_a 倍的常數),除以 A0 (即 >> shift_a) 歸一化
# Python 的 // 等同於硬體的 SRA (Arithmetic Right Shift),會向負無窮大 Floor
fb_shifted = (fb_sum + round_offset_a) // A0
sum_a_scaled = (sum_a + round_offset_a) // A0
acc -= fb_shifted
acc = sum_b - sum_a_scaled
# 將超高精度的 acc 直接存入歷史變數
y_hist[n] = acc