function calculateNyquist() {
// Get inputs
var maxFreqInput = document.getElementById('maxFrequency');
var unitSelect = document.getElementById('freqUnit');
var errorMsg = document.getElementById('errorMsg');
var resultContainer = document.getElementById('result-container');
var maxFreqValue = parseFloat(maxFreqInput.value);
var multiplier = parseFloat(unitSelect.value);
// Validation
if (isNaN(maxFreqValue) || maxFreqValue = 2 * f_max
var nyquistRateHz = 2 * freqInHz;
// Nyquist Interval formula: T = 1 / f_s
var nyquistIntervalSeconds = 1 / nyquistRateHz;
// Determine display unit for Sampling Rate
var displayRate;
var displayUnit;
if (nyquistRateHz >= 1000000000) {
displayRate = (nyquistRateHz / 1000000000).toFixed(4);
displayUnit = "GHz";
} else if (nyquistRateHz >= 1000000) {
displayRate = (nyquistRateHz / 1000000).toFixed(4);
displayUnit = "MHz";
} else if (nyquistRateHz >= 1000) {
displayRate = (nyquistRateHz / 1000).toFixed(4);
displayUnit = "kHz";
} else {
displayRate = nyquistRateHz.toFixed(2);
displayUnit = "Hz";
}
// Format Time Interval (seconds, ms, us, ns)
var timeDisplay;
if (nyquistIntervalSeconds < 0.000000001) {
timeDisplay = (nyquistIntervalSeconds * 1000000000000).toFixed(4) + " ps";
} else if (nyquistIntervalSeconds < 0.000001) {
timeDisplay = (nyquistIntervalSeconds * 1000000000).toFixed(4) + " ns";
} else if (nyquistIntervalSeconds < 0.001) {
timeDisplay = (nyquistIntervalSeconds * 1000000).toFixed(4) + " µs";
} else if (nyquistIntervalSeconds < 1) {
timeDisplay = (nyquistIntervalSeconds * 1000).toFixed(4) + " ms";
} else {
timeDisplay = nyquistIntervalSeconds.toFixed(6) + " s";
}
// Get original unit label
var originalUnitLabel = unitSelect.options[unitSelect.selectedIndex].text;
// Output Results
document.getElementById('nyquistRateResult').innerHTML = displayRate + " " + displayUnit;
document.getElementById('nyquistIntervalResult').innerHTML = timeDisplay;
document.getElementById('bandwidthResult').innerHTML = maxFreqValue + " " + originalUnitLabel;
resultContainer.style.display = 'block';
}
How to Calculate Nyquist Sampling Rate: A Complete Guide for Signal Processing
In the world of digital signal processing (DSP) and telecommunications, bridging the gap between analog reality and digital representation relies on one fundamental rule: the Nyquist–Shannon sampling theorem. Whether you are an audio engineer, a data scientist dealing with time-series data, or an electrical engineering student, understanding how to calculate the Nyquist sampling rate is critical to preventing data loss and distortion.
This guide provides a functional calculator and a detailed breakdown of the math behind sampling theory, aliasing, and reconstruction.
What is the Nyquist Sampling Rate?
The Nyquist Sampling Rate (often simply called the "Nyquist Rate") is the minimum rate at which a signal can be sampled without introducing errors, specifically a type of distortion known as aliasing. To perfectly reconstruct a continuous analog signal from its digital samples, the sampling frequency must be strictly greater than twice the highest frequency component present in the signal.
Formula: fs ≥ 2 · fmax
fs: The Sampling Frequency (samples per second).
fmax: The highest frequency component (bandwidth) of the analog signal.
Step-by-Step Calculation Formula
Calculating the Nyquist rate is mathematically straightforward, but identifying the correct variables is where mistakes often happen. Here is the process:
1. Identify the Highest Frequency (fmax)
Analyze your analog signal to determine the highest frequency component. For example, the human ear can hear frequencies up to approximately 20 kHz (20,000 Hz). Therefore, for high-fidelity audio, fmax is considered 20 kHz.
2. Apply the Multiplier
Multiply the highest frequency by 2. This gives you the theoretical lower limit for sampling.
Example: 20 kHz × 2 = 40 kHz.
3. Add a Buffer (Oversampling)
While the theorem states 2x is the minimum, in real-world engineering, we sample slightly higher to allow for practical anti-aliasing filters. This is why standard CD audio is sampled at 44.1 kHz rather than exactly 40 kHz.
What is the Nyquist Interval?
While the rate tells you "how many samples per second," the Nyquist Interval tells you the maximum time allowed between two consecutive samples.
Interval Formula: Ts = 1 / (2 · fmax)
If you sample slower than this interval (i.e., the time between samples is too long), you will miss critical information about how the wave is changing.
Real-World Examples
Digital Audio
The standard range of human hearing is 20 Hz to 20,000 Hz (20 kHz). To capture the highest pitch (20 kHz):
fmax: 20,000 Hz
Calculation: 20,000 × 2 = 40,000 Hz
Result: The sampling rate must be at least 40 kHz.
Telephone Systems (Voice)
Standard voice telephony limits bandwidth to approximately 3.4 kHz to save data while keeping speech intelligible.
fmax: 3.4 kHz (or 4 kHz including guard bands)
Calculation: 4 kHz × 2 = 8 kHz
Result: Standard PCM voice is sampled at 8 kHz.
Why is Aliasing a Problem?
If you fail to sample at the Nyquist rate (i.e., you sample too slowly), high-frequency signal components will "fold over" into the low-frequency spectrum. This creates "ghost" signals that were not present in the original source.
In audio, this sounds like metallic distortion. In video, this creates the "wagon-wheel effect," where wheels appear to spin backward because the camera's frame rate (sampling rate) is too slow to capture the wheel's actual rotation speed.
Frequently Asked Questions
Does the Nyquist rate apply to the fundamental frequency or harmonics?
It applies to the highest harmonic component. If you have a 1 kHz square wave, it contains harmonics at 3 kHz, 5 kHz, 7 kHz, etc. To capture the square wave shape perfectly, you need to sample based on the highest harmonic you wish to preserve, not just the 1 kHz fundamental.
What happens if I sample exactly at the Nyquist rate?
Technically, sampling exactly at 2x can be problematic depending on the phase of the sampling. It is always safer to sample strictly above the Nyquist rate ($f_s > 2f_{max}$).
Is higher always better?
Oversampling (sampling much faster than the Nyquist rate) simplifies the design of analog-to-digital converters and improves signal-to-noise ratio, but it requires more storage space and processing power.