DSP.jl icon indicating copy to clipboard operation
DSP.jl copied to clipboard

Moving average function

Open Luapulu opened this issue 4 years ago • 4 comments

So, while I know it's not difficult to write a moving average function, I find I often need to calculate moving averages, and since moving averages are often used as and act like low pass filters, I feel like they ought to belong in a package like DSP.

If there's interest, I'd like to make a pull request for an implementation of a moving average function, a moving average function that repeatedly averages over a signal n times as well as mutating/non-mutating versions as appropriate. It may also be worth it to use conv / filter at some length / number of repeated averages, especially since multiple moving averages can probably be expressed as a convolution, requiring only one pass over a signal.

Luapulu avatar Oct 21 '20 11:10 Luapulu

Isn't this just convolution with a "boxcar" window?

galenlynch avatar Apr 27 '21 14:04 galenlynch

What about repeated moving averages?

Luapulu avatar Apr 27 '21 15:04 Luapulu

My understanding is that a repeated moving average can be accomplished by convolving the rectangular window with itself however many times the average is repeated, and then convolve the resulting filter with the data to be averaged over.

Anyways if you're interested in putting together a PR to make this process easier, I think that might be useful.

galenlynch avatar Apr 27 '21 16:04 galenlynch

I wrote a moving average filter for some dsp purposes, I and am somewhat unsure of how to submit a pull request, or if it is appropriate.

function moving_average(x::AbstractArray,M::Integer)
    
    M > 1 || error("window size must be larger that 1")
    conv(x,rect(M)/M)[M ÷ 2 + 1 : length(x) + M÷2]

end

The output is the same as a python implementation I saw.

justinbroce avatar Jan 31 '22 03:01 justinbroce