Created
June 22, 2025 19:59
-
-
Save deckar01/f77d98550eaf5d9b3a954eb034388f86 to your computer and use it in GitHub Desktop.
Graphing the density distribution of audio bit depth formats
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
import matplotlib.pyplot as plt | |
import numpy as np | |
def bits_to_unit(d, n): | |
sign = (n >> (d - 1)) & 1 | |
mantissa = n & (2**(d - 1) - 1) | |
return ((-1) ** sign) * (mantissa / (2 ** (d - 1))) | |
def bits_to_float32(n): | |
sign = (n >> 31) & 1 | |
exponent = ((n >> 23) & (2 ** 8 - 1)) - (2 ** 7 - 1) | |
mantissa = n & (2 ** 23 - 1) | |
return ((-1) ** sign) * (1 + mantissa / (2 ** 23)) * (2 ** exponent) | |
stride = 2**12 | |
bins = np.linspace(-4, 4, 2 ** 10) | |
dist24u = [bits_to_unit(24, n) for n in range(0, 2**24, stride)] | |
dist32u = [bits_to_unit(32, n) for n in range(0, 2**32, stride)] | |
dist32f = [bits_to_float32(n) for n in range(0, 2**32, stride)] | |
plt.style.use('dark_background') | |
plt.hist( | |
[dist24u, dist32u, dist32f], | |
bins, | |
label=['24-bit Linear', '32-bit Linear', '32-bit Float'], | |
color=['blue', 'red', 'white'], | |
alpha=0.75, | |
histtype="step", | |
) | |
plt.xlim(bins[0], bins[-1]) | |
plt.yscale('log') | |
plt.xlabel('Amplitude') | |
plt.ylabel('Resolution') | |
plt.legend() | |
plt.title('Audio Bit Depth') | |
plt.show() |
Author
deckar01
commented
Jun 22, 2025

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment