Research-Stack/5-Applications/tools-scripts/audio/audio_compression_sim.py

202 lines
9.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ==============================================================================
# COPYRIGHT NO ONE EVERYWHERE LLC (WYOMING HOLDING COMPANY)
# PROJECT: SOVEREIGN STACK
# This artifact is entirely proprietary and cryptographically proven.
# Open-Source usage requires explicit permission from Brandon Scott Schneider.
# ==============================================================================
"""
USC-Audio: Topological Soliton Encoding — Shannon-Eddington-Bekenstein revision.
Soliton dimensions are distributed across frequency bands using gravitational
shift weighting (Yu et al. 2025 / Bekenstein 1973):
- High-entropy bands sit near the snag (horizon) — heavily blueshifted,
need fewer basis dimensions (information already captured by the snag).
- Low-entropy bands are redshifted — spread thin, need more dimensions.
Each dimension still must carry >= 1 bit (Landauer floor).
"""
import math
from usc_spectral_core import (
eddington_utilization, shannon_capacity, signal_band, landauer_cost,
blueshift_factor, redshift_factor, shift_allocation,
geometric_dimensions, deep_compression_snag,
total_shift, angular_momentum_modes, conversion_efficiency, friction_loss,
)
PARAMS_PER_SOLITON = 8 # position(x), amplitude, phase, velocity(x), temporal-rate,
# curvature, bandwidth, coherence-length
def pcm_entropy(bit_depth: int, occupancy: float = 0.6) -> float:
"""Estimate entropy of a PCM band given bit depth and dynamic-range occupancy."""
active_levels = (2 ** bit_depth) * occupancy
return math.log2(max(active_levels, 1.0))
def octave_bands(f_low: float, f_high: float, n_bands: int) -> list:
"""Return n_bands logarithmically-spaced center frequencies between f_low and f_high."""
log_step = (math.log2(f_high) - math.log2(f_low)) / n_bands
return [f_low * (2 ** (i * log_step)) for i in range(n_bands)]
def spectral_occupancy(freq_hz: float, f_peak: float) -> float:
"""
Model spectral occupancy as a log-Gaussian envelope peaked at f_peak.
Low and high frequencies have less energy (lower occupancy).
"""
log_dist = (math.log2(max(freq_hz, 1.0)) - math.log2(max(f_peak, 1.0))) ** 2
return max(0.05, math.exp(-log_dist / 4.0))
class TSESolitonEncoder:
"""
Topological Soliton Encoder — full five-physics model:
1. Gravitational shift (snag geometry / Bekenstein)
2. Doppler shift (infall velocity)
3. Angular momentum (Kerr frame-dragging / mode splitting)
4. Conversion efficiency(accretion efficiency η)
5. Friction (Shakura-Sunyaev viscous dissipation)
"""
N_BANDS = 8
BASIL_LEN_M = 0.035 # basilar membrane length [m]
def _band_physics(self, centers, band_entropy, spin_param, friction_coeff):
"""Per-band shift, friction retention, and velocity for each octave band."""
h_max = max(band_entropy)
f_max = max(centers)
rows = []
for f, h in zip(centers, band_entropy):
vel = (f / f_max) * 0.5 # infall velocity: faster near snag
retained = friction_loss(h, h_max, friction_coeff)
shift = total_shift(h, h_max, vel)
rows.append((f, h, vel, retained, shift))
return rows
def encode(self, f_low: float, f_high: float, snr_db: float,
duration_s: float, bit_depth: int = 16,
spin_param: float = 0.5,
friction_coeff: float = 0.05) -> dict:
"""
Encode a signal using the five-physics TSE model.
Parameters
----------
f_low : lower frequency bound [Hz]
f_high : upper frequency bound [Hz]
snr_db : signal-to-noise ratio [dB]
duration_s : clip duration [s]
bit_depth : PCM quantisation depth
spin_param : temporal coherence ∈ [0,1] (0=noise, 1=pure tone)
friction_coeff : viscous dissipation μ ≥ 0 (Shakura-Sunyaev analog)
"""
snr_linear = 10 ** (snr_db / 10.0)
bandwidth = f_high - f_low
f_peak = math.sqrt(f_low * f_high)
centers = octave_bands(f_low, f_high, self.N_BANDS)
band_entropy = [pcm_entropy(bit_depth, spectral_occupancy(f, f_peak))
for f in centers]
h_total = sum(band_entropy) * (bandwidth * 2 * duration_s / self.N_BANDS)
physics = self._band_physics(centers, band_entropy, spin_param, friction_coeff)
mean_retained = sum(r[3] for r in physics) / len(physics)
# Bekenstein snag (3D → sqrt(H) law)
# n_snag uses raw horizon modes for band allocation — the Kerr AM
# splitting is for display; its efficiency benefit enters via η below.
snag = deep_compression_snag(h_total, n_dims=3)
n_snag = snag['horizon_modes']
n_am = angular_momentum_modes(n_snag, spin_param) # display only
# Friction and conversion efficiency reduce effective captured entropy
eta = conversion_efficiency(spin_param) * mean_retained
captured = h_total * eta
residual = h_total - captured
band_dims = shift_allocation(band_entropy, n_snag)
encoded_bits = sum(d * PARAMS_PER_SOLITON * 16 for d in band_dims)
residual_bits = residual # irreducible floor (Hawking-analog)
total_bits = encoded_bits + residual_bits
capacity = shannon_capacity(bandwidth, snr_linear) * duration_s
# λ_Edd measures the soliton basis against Shannon capacity.
# The residual is thermodynamically irreducible (like Hawking radiation)
# and does not count against channel capacity.
lam = eddington_utilization(encoded_bits, capacity)
band_name, band_desc = signal_band(f_peak)
geo_n = geometric_dimensions(self.BASIL_LEN_M / self.N_BANDS,
self.BASIL_LEN_M / (2 * math.pi))
return {
'band': band_name,
'band_desc': band_desc,
'h_total': h_total,
'geo_n': geo_n,
'snag_modes': n_snag,
'am_modes': n_am,
'eta': eta,
'mean_retained': mean_retained,
'residual_bits': residual_bits,
'encoded_bytes': total_bits / 8,
'soliton_bytes': encoded_bits / 8,
'capacity_bits': capacity,
'lambda_edd': lam,
'landauer_J': landauer_cost(h_total),
'physics': physics,
'band_dims': band_dims,
'band_entropy': band_entropy,
}
def run_audio_poc():
"""Benchmark three signal types under the five-physics TSE model."""
print("=" * 70)
print(" USC-AUDIO: TSE — gravity + doppler + ang.mom. + η + friction")
print("=" * 70)
duration = 1.0
flac_bytes = int(192000 * 2 * duration * 2) * 0.20
enc = TSESolitonEncoder()
# spin: voice=periodic formants, music=moderate, chaos=near-noise
# friction: Shakura-Sunyaev α — higher for chaotic signals
voice = enc.encode(300, 3400, snr_db=40, duration_s=duration,
bit_depth=16, spin_param=0.80, friction_coeff=0.05)
music = enc.encode(20, 20000, snr_db=60, duration_s=duration,
bit_depth=16, spin_param=0.50, friction_coeff=0.05)
chaos = enc.encode(20, 96000, snr_db=80, duration_s=duration,
bit_depth=24, spin_param=0.10, friction_coeff=0.15)
for label, r in [('VOICE', voice), ('MUSIC', music), ('CHAOS', chaos)]:
print(f"\n[{label}] H={r['h_total']:.0f} bits band={r['band']}")
print(f" Snag (Bekenstein) : {r['snag_modes']}")
print(f" + AM split (Kerr) : {r['am_modes']}")
print(f" η (accrtn×friction): {r['eta']:.4f} "
f"[friction retained={r['mean_retained']:.4f}]")
print(f" Captured entropy : {r['h_total']*r['eta']:.0f} bits")
print(f" Friction residual : {r['residual_bits']:.0f} bits ← irreducible floor")
print(f" Soliton basis : {r['soliton_bytes']:.1f} bytes")
print(f" Total encoded : {r['encoded_bytes']:.1f} bytes")
print(f" FLAC market : {flac_bytes:.0f} bytes")
print(f" λ_Edd : {r['lambda_edd']:.6f}")
print(f" Landauer cost : {r['landauer_J']:.3e} J")
print(f"\n {'Hz':>8} {'H':>6} {'β vel':>6} {'fric':>6} "
f"{'shift':>7} {'redshft':>7} {'N':>5}")
print(f" {'-'*57}")
h_max = max(r['band_entropy'])
for (f, h, vel, ret, shift), dims in zip(r['physics'], r['band_dims']):
rf = redshift_factor(h, h_max)
print(f" {f:>8.0f} {h:>6.2f} {vel:>6.3f} {ret:>6.3f} "
f"{shift:>7.3f} {rf:>7.3f} {dims:>5}")
print("\n" + "=" * 70)
print(" Friction floor is irreducible — entropy is a law, not a suggestion.")
print(" High spin → higher η → smaller residual → better compression.")
print("=" * 70)
if __name__ == "__main__":
run_audio_poc()