Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 1.87 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (use-modules (srfi srfi-1))
  2.  
  3. (define default-algebra
  4.   (list
  5.    ;; Binary addition
  6.    (cons #:add +)
  7.    ;; Binary multiplication
  8.    (cons #:multiply *)
  9.    ;; Binary subtraction
  10.    (cons #:subtract -)))
  11.  
  12. (define (make-iir a-in  ;; a_1..a_n-1
  13.                   b-in  ;; b_0..b_n-1
  14.                 . rest) ;; algebra
  15.   (let* (;; Algebra that has at least addition and multiplication.
  16.          (algebra (if (null? rest) default-algebra (car rest)))
  17.  
  18.          ;; Extract operations from algebra for easier use. This also
  19.          ;; conveniently causes an error if a necessary operation is
  20.          ;; missing.
  21.          (add (cdr (assq #:add algebra)))
  22.          (mul (cdr (assq #:multiply algebra)))
  23.  
  24.          ;; Output side coefficients, with values for the latest
  25.          ;; samples placed last.
  26.          (a (reverse a-in))
  27.          ;; Input side coefficients. Same order as in output side.
  28.          (b (reverse b-in))
  29.  
  30.          ;; Lengths of coefficient sets
  31.          (Na (length a))
  32.          (Nb (length b))
  33.  
  34.          ;; Delay lines. Cars have the oldest value and ends have the
  35.          ;; most recent ones.
  36.          (x (make-list      Nb  0))
  37.          (y (make-list (+ 1 Na) 0))
  38.  
  39.          ;; References to the last pairs
  40.          (last-x (last-pair x))
  41.          (last-y (last-pair y)))
  42.  
  43.     (lambda (x0)
  44.       ;; Input delay line propagation
  45.       (set-cdr! last-x (cons #f '()))
  46.       (set! last-x (cdr last-x))
  47.       (set! x (cdr x))
  48.  
  49.       ;; Insert the new X value
  50.       (set-car! last-x x0)
  51.  
  52.       ;; Output delay line propagation
  53.       (set-cdr! last-y (cons #f '()))
  54.       (set! last-y (cdr last-y))
  55.       (set! y (cdr y))
  56.  
  57.       ;; Compute and insert new Y value
  58.       (set-car! last-y
  59.                 ;; The output value is computed here.
  60.                 (- (fold add 0 (map mul x b))
  61.                    (fold add 0 (map mul y a))))
  62.  
  63.       (car last-y))))
  64.  
  65. (define (make-fir b-in)
  66.   (make-iir '() b-in))