Skip to main content
5 of 7
added 427 characters in body
zexot
  • 41
  • 5

Normally, the weighted average weights should add to 1, and in your sample it adds to 2

w=c(1/2,1,1/2)

sum(w)=2

maybe it should be?

w=c(1/4,1/2,1/4) 

and you can get a moving average with the filter function

F <- filter(x, filter = w, method = c("convolution"), sides = 2)

If your convolution kernel w is too large, and you want to check it for speed, I would try Fast Fourier convolution. FFT convolution should already be implemented somewhere in all languages.

The FFT has the property that

#pseudocode
FFT(F) = FFT(x) * FFT(w)

so, to get F you do

#pseudocode
F <- inverseFFT( FFT(x) * FFT(w) )

Also, in your Rcpp code, frequently a weighted average kernel (w) is symmetrical and has repeated weights, so you can take advantage of it by saving the multiplications to avoid repeating them. I do not do c++ code, but somebody else may use it to improve your code.

zexot
  • 41
  • 5