On Mon, 22 Jun 2009 23:36:25 -0700 (PDT), Sean Devlin <francoisdev...@gmail.com> wrote: > Hey all, > Does anyone know of a moving window function? I'm curious if there > are any tools like this for digital signals processing, 30-day moving > averages, etc.
If you need weighted moving averages, this is a tad ugly but seems to work .. (defn weighted-moving-average* "Generate a lazy sequence consisting of weighted moving averages over the pre-partitioned input sequence. The weights parameter should be a sequence of weights the same length as the partition size." [weights input] (lazy-seq (when-let [input (seq input)] (let [weight-sum (apply + weights) weighted-input-sum (apply + (map * weights (first input)))] (cons (/ weighted-input-sum weight-sum) (weighted-moving-average* weights (rest input))))))) (defn weighted-moving-average "Generate a lazy sequence consisting of weighted moving averages over the input sequence. The weighting is given by weight-fn, having a domain 0..1 which will be scaled across win-size elements to calculate the weighting." [weight-fn win-size input] (let [weight-samples (reverse (for [t (range 1 (inc win-size))] (/ t win-size))) weights (map weight-fn weight-samples)] (weighted-moving-average* weights (partition win-size 1 input)))) -Kyle --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---