Sampling rate and aliased frequency
Explain how aliasing and anti-alias filters change sampled evidence.

Voltage Vera
Explain how aliasing and anti-alias filters change sampled evidence.
Predict the reading, then compare it with the measurement.
Python 3 in your browser (JupyterLite)
Python · no installOpen the notebook in your browser and run each Python cell; no install or account is needed.
Three ways to run: use JupyterLite here with no install; run main.py locally from the downloadable lab folder; or open the same notebook in Google Colab.
Steps
Step 1
- Do
- Run the Step 1 notebook cell to inspect the synthetic 7 Hz source and reference sample table.
- You will see
Run the cell. You should see:
- SYNTHETIC sine fixture; seed=207; no random draws; source(t)=sin(2*pi*7 Hz*t); duration=1.000 s; reference time_s signal; 0.000 0.000; 0.025 0.891
- Why it matters
- The original generated signal fixes the source frequency before sampling.
Code for step 1import platform import pyodide print(f'Runtime: Python {platform.python_version()} / Pyodide {pyodide.__version__}') """Sampling and aliasing with an explicitly synthetic 7 Hz sine fixture.""" import math SEED = 207 # deterministic fixture identifier; no random draws SOURCE_HZ = 7 DURATION_S = 1 def sample(rate_hz): return [math.sin(2 * math.pi * SOURCE_HZ * n / rate_hz) for n in range(rate_hz * DURATION_S)] def rising_crossings(values, rate_hz): return [n / rate_hz for n in range(1, len(values)) if values[n - 1] < 0 <= values[n]] def dominant_hz(values, rate_hz): """Largest one-second discrete Fourier bin below the Nyquist limit.""" bins = range(1, rate_hz // 2) return max(bins, key=lambda f: sum(values[n] * math.cos(2 * math.pi * f * n / rate_hz) for n in range(len(values))) ** 2 + sum(values[n] * math.sin(2 * math.pi * f * n / rate_hz) for n in range(len(values))) ** 2) def boxcar_then_decimate(): high = sample(40) # Four equally spaced 40 Hz samples per output. A causal boxcar is a # deliberately simple example, not an adequate production anti-alias filter. return [sum(high[(4 * n - k) % 40] for k in range(4)) / 4 for n in range(10)] def rms(values): return math.sqrt(sum(x * x for x in values) / len(values)) def run_step(n): high, low = sample(40), sample(10) filtered = boxcar_then_decimate() if n == 1: lines = ["SYNTHETIC sine fixture; seed=207; no random draws", "source(t)=sin(2*pi*7 Hz*t); duration=1.000 s", "reference time_s signal", *[f"{i/40:>13.3f} {high[i]:>7.3f}" for i in range(5)], "Source frequency is an input, not inferred from samples."] elif n == 2: lines = ["40 Hz sampling: Nyquist limit=20 Hz", f"sample count={len(high)}; spacing={1/40:.3f} s", "n time_s value", *[f"{i:>1} {i/40:>6.3f} {high[i]:>7.3f}" for i in range(5)], f"dominant 1-second Fourier bin={dominant_hz(high,40)} Hz", f"inferred period={1/dominant_hz(high,40):.3f} s"] elif n == 3: lines = ["10 Hz sampling: Nyquist limit=5 Hz", f"same 7 Hz source; sample count={len(low)}", "n time_s value", *[f"{i:>1} {i/10:>6.3f} {low[i]:>7.3f}" for i in range(5)], f"dominant 1-second Fourier bin={dominant_hz(low,10)} Hz", "The visible sequence cycles more slowly."] elif n == 4: predicted = abs(SOURCE_HZ - round(SOURCE_HZ / 10) * 10) measured = dominant_hz(low, 10) lines = ["Alias prediction for 7 Hz sampled at 10 Hz", "f_alias=abs(f_source-round(f_source/f_s)*f_s)", f"predicted alias={predicted} Hz", f"measured dominant Fourier bin={measured} Hz", f"apparent frequency={measured} Hz", f"predicted == measured: {predicted == measured}"] elif n == 5: lines = ["Causal 4-point boxcar at 40 Hz, then keep every fourth sample", "filter output[n]=mean(high[4*n-k], k=0..3)", "n raw_10Hz filtered_10Hz", *[f"{i:>1} {low[i]:>8.3f} {filtered[i]:>13.3f}" for i in range(4)], f"raw RMS={rms(low):.3f}; filtered RMS={rms(filtered):.3f}", f"RMS ratio={rms(filtered)/rms(low):.3f}"] elif n == 6: lines = ["RESULT CARD: synthetic 7 Hz source; seed=207", f"40 Hz: dominant bin {dominant_hz(high,40)} Hz", f"10 Hz: dominant bin {dominant_hz(low,10)} Hz", "alias arithmetic: |7-10|=3 Hz", f"boxcar downsample RMS: {rms(low):.3f} -> {rms(filtered):.3f}", "A 4-point boxcar attenuates this tone but is not a complete anti-alias design.", "This synthetic run does not establish a real sensor bandwidth."] else: raise ValueError(n) lines.append(f"STEP {n} complete") output = "\n".join(lines) print(output) return output run_step(1);
Step 1 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 2
- Do
- Run the Step 2 notebook cell to sample at 40 Hz and read the dominant frequency bin.
- You will see
Run the cell. You should see:
- 40 Hz sampling: Nyquist limit=20 Hz; sample count=40; spacing=0.025 s; dominant 1-second Fourier bin=7 Hz; inferred period=0.143 s
- Why it matters
- The faster sample rate keeps 7 Hz below its Nyquist limit.
Code for step 2run_step(2);
Step 2 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 3
- Do
- Run the Step 3 notebook cell to sample the same source at 10 Hz and inspect the slower sequence.
- You will see
Run the cell. You should see:
- 10 Hz sampling: Nyquist limit=5 Hz; same 7 Hz source; sample count=10; dominant 1-second Fourier bin=3 Hz; The visible sequence cycles more slowly.
- Why it matters
- The slower rate puts 7 Hz above its Nyquist limit.
Code for step 3run_step(3);
Step 3 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 4
- Do
- Run the Step 4 notebook cell to compare the predicted alias with the measured Fourier bin.
- You will see
Run the cell. You should see:
- Alias prediction for 7 Hz sampled at 10 Hz; f_alias=abs(f_source-round(f_source/f_s)*f_s); apparent frequency=3 Hz; predicted == measured: True
- Why it matters
- Agreement between arithmetic and the sampled spectrum identifies the alias.
Code for step 4run_step(4);
Step 4 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 5
- Do
- Run the Step 5 notebook cell to apply a four-point boxcar before decimation and compare RMS values.
- You will see
Run the cell. You should see:
- Causal 4-point boxcar at 40 Hz, then keep every fourth sample; filter output[n]=mean(high[4*n-k], k=0..3); raw RMS=0.707; filtered RMS=0.274; RMS ratio=0.387
- Why it matters
- An average can attenuate a tone before downsampling, with limited rejection.
Code for step 5run_step(5);
Step 5 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 6
- Do
- Run the Step 6 notebook cell and check the two rates, alias calculation, and filter limitation.
- You will see
Run the cell. You should see:
- RESULT CARD: synthetic 7 Hz source; seed=207; 40 Hz: dominant bin 7 Hz; A 4-point boxcar attenuates this tone but is not a complete anti-alias design.; This synthetic run does not establish a real sensor bandwidth.
- Why it matters
- The result must keep the simulated source and filter limitation visible.
Code for step 6run_step(6);
Step 6 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
Chapter checks
These questions refer to the chapter’s examples. Use the return links to review their answers.
You need to sample a 500 Hz signal. What is the minimum sampling rate according to Nyquist?
Return to the chapter’s knowledge checkAn ADC samples at eight kilohertz and the wanted signal ends at one kilohertz. Which anti-alias filter plan is reviewable?
Return to the chapter’s knowledge check
Caution
Before you start
Read this before you start.
This synthetic sine run does not establish a real sensor bandwidth or the suitability of a production anti-alias filter.
Technical details
- Version tested
- Version tested: Python 3.12.7 / Pyodide 0.27.6 in JupyterLite 0.6.4; Chromium real browser capture, 2026-10-09. Date: 2026-10-09.
https://iotclass.org/labs/sampling-rate-alias-frequency.htmlReturn to Sampling and Aliasing: Nyquist and Filters · Browse Labs