acf_fft: Autocorrelation with FFT

View source: R/fft.R

acf_fftR Documentation

Autocorrelation with FFT

Description

Analogous to acf, but based on the Fast Fourier Transform (Wiener-Khinchin theorem) and 5-10 times faster, especially for long input vectors.

Usage

acf_fft(x, center = TRUE, padToMult = 2)

Arguments

x

numeric vector

center

if TRUE (default), x is centered before padding with 0

padToMult

pad with 0 to the smallest power of 2 above padToMult * length(x) - 1, must be >=1 (padToMult = 1 means no padding, leading to circular ACF)

Value

Numeric vector that is usually longer than input because it is padded with zeros to the next power of two. Constant and zero inputs return NA.

Examples

len = 200
x = sin(2 * pi * 100 * (1:len) / 1000) + rnorm(len, 0, .5)
plot(x, type = 'l')
aut = acf(x, lag.max = len/2)
aut2 = acf_fft(x)
points(0:100, aut2[1:(len/2+1)], type = 'l', col = 'blue')
aut$acf[1:10]
aut2[1:10]

# compare execution time
system.time(for (i in 1:100) acf(x, lag.max = len/2, plot = FALSE))
system.time(for (i in 1:100) acf_fft(x))

soundgen documentation built on Sept. 20, 2026, 5:07 p.m.