Low-Pass Filter (Moving Average)
Simplest smoothing filter. Averages signal over window. Removes high-frequency noise. Used for signal smoothing and noise reduction.
Visualization
Interactive visualization for Low-Pass Filter (Moving Average)
Low-Pass Filter:
- • Removes high frequencies
- • Smoothing
Interactive visualization with step-by-step execution
Implementation
1class LowPassFilter {
2 private buffer: number[] = [];
3 private sum: number = 0;
4
5 constructor(private windowSize: number) {}
6
7 // Batch processing
8 filter(signal: number[]): number[] {
9 const output: number[] = [];
10
11 for (const sample of signal) {
12 this.buffer.push(sample);
13 this.sum += sample;
14
15 if (this.buffer.length > this.windowSize) {
16 this.sum -= this.buffer.shift()!;
17 }
18
19 output.push(this.sum / this.buffer.length);
20 }
21
22 return output;
23 }
24
25 // Optimized with circular buffer
26 filterOptimized(signal: number[]): number[] {
27 const output = new Array(signal.length);
28 const window = new Array(this.windowSize).fill(0);
29 let sum = 0;
30 let idx = 0;
31
32 for (let i = 0; i < signal.length; i++) {
33 sum -= window[idx];
34 sum += signal[i];
35 window[idx] = signal[i];
36 idx = (idx + 1) % this.windowSize;
37
38 output[i] = sum / this.windowSize;
39 }
40
41 return output;
42 }
43}Deep Dive
Theoretical Foundation
Simple moving average: y[n] = (1/k)×Σ(x[n-i]) for i=0 to k-1. Equivalent to convolution with rectangular kernel. Frequency response: sinc function. Cutoff ≈ 1/k. Can use circular buffer for O(1) per sample.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(k)
Applications
Industry Use
Real-time audio noise reduction
Sensor data smoothing (temperature, pressure)
Financial data trend analysis
Biomedical signal preprocessing
Control system feedback filtering
Image processing (box blur)
Network traffic analysis
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¹.