Autocorrelation Function
Measures signal similarity with delayed version of itself. Detects periodicity, pitch detection in audio. Cross-correlation variant compares two signals.
Visualization
Interactive visualization for Autocorrelation Function
Autocorrelation:
- • Self-similarity at different lags
- • Signal processing
Interactive visualization with step-by-step execution
Implementation
1function autocorrelation(signal: number[]): number[] {
2 const n = signal.length;
3 const result: number[] = new Array(n);
4
5 for (let lag = 0; lag < n; lag++) {
6 let sum = 0;
7 for (let i = 0; i < n - lag; i++) {
8 sum += signal[i] * signal[i + lag];
9 }
10 result[lag] = sum;
11 }
12
13 // Normalize
14 const r0 = result[0];
15 return result.map(r => r / r0);
16}
17
18// FFT-based (faster for large signals)
19function autocorrelationFFT(signal: number[]): number[] {
20 const fft = computeFFT(signal);
21 const powerSpectrum = fft.map(c => c.re * c.re + c.im * c.im);
22 const ifft = computeIFFT(powerSpectrum.map(p => ({ re: p, im: 0 })));
23
24 const result = ifft.map(c => c.re);
25 const r0 = result[0];
26 return result.map(r => r / r0);
27}Deep Dive
Theoretical Foundation
R[lag] = Σ x[n]×x[n+lag] for n=0 to N-lag-1. Normalized: R[lag]/R[0]. Peak at lag k → periodicity with period k. Used in pitch detection, pattern finding. Wiener-Khinchin: autocorrelation = IFFT(|FFT(signal)|²).
Complexity
Time
O(n log n) FFT
O(n²) direct
O(n²) direct
Space
O(n)
Applications
Industry Use
Musical pitch detection and tuning
Speech fundamental frequency analysis
Engine vibration monitoring
Heartbeat detection in medical devices
Radar target detection and ranging
Seismic signal analysis
Pattern recognition in time series
Use Cases
Related Algorithms
GCD (Euclidean Algorithm)
Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Dating back to around 300 BC and appearing in Euclid's Elements, it's one of the oldest algorithms still in common use. The algorithm is based on the principle that GCD(a,b) = GCD(b, a mod b) and is remarkably efficient with O(log min(a,b)) time complexity. The GCD is the largest positive integer that divides both numbers without remainder.
LCM (Least Common Multiple)
Calculate the Least Common Multiple (LCM) of two integers - the smallest positive integer that is divisible by both numbers. The LCM is intimately related to the GCD through the formula: LCM(a,b) = |a×b| / GCD(a,b). This relationship allows us to compute LCM efficiently using the Euclidean algorithm for GCD, achieving O(log min(a,b)) time complexity instead of naive factorization methods.
Sieve of Eratosthenes
Ancient and highly efficient algorithm to find all prime numbers up to a given limit n. Invented by Greek mathematician Eratosthenes of Cyrene (276-194 BC), this sieve method systematically eliminates multiples of primes, leaving only primes in the array. With O(n log log n) time complexity, it remains one of the most practical algorithms for generating large lists of primes, vastly superior to trial division which runs in O(n² / log n) time.
Prime Factorization
Decompose a positive integer into its unique prime factor representation. Every integer greater than 1 can be expressed as a product of prime numbers in exactly one way (Fundamental Theorem of Arithmetic). This algorithm uses trial division optimized to check only up to √n, as any composite number must have a prime factor ≤ √n. Returns a map of prime factors to their powers, e.g., 360 = 2³ × 3² × 5¹.