Created
July 14, 2026 10:16
-
-
Save gkucmierz/e78c5c445689070cb4efa747a972f016 to your computer and use it in GitHub Desktop.
Run this code instantly in your browser: https://instacode.app/gist/e78c5c445689070cb4efa747a972f016
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Shannon-Hartley Theorem: Channel Capacity Calculator | |
| * | |
| * Formula: C = B * log2(1 + SNR) | |
| * | |
| * Where: | |
| * - C = Channel Capacity (bits per second) | |
| * - B = Bandwidth (Hz) | |
| * - SNR = Signal-to-Noise Ratio (linear) | |
| */ | |
| function dbToLinear(db) { | |
| return 10 ** (db / 10); | |
| } | |
| function calculateCapacity(bandwidthHz, snrdB) { | |
| const snrLinear = dbToLinear(snrdB); | |
| const capacityBps = bandwidthHz * Math.log2(1 + snrLinear); | |
| return { | |
| bps: capacityBps, | |
| kbps: capacityBps / 1000, | |
| bytesPerSec: capacityBps / 8 | |
| }; | |
| } | |
| // ---------------------------------------------------- | |
| // Acoustic Link Simulation (e.g. 2 kHz to 12 kHz) | |
| // ---------------------------------------------------- | |
| const BANDWIDTH = 10000; // 10 kHz | |
| const SNR_DB = 30; // 30 dB (typical quiet room) | |
| const capacity = calculateCapacity(BANDWIDTH, SNR_DB); | |
| console.log(`=== Shannon-Hartley Theorem ===`); | |
| console.log(`Bandwidth: ${(BANDWIDTH / 1000).toFixed(1)} kHz`); | |
| console.log(`SNR: ${SNR_DB} dB (Linear ratio: ${dbToLinear(SNR_DB).toFixed(0)}:1)`); | |
| console.log(`Capacity: ${capacity.kbps.toFixed(2)} kbps (${capacity.bytesPerSec.toFixed(0)} B/s)`); | |
| console.log('\nCapacity at different SNR levels:'); | |
| console.log('SNR (dB) | Ratio | Capacity (kbps)'); | |
| console.log('----------+----------+----------------'); | |
| [0, 10, 20, 30, 40, 50].forEach(db => { | |
| const ratio = dbToLinear(db); | |
| const cap = calculateCapacity(BANDWIDTH, db); | |
| const dbStr = `${db} dB`.padEnd(9); | |
| const ratioStr = `${ratio.toFixed(0)}:1`.padEnd(9); | |
| console.log(`${dbStr} | ${ratioStr} | ${cap.kbps.toFixed(2)} kbps`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment