function calculateSampleRate() {
// 1. Get Input Values
var maxFreqInput = document.getElementById("maxFrequency").value;
var bitDepth = parseFloat(document.getElementById("bitDepth").value);
var channels = parseFloat(document.getElementById("channels").value);
var durationMins = document.getElementById("duration").value;
// 2. Validate Inputs
if (!maxFreqInput || maxFreqInput <= 0) {
alert("Please enter a valid Maximum Frequency (Hz).");
return;
}
var maxFreq = parseFloat(maxFreqInput);
var duration = durationMins ? parseFloat(durationMins) : 0;
// 3. Calculate Nyquist Rate
var nyquistRate = maxFreq * 2;
// 4. Determine Recommended Standard Rate
// Common Audio Standards: 44100, 48000, 88200, 96000, 192000
var standards = [44100, 48000, 88200, 96000, 192000];
var recommended = standards[0]; // Default
var found = false;
for (var i = 0; i = nyquistRate) {
recommended = standards[i];
found = true;
break;
}
}
// If Nyquist is higher than all standards, suggest Nyquist + 10% buffer or highest standard logic
if (!found) {
recommended = Math.ceil(nyquistRate * 1.1); // Custom high rate
}
// 5. Calculate Data Rates based on Recommended Rate
// Bitrate (kbps) = Sample Rate * Bit Depth * Channels
var bitrate = recommended * bitDepth * channels;
// 6. Calculate File Size
// Size (Bits) = Bitrate * Duration(seconds)
// Size (Bytes) = Size(Bits) / 8
// Size (MB) = Size(Bytes) / 1024 / 1024
var fileSizeMB = 0;
if (duration > 0) {
var durationSec = duration * 60;
var totalBits = bitrate * durationSec;
var totalBytes = totalBits / 8;
fileSizeMB = totalBytes / (1024 * 1024);
}
// 7. Format Output
var nyquistDisplay = nyquistRate.toLocaleString() + " Hz";
var recDisplay = recommended.toLocaleString() + " Hz";
var bitrateDisplay = (bitrate / 1000).toFixed(1) + " kbps";
var sizeDisplay = duration > 0 ? fileSizeMB.toFixed(2) + " MB" : "Enter duration for size";
// 8. Update DOM
document.getElementById("nyquistResult").innerHTML = nyquistDisplay;
document.getElementById("recRateResult").innerHTML = recDisplay;
document.getElementById("bitrateResult").innerHTML = bitrateDisplay;
document.getElementById("fileSizeResult").innerHTML = sizeDisplay;
document.getElementById("resultsArea").style.display = "block";
}
How to Calculate Sample Rate
Calculating the correct sample rate is fundamental to digital audio processing, analog-to-digital conversion (ADC), and understanding data bandwidth. The core principle behind calculating the minimum necessary sample rate is the Nyquist-Shannon Sampling Theorem.
1. The Nyquist-Shannon Theorem Formula
The theorem states that to perfectly reconstruct a continuous signal from its samples, the sampling rate must be at least twice the highest frequency component present in the signal. This minimum rate is known as the Nyquist Rate.
Sample Rate ≥ 2 × f_max
Where:
Sample Rate: The number of samples per second (measured in Hz).
f_max: The highest frequency (in Hz) you wish to record or reproduce.
Example Calculation
The upper limit of human hearing is generally accepted to be 20,000 Hz (20 kHz). To calculate the minimum sample rate required to capture full-spectrum human audio:
Identify f_max: 20,000 Hz
Apply the formula: 2 × 20,000 Hz = 40,000 Hz
Result: You need a sample rate of at least 40,000 Hz.
This explains why the CD standard was set at 44,100 Hz. It covers the 40,000 Hz requirement plus a small buffer to accommodate anti-aliasing filters.
2. Calculating Audio Bitrate
Once you have determined your sample rate, you often need to calculate the resulting bitrate (bandwidth). The bitrate tells you how much data is processed per second.
Bitrate (bps) = Sample Rate × Bit Depth × Channels
Example: Recording at 48,000 Hz (48 kHz), 24-bit depth, in Stereo (2 channels):
48,000 × 24 × 2 = 2,304,000 bits per second (bps)
Divide by 1,000 to get kilobits: 2,304 kbps
3. Common Standard Sample Rates
While you can calculate a theoretical minimum, digital audio hardware relies on standard rates. When your calculation gives you a number (e.g., 40kHz), you should select the next highest standard rate.
44.1 kHz: Standard for CD audio and most consumer music.
48 kHz: Standard for video (DVD, Blu-ray) and broadcasting.
88.2 kHz / 96 kHz: High-resolution audio standards used in professional mastering.
192 kHz: Ultra-high-resolution used in archival and specialized audio design.
Why is Aliasing a Problem?
If you calculate your sample rate incorrectly and set it lower than 2 × f_max, a phenomenon called Aliasing occurs. Frequencies above the Nyquist limit "fold over" and appear as lower, incorrect frequencies in the digital recording, resulting in harsh distortion that cannot be removed later. Always round up your calculated sample rate to ensure fidelity.