viewof signalFrequency = Inputs.range([1, 100], {
value: 10,
step: 1,
label: "Signal Frequency (Hz)"
})
viewof samplingRate = Inputs.range([10, 500], {
value: 100,
step: 5,
label: "Sampling Rate (Hz)"
})
viewof adcBits = Inputs.select([8, 10, 12], {
value: 10,
label: "ADC Resolution (bits)"
})
viewof signalAmplitude = Inputs.range([0.1, 1.0], {
value: 0.8,
step: 0.1,
label: "Signal Amplitude (V)"
})ADC Sampling and Aliasing Demo
ADC Sampling and Aliasing Demo
animation
electronics
Try Keep Sampling Rate (Hz) at 100 and raise Signal Frequency (Hz) from 10 past half the sampling rate.
Observe The opening 10 Hz signal shows OK, a 50.0 Hz Nyquist frequency, and no aliasing artifact.
Explain Sampling Rate (Hz) divided by two sets the aliasing threshold; frequencies above it fold into the lower half of the sampling range.
Technical boundaries. The model assumes a pure sine wave and flags aliasing only strictly above the Nyquist frequency. The LSB card uses a fixed 3.3 V span, while plotted samples use twice the selected amplitude as their quantization span.
ADC Sampling and Aliasing Demo
Note
ADC Sampling and Aliasing Demo
Experiment with ADC sampling concepts: adjust signal frequency, sampling rate, and ADC resolution to see how they affect signal reconstruction and observe aliasing when the Nyquist criterion is violated.
nyquistFrequency = samplingRate / 2
isAliasing = signalFrequency > nyquistFrequency
// General aliasing formula: fold frequency into [0, fs/2] range
aliasedFrequency = isAliasing ? (() => {
let f = signalFrequency % samplingRate;
return f > samplingRate / 2 ? samplingRate - f : f;
})() : signalFrequency
// Calculate quantization parameters
quantizationLevels = Math.pow(2, adcBits)
lsbVoltage = 3.3 / quantizationLevels
maxQuantizationError = lsbVoltage / 2// Status display
html`<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; margin: 20px 0;">
<div style="background: ${isAliasing ? '#9b1c1c' : '#0b6b50'}; color: white; padding: 15px; border-radius: 8px; text-align: center;">
<div style="font-size: 24px; font-weight: bold;">${isAliasing ? 'ALIASING!' : 'OK'}</div>
<div style="font-size: 12px; margin-top: 5px;">${isAliasing ? 'fs < 2 x f (Nyquist violation)' : 'fs >= 2 x f (Nyquist satisfied)'}</div>
</div>
<div style="background: #2c3e50; color: white; padding: 15px; border-radius: 8px; text-align: center;">
<div style="font-size: 24px; font-weight: bold;">${nyquistFrequency.toFixed(1)} Hz</div>
<div style="font-size: 12px; margin-top: 5px;">Nyquist Frequency (fs/2)</div>
</div>
<div style="background: #7a4a00; color: white; padding: 15px; border-radius: 8px; text-align: center;">
<div style="font-size: 24px; font-weight: bold;">${isAliasing ? aliasedFrequency.toFixed(1) + ' Hz' : 'None'}</div>
<div style="font-size: 12px; margin-top: 5px;">${isAliasing ? 'Aliased Frequency (appears as)' : 'No Aliasing Artifact'}</div>
</div>
</div>`// Quantization info display
html`<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin: 15px 0;">
<div style="background: #34495e; color: white; padding: 12px; border-radius: 6px; text-align: center;">
<div style="font-size: 18px; font-weight: bold;">${adcBits}-bit</div>
<div style="font-size: 11px;">Resolution</div>
</div>
<div style="background: #34495e; color: white; padding: 12px; border-radius: 6px; text-align: center;">
<div style="font-size: 18px; font-weight: bold;">${quantizationLevels}</div>
<div style="font-size: 11px;">Quantization Levels</div>
</div>
<div style="background: #34495e; color: white; padding: 12px; border-radius: 6px; text-align: center;">
<div style="font-size: 18px; font-weight: bold;">${(lsbVoltage * 1000).toFixed(2)} mV</div>
<div style="font-size: 11px;">LSB Step Size</div>
</div>
<div style="background: #34495e; color: white; padding: 12px; border-radius: 6px; text-align: center;">
<div style="font-size: 18px; font-weight: bold;">+/-${(maxQuantizationError * 1000).toFixed(2)} mV</div>
<div style="font-size: 11px;">Max Quantization Error</div>
</div>
</div>`// Waveform visualization
{
const width = 700;
const height = 250;
const margin = {top: 20, right: 20, bottom: 35, left: 50};
// Time span: show 2-3 full cycles of the signal
const cycles = 3;
const duration = cycles / signalFrequency;
const numPoints = 500;
// Generate continuous signal
const continuous = Array.from({length: numPoints}, (_, i) => {
const t = (i / (numPoints - 1)) * duration;
return {t, v: signalAmplitude * Math.sin(2 * Math.PI * signalFrequency * t)};
});
// Generate sample points
const sampleInterval = 1 / samplingRate;
const numSamples = Math.floor(duration / sampleInterval) + 1;
const samples = Array.from({length: numSamples}, (_, i) => {
const t = i * sampleInterval;
const v = signalAmplitude * Math.sin(2 * Math.PI * signalFrequency * t);
// Quantize
const levels = Math.pow(2, adcBits);
const step = (2 * signalAmplitude) / levels;
const quantized = Math.round((v + signalAmplitude) / step) * step - signalAmplitude;
return {t, v: quantized};
});
// Generate aliased (reconstructed) waveform if aliasing
const aliased = isAliasing ? Array.from({length: numPoints}, (_, i) => {
const t = (i / (numPoints - 1)) * duration;
return {t, v: signalAmplitude * Math.sin(2 * Math.PI * aliasedFrequency * t)};
}) : null;
const aliasLabel = "Aliased (" + aliasedFrequency.toFixed(1) + " Hz)";
return Plot.plot({
width, height, marginTop: margin.top, marginRight: margin.right,
marginBottom: margin.bottom, marginLeft: margin.left,
style: {background: "#fafafa", borderRadius: "8px"},
x: {domain: [0, duration], ticks: 6, label: null,
tickFormat: d => (d * 1000).toFixed(1) + " ms"},
y: {domain: [-1.1 * signalAmplitude, 1.1 * signalAmplitude], ticks: 5, label: null,
tickFormat: d => d.toFixed(1) + " V"},
color: {domain: isAliasing ? ["Original signal", "ADC samples", aliasLabel] : ["Original signal", "ADC samples"],
range: isAliasing ? ["#3498DB", "#E67E22", "#9b1c1c"] : ["#3498DB", "#E67E22"], legend: true},
marks: [
Plot.lineY(continuous, {x: "t", y: "v", stroke: () => "Original signal", strokeWidth: 1.5, opacity: 0.6}),
...(aliased ? [Plot.lineY(aliased, {x: "t", y: "v", stroke: () => aliasLabel, strokeWidth: 2, strokeDasharray: "6,3"})] : []),
Plot.lineY(samples, {x: "t", y: "v", stroke: "#0b6b50", strokeWidth: 1, opacity: 0.5}),
Plot.dot(samples, {x: "t", y: "v", r: 3.5, fill: () => "ADC samples", stroke: "#2C3E50", strokeWidth: 0.5})
]
});
}How to Use This Demo:
- Start with defaults (10 Hz signal, 100 Hz sampling) - observe clean sampling
- Increase signal frequency past 50 Hz (half of sampling rate) to see aliasing occur
- Watch the aliased frequency - a 60 Hz signal sampled at 100 Hz appears as 40 Hz!
- Increase sampling rate to eliminate aliasing
- Change ADC resolution to see how quantization levels affect the sampled values
Key Observations:
- Nyquist Rule: When signal frequency > sampling rate / 2, aliasing occurs
- Aliased Frequency: The signal “folds back” into the [0, fs/2] range
- Quantization: Higher bit resolution = more voltage levels = finer measurements
- Status Indicators: The dashboard above turns red when the Nyquist criterion is violated
Try These Experiments:
| Experiment | Settings | What to Observe |
|---|---|---|
| Nyquist Limit | f=49 Hz, fs=100 Hz | Just under limit - OK |
| Aliasing | f=60 Hz, fs=100 Hz | 60 Hz appears as 40 Hz |
| Severe Aliasing | f=90 Hz, fs=100 Hz | 90 Hz appears as 10 Hz |
| More Samples | f=20 Hz, fs=200 Hz | Clean reconstruction |
| Low Resolution | Any, 8-bit | Visible quantization steps |
| High Resolution | Any, 12-bit | Smoother quantization |