methodS1 <- function(x,d)
{
  if (d%%2 == 0) q <- d/2
  else q <- (d-1)/2
  filtcoef <- rep(1,2*q+1)
  if (d%%2 ==0) {
    filtcoef[c(1,2*q+1)] <- .5
    filtcoef <- filtcoef/(2*q)
  }
  else filtcoef <- filtcoef/(2*q+1)
  mtilde <- filter(x,filtcoef)
  resids <- x-mtilde
  t <- 1:length(x)
  w <- rep(1,d)
  for (k in 1:(d-1))
    w[k] <- mean(resids[t[t%%d == k]],na.rm=TRUE)
  w[d] <- mean(resids[t[t%%d == 0]],na.rm=TRUE)
  shat <- w-mean(w)
  shat <- rep(shat,ceiling(length(x)/d))
  shat <- shat[1:length(x)]
  shat
}

