#!/usr/bin/env python3 # SPDX-License-Identifier: MIT AND ISC """Evaluate ares's Gaussian-table formula and print the table's SHA-256. python -I scripts/dsp_gauss_formula.py [--print] The formula is ares's ``DSP::gaussianConstructTable`` (ares ``ares/sfc/dsp/gaussian.cpp`` at 9408cb43), restated in Python. It reads no file. The hash is of the 512 entries packed as little-endian int16, the form the module's rung 0 asserts. The formula is taken from ares under the ISC licence: ares - Copyright (c) 2004-2025 ares team, Near et al Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ from __future__ import annotations import hashlib import math import struct import sys def gaussian_table() -> list[int]: """The 512-entry table: a windowed sinc, then each phase's four taps scaled to sum 2048.""" raw = [0.0] * 512 for n in range(512): k = 0.5 + n s = math.sin(math.pi * k * 1.280 / 1024) t = (math.cos(math.pi * k * 2.000 / 1023) - 1) * 0.50 u = (math.cos(math.pi * k * 4.000 / 1023) - 1) * 0.08 raw[511 - n] = s * (t + u + 1.0) / k table = [0] * 512 for phase in range(128): taps = (phase, phase + 256, 511 - phase, 255 - phase) scale = 2048.0 / sum(raw[i] for i in taps) for i in taps: table[i] = int(raw[i] * scale + 0.5) # positive values: truncation is the C cast return table def main() -> int: table = gaussian_table() digest = hashlib.sha256(struct.pack("<512h", *table)).hexdigest() if "--print" in sys.argv[1:]: for row in range(0, 512, 16): print(", ".join(f"{v:4d}" for v in table[row : row + 16])) print(f"min {min(table)} max {max(table)} sha256 {digest}") return 0 if __name__ == "__main__": sys.exit(main())