improved to_dB
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user