;; biquad band-pass filter
(bind-func static bpfbq_c
(lambda ()
(let* ((y1 0.0)
(y2 0.0)
(x1 0.0)
(x2 0.0)
(b0 0.0)
(b1 0.0)
(b2 0.0)
(a0 0.0)
(a1 0.0)
(a2 0.0)
(two:SAMPLE 2.0)
(oldfreq:SAMPLE 0.0)
(oldbw 0.0))
;; bandwidth in octaves
(lambda (x freq bandwidth)
;; if frequency or bandwidth change
;; recalculate coefficients
(if (or (<> freq oldfreq)
(<> bandwidth oldbw))
(let* ((omega (* 1.0 STWOPI (/ freq SAMPLERATE)))
(sino (sin omega))
(coso (cos omega))
(alpha (* sino (sinh (* (/ (log2 two) two)
bandwidth
(/ omega sino))))))
(set! oldfreq freq)
(set! oldbw bandwidth)
(set! b0 alpha)
(set! b1 0.0)
(set! b2 (* -1.0 b0))
(set! a0 (+ 1.0 alpha))
(set! a1 (* -2.0 coso))
(set! a2 (- 1.0 alpha))))
(let ((y (- (+ (* (/ b0 a0) x)
(* (/ b1 a0) x1)
(* (/ b2 a0) x2))
(* (/ a1 a0) y1)
(* (/ a2 a0) y2))))
(set! y2 y1)
(set! y1 y)
(set! x2 x1)
(set! x1 x)
y)))))