The problem
A microphone picks up a 1 kHz tone we want and a loud 7.5 kHz whistle we do not. Each is a sine wave of 1 V. On the wire they add up: a slow wave covered in fast wiggles.
The converter (ADC) takes fs = 8000 samples a second. It can only tell frequencies apart below half of that, fs/2 = 4 kHz: the Nyquist frequency. The 1 kHz tone fits. The 7.5 kHz whistle does not.
Aliasing
With the switch closed, the filter is skipped and the converter samples the raw wave. The whistle folds back across 4 kHz to 8 − 7.5 = 0.5 kHz. The samples look like a 1 kHz tone plus a 500 Hz hum.
In memory the alias is a real-looking 500 Hz tone of 1.00 V. A true 500 Hz sound could give the very same numbers, so no program can remove it later. The whistle must go before sampling.
The anti-alias filter
Open the switch: now the signal passes resistor R, with capacitor C to ground. C lets fast changes leak away, so high frequencies shrink and low ones pass. A low-pass filter used this way is an anti-alias filter.
The filter's corner, or cutoff, is fc = 1/(2πRC). With R = 10 kΩ and C = 10 nF, fc = 1/(2π × 10 000 × 0.000 000 01) = 1592 Hz. At fc the gain is 1/√2 = 0.707.
The 1 kHz tone keeps 0.85 of its size. The 7.5 kHz whistle shrinks to 0.21. Both started at 1 V, so each bar now ends on the curve. The price: the wanted tone loses a little too.
Now the converter samples the filtered wave. The whistle still folds to 500 Hz, but at 0.21 V instead of 1.00 V: a DFT of 64 samples finds 0.21 V at 500 Hz and 0.85 V at 1 kHz. Smaller, not gone.
Better filters
One R and one C fall only 20 dB per decade. A 4th-order Butterworth filter with the same fc falls 80 dB per decade: it keeps 0.99 of the 1 kHz tone and leaves just 0.002 of the whistle.
With numpy, three lines give the cutoff and both gains: fc = 1591.5 Hz, H = 0.847 and 0.208. np.sqrt works on the whole array at once. Change R or C and run it again.
The working
# step 1/10 · mix
x(t) = sin(2π·1000·t) + sin(2π·7500·t) volts
wanted 1000 Hz, 1 V peak
whistle 7500 Hz, 1 V peak
left plot: the wave over time
right plot: its spectrum, one bar per
frequency (bar height = peak V)
Both tones are 1 V (a chosen example); 8000 samples a second is the telephone rate of ITU-T G.711, whose voice band is 300 to 3400 Hz, below the 4 kHz Nyquist frequency. The filtered wave is the steady state, after the switch-on transient (a few RC = 0.1 ms) has died away. Every number is computed from this model. The motion is slowed down a great deal: the real converter takes a sample every 125 µs.