File size: 2,187 Bytes
570b87b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #!/usr/bin/env python3
"""Phase 2 demo: Wigner-D vs dense rotation accuracy + timing."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from hoa64 import encode_points, rotate_yaw_pitch_roll, doa_from_intensity
from hoa64.analysis import angular_error_deg
from hoa64.rotate import rotate_source_directions
def main() -> None:
az0, el0 = 25.0, -12.0
yaw, pitch, roll = 40.0, 15.0, -10.0
a0 = encode_points([az0], [el0], [1.0])
az1, el1 = rotate_source_directions(az0, el0, yaw=yaw, pitch=pitch, roll=roll)
a_gt = encode_points([float(az1)], [float(el1)], [1.0])
a_w = rotate_yaw_pitch_roll(
a0, yaw=yaw, pitch=pitch, roll=roll, method="wigner"
)
a_d = rotate_yaw_pitch_roll(
a0, yaw=yaw, pitch=pitch, roll=roll, method="dense", n_azi=72, n_el=36
)
rel_w = np.linalg.norm(a_w - a_gt) / np.linalg.norm(a_gt)
rel_d = np.linalg.norm(a_d - a_gt) / np.linalg.norm(a_gt)
print(f"Ground-truth source after rot: az={float(az1):.2f} el={float(el1):.2f}")
print(f"Wigner rel error vs re-encode: {rel_w:.3e}")
print(f"Dense rel error vs re-encode: {rel_d:.3e}")
az_w, el_w = doa_from_intensity(a_w)
err = angular_error_deg(float(az1), float(el1), az_w, el_w)
print(f"Intensity DOA after Wigner: ({az_w:.2f},{el_w:.2f}) err={err:.3f}°")
# timing
for _ in range(20):
rotate_yaw_pitch_roll(a0, yaw=yaw, pitch=pitch, roll=roll, method="wigner")
t0 = time.perf_counter()
n = 200
for _ in range(n):
rotate_yaw_pitch_roll(a0, yaw=yaw, pitch=pitch, roll=roll, method="wigner")
tw = (time.perf_counter() - t0) / n
t0 = time.perf_counter()
n2 = 5
for _ in range(n2):
rotate_yaw_pitch_roll(
a0, yaw=yaw, pitch=pitch, roll=roll, method="dense", n_azi=48, n_el=24
)
td = (time.perf_counter() - t0) / n2
print(f"Timing: wigner={tw*1e3:.3f} ms dense={td*1e3:.3f} ms speedup≈{td/tw:.0f}×")
print("Phase 2: Wigner-D rotation is default for pose loops and tools.")
if __name__ == "__main__":
main()
|