convolve()関数

n<-50
x<-rnorm(n)
y<-runif(n)
x<-sort(x)
y<-sort(y)
Han <- function(y) # Hanning
        convolve(y, c(1,2,1)/4, type = "filter") 
plot(x,y, main="Using  convolve(.) for Hanning filters")
lines(x[-c(1  , n)      ], Han(y), col="red")
lines(x[-c(1:2, (n-1):n)], Han(Han(y)), lwd=2, col="dark blue")
  • 使われている関数はconvolve()、その中で使われている関数はfft()、さらにnextn()
    • fft()は高速フーリエ変換の関数
    • nextn()はfft()で使う(らしい)関数で、ある整数を因数の積で表すとしたら、いくつになるかを示す関数(与えられた数値について、与えられた数値列の合成数のうち、その値以上の最小の数を返す)(1:10は(1,)2,3,5,7に素因数分解されるから、そのままの数値、11は(1,)2,3,5,7では素因数分解できないので、2^2*3=12を返している
sapply(1:100,nextn,c(2,3,5,7))
  • おまけ。概数表示
x2 <- pi * 100^(-1:3)
print(x2 / 1000, digits=4)
zapsmall(x2 / 1000, digits=4)
> x2 <- pi * 100^(-1:3)
> print(x2 / 1000, digits=4)
[1] 3.142e-05 3.142e-03 3.142e-01 3.142e+01 3.142e+03
> zapsmall(x2 / 1000, digits=4)
[1]    0.0    0.0    0.3   31.4 3141.6