Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (use-modules (srfi srfi-1))
- (define default-algebra
- (list
- ;; Binary addition
- (cons #:add +)
- ;; Binary multiplication
- (cons #:multiply *)
- ;; Binary subtraction
- (cons #:subtract -)))
- (define (make-iir a-in ;; a_1..a_n-1
- b-in ;; b_0..b_n-1
- . rest) ;; algebra
- (let* (;; Algebra that has at least addition and multiplication.
- (algebra (if (null? rest) default-algebra (car rest)))
- ;; Extract operations from algebra for easier use. This also
- ;; conveniently causes an error if a necessary operation is
- ;; missing.
- (add (cdr (assq #:add algebra)))
- (mul (cdr (assq #:multiply algebra)))
- ;; Output side coefficients, with values for the latest
- ;; samples placed last.
- (a (reverse a-in))
- ;; Input side coefficients. Same order as in output side.
- (b (reverse b-in))
- ;; Lengths of coefficient sets
- (Na (length a))
- (Nb (length b))
- ;; Delay lines. Cars have the oldest value and ends have the
- ;; most recent ones.
- (x (make-list Nb 0))
- (y (make-list (+ 1 Na) 0))
- ;; References to the last pairs
- (last-x (last-pair x))
- (last-y (last-pair y)))
- (lambda (x0)
- ;; Input delay line propagation
- (set-cdr! last-x (cons #f '()))
- (set! last-x (cdr last-x))
- (set! x (cdr x))
- ;; Insert the new X value
- (set-car! last-x x0)
- ;; Output delay line propagation
- (set-cdr! last-y (cons #f '()))
- (set! last-y (cdr last-y))
- (set! y (cdr y))
- ;; Compute and insert new Y value
- (set-car! last-y
- ;; The output value is computed here.
- (- (fold add 0 (map mul x b))
- (fold add 0 (map mul y a))))
- (car last-y))))
- (define (make-fir b-in)
- (make-iir '() b-in))
Advertisement
Add Comment
Please, Sign In to add comment