Coefficient weights of Simple Moving Average (SMA) on SMA A simple moving average (SMA: arithmetic mean) is a low-pass FIR-filter. When you cascade 2 SMA's with a window of length $n$, so when you apply the second SMA on the output of the first SMA, and you want to know which coefficient weights a filter would have with the same output result, applied to the original signal, the 2 impulse responses (coefficients/weights) need to undergo a convolution (non-circular): this should return a vector of $(2*n)-1$ coefficients / weights. I refer to the image below: you have a sinusoidal signal (black), an SMA (blue), and an SMA of this SMA (green), and I compared this to a filter with the &quot;convolution weights&quot;:

If you do this in R: the command should be convolve(rep(1/n,n),rep(1/n,n),conj = FALSE,type=&quot;open&quot;), right? Obviously the red curve does not coincide with the green curve. Is there an analytic solution that returns the weight vector for this new moving average? Not just for 1 SMA on 1 SMA, but for $x$ SMA's on $x$ SMA's?
Here's my pretty basic R code (I assume Matlab-users / dsp-engineers understand):
n&lt;-10
vperiod&lt;-40
vwave&lt;-sin(2*pi*1/vperiod*(1:(600)))
# 
SMA&lt;-function(x,n)
{
  out&lt;-c()
  for (i in n:length(x))
  {
    out[i]&lt;-(sum((x[(i-(n-1)):i])*rep((1/n),n)))
  }
  out[1:(n-1)]&lt;-out[n]
  return(out)
}
# 
SmaOnSma&lt;-function(x,n)
{
  out&lt;-c()
  cnvweights&lt;-convolve(rep(1/n,n),rep(1/n,n),conj = FALSE,type=&quot;open&quot;)
  for (i in length(cnvweights):length(x))
  {
    out[i]&lt;-sum(x[(i-(length(cnvweights))+1):i]*cnvweights)
  }
  out[1:(n-1)]&lt;-out[n]
  return(out)
}
# 
plot(vwave[(2*vperiod):(3*vperiod)],type=&quot;l&quot;,lwd=2,main=&quot;SMA on SMA vs. convolution weights&quot;);abline(h=0,lty=3,col=&quot;gray&quot;)
legend(&quot;bottomleft&quot;,inset=.03,c(&quot;signal&quot;,&quot;SMA of signal&quot;,&quot;SMA of SMA of signal&quot;,&quot;Filter with convolution weights&quot;),fill=c(&quot;black&quot;,&quot;blue&quot;,&quot;green&quot;,&quot;red&quot;),horiz=FALSE,border=&quot;white&quot;,box.col=&quot;white&quot;)
# 
tempwave&lt;-vwave
lines(SMA(tempwave,n)[(2*vperiod):(3*vperiod)],col=&quot;blue&quot;,lwd=2)
tempwave&lt;-SMA(tempwave,n)
lines(SMA(tempwave,n)[(2*vperiod):(3*vperiod)],col=&quot;green&quot;,lwd=2)
lines(SmaOnSma(vwave,n)[(2*vperiod):(3*vperiod)],col=&quot;red&quot;,lwd=2)


Update:

Thanks to the kind answers here is the code in R for a moving average on moving average:
smavector&lt;-function(n)
{
  return(rep((1/n),n))
}

SmaOnSma&lt;-function(x,n,nit)
{
  if (nit==1)
  {
    cnvweights&lt;-smavector(n)
  }
  if (nit==2)
  {
    cnvweights&lt;-convolve(smavector(n),smavector(n),conj = TRUE,type=&quot;open&quot;)
  }
  if (nit&gt;2) 
  {
    cnvweights&lt;-convolve(smavector(n),smavector(n),conj = TRUE,type=&quot;open&quot;)
    for (j in 1:(nit-2))
    {
      cnvweights&lt;-convolve(smavector(n),cnvweights,conj = TRUE,type=&quot;open&quot;)
    }
  }
  #
  out&lt;-c()
  for (i in length(cnvweights):length(x))
  {
    out[i]&lt;-sum(x[(i-(length(cnvweights))+1):i]*cnvweights)
  }
  out[1:(n-1)]&lt;-out[n]
  return(out)
}

I initially thought this could be a method for estimating the instantaneous frequency of a smooth curve, by correcting for the SMA's frequency response, as per my previous question on SE: which was calculated as ($(sin(n*(\pi/p)))/(n*sin(\pi/p))$), (with p = the period of the wave = 1/frequency), but it becomes clear that with every iteration, the length of the weight vector grows by a factor of $(2*n)+1$ the previous weight vector length. Even if you do only half the iterations and multiply with $-1$, the minimum length of required input data (with the same frequency), is still $2$ times the period + $1$.
The green curve in your plot corresponds to the correct result. The red curve is wrong, because the weights in the vector cnvweights are in the wrong order (left and right halves are interchanged). The correct way to compute those weights is
cnvweights &lt;- convolve(rep(1/n,n), rev(rep(1/n,n)), conj = TRUE, type = &quot;open&quot;)
According to the R documentation of the function 'convolve', you need to use conj = TRUE for standard convolution.