1D Convolution
Fundamental signal processing operation. Blends two signals. Used in filtering, smoothing, edge detection. Linear time-invariant system.
Visualization
Interactive visualization for 1D Convolution
1D Convolution:
- • Signal * Kernel
- • Filtering operation
Interactive visualization with step-by-step execution
Implementation
1function convolve1D(signal: number[], kernel: number[]): number[] {
2 const n = signal.length;
3 const m = kernel.length;
4 const output: number[] = new Array(n + m - 1).fill(0);
5
6 // Flip kernel
7 const flipped = [...kernel].reverse();
8
9 // Convolution
10 for (let i = 0; i < output.length; i++) {
11 for (let j = 0; j < m; j++) {
12 const idx = i - j;
13 if (idx >= 0 && idx < n) {
14 output[i] += signal[idx] * flipped[j];
15 }
16 }
17 }
18
19 return output;
20}
21
22// FFT-based convolution for large kernels
23function fftConvolve(signal: number[], kernel: number[]): number[] {
24 const n = signal.length + kernel.length - 1;
25 const paddedSize = 2 ** Math.ceil(Math.log2(n));
26
27 const signalFFT = fft(padSignal(signal, paddedSize));
28 const kernelFFT = fft(padSignal(kernel, paddedSize));
29
30 const product = signalFFT.map((s, i) => ({
31 re: s.re * kernelFFT[i].re - s.im * kernelFFT[i].im,
32 im: s.re * kernelFFT[i].im + s.im * kernelFFT[i].re
33 }));
34
35 const result = ifft(product);
36 return result.slice(0, n).map(c => c.re);
37}Deep Dive
Theoretical Foundation
Discrete convolution: (f * g)[n] = Σ f[k]×g[n-k]. Flips kernel, slides across signal. Commutative: f*g = g*f. Associative. Output length = n+m-1. FFT convolution: O(n log n) for large kernels.
Complexity
Time
O(n×m) direct, O(n log n) FFT
O(n×m)
O(n×m)
Space
O(n+m)
Applications
Industry Use
Digital audio effects (reverb, echo)
Image processing (blurring, sharpening)
Radar and sonar pulse compression
Communications channel modeling
Biomedical signal filtering
Seismic data processing
Computer vision feature detection
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¹.