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][1]. 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) )

Some disadvantages with the FFT are that 

 - commonly it needs length(x) == 2^n (n integer)
 - it is periodic.

Those problems tend to be solved by padding the data x with 0

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.


  [1]: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/convolve.html