improved to_dB

This commit is contained in:
Benoît Sierro
2023-10-02 15:16:48 +02:00
parent 04d5939215
commit f52d507416

View File

@@ -3,6 +3,7 @@ collection of purely mathematical function
"""
import math
import warnings
from dataclasses import dataclass
from functools import cache
from typing import Sequence
@@ -142,12 +143,15 @@ def to_dB(arr: np.ndarray, ref=None, axis=None) -> np.ndarray:
if axis is not None and arr.ndim > 1 and ref is None:
return np.apply_along_axis(to_dB, axis, arr)
out = np.ones_like(arr)
if ref is None:
ref = np.max(arr)
above_0 = arr > 0
if not np.any(above_0) or ref <= 0:
warnings.warn("invalid array to convert to dB, returning 0 instead")
return out
m = arr / ref
above_0 = m > 0
m = 10 * np.log10(m, out=np.ones_like(m) * (10 * np.log10(m[above_0].min())), where=above_0)
return m
return 10 * np.log10(m, out=out * (10 * np.log10(m[above_0].min())), where=above_0)
def u_nm(n, m):