metaclass (metaclass) wrote,
metaclass
metaclass

FIR/IIR фильтр

Тормозит, конечно, безжалостно - тут вам не здесь, сплошные мультиметоды да персистентные структуры.

;;===========================
;; filter blocks

(defn- calc-filter
  [delay-queue koefs]
  (apply ga/+ (map ga/* delay-queue koefs))
)

(defn digital-filter-block-form-II
  "create digital filter block with feed-forward koefs b and feed-backward koefs b
   direct form II - one delay line; two addition chains"
  [akoefs bkoefs] 
  (let [ ; akoefs0 (doall (cons 1.0 akoefs))
        [b0 & brest] bkoefs
        bkoefs-rev (reverse brest)
        akoefs-rev (reverse akoefs)
        bN (count brest)
        aN (count akoefs)
        N (max bN aN)
       ] 
  (println "Order is "  N)
  (simple-block
   :inputs ['in]
   :outputs ['out] 
   :state (clojure.lang.PersistentQueue/EMPTY)
   :step (fn [r state [input]]
              (let [delay-input (ga/+ input (calc-filter state akoefs-rev))
                    output (ga/+ (ga/* delay-input b0) (calc-filter state bkoefs-rev))
                    new-delay (conj state delay-input)
                    new-delay-limit (if (> (count new-delay) N) (pop new-delay) new-delay)
                   ]
                 [new-delay-limit [output]]
              ))
  )))


(def digital-filter-block digital-filter-block-form-II)

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 48 comments