"""Shared helpers for the example solvers: load the challenge, write wavs, write submission JSON.""" from __future__ import annotations import json import platform from pathlib import Path import numpy as np from scipy.io import wavfile def load_challenge(path: Path) -> dict: with open(path) as f: return json.load(f) def write_wav(path: Path, samples_si: np.ndarray, challenge: dict) -> None: """Write pointwise samples of the SI signal, divided by signal_scale, as float32.""" out = challenge["output"] scaled = np.asarray(samples_si, dtype=np.float64) / float(out["signal_scale"]) path.parent.mkdir(parents=True, exist_ok=True) wavfile.write(path, int(out["sample_rate_hz"]), scaled.astype(np.float32)) def hardware_string() -> str: cpu = platform.processor() or platform.machine() try: with open("/proc/cpuinfo") as f: for line in f: if line.startswith("model name"): cpu = line.split(":", 1)[1].strip() break except OSError: pass return f"{cpu}, single thread, Python {platform.python_version()} + numpy {np.__version__}" def initial_shape(x: np.ndarray, p: dict) -> np.ndarray: """Raised-cosine pluck u_0(x) from the challenge spec.""" d = x - p["x_p"] u = np.zeros_like(x) m = np.abs(d) <= p["w"] u[m] = 0.5 * p["h"] * (1.0 + np.cos(np.pi * d[m] / p["w"])) return u