change: improved coherence

This commit is contained in:
Benoît Sierro
2023-08-16 16:09:17 +02:00
parent 1dde47a2e1
commit cdd107adb9

View File

@@ -716,7 +716,7 @@ def spectrogram(
return spec return spec
def g12(values: np.ndarray, axis: int = 0): def g12(values: np.ndarray, axis: int = 0, rel_cutoff: float = 1e-16) -> np.ndarray:
""" """
computes the first order coherence function of a ensemble of values computes the first order coherence function of a ensemble of values
@@ -725,7 +725,9 @@ def g12(values: np.ndarray, axis: int = 0):
values : np.ndarray, shape (..., n, nt) values : np.ndarray, shape (..., n, nt)
complex values following sc-ordering complex values following sc-ordering
axis : int, optional axis : int, optional
axis to collapse on which to compute the coherence axis to collapse on which to compute the coherence, by default 0
rel_cutoff : float, optional
relative mean spectrum intensity above which to compute coherence
Returns Returns
------- -------
@@ -736,7 +738,7 @@ def g12(values: np.ndarray, axis: int = 0):
n = len(values) n = len(values)
mean_spec = np.mean(math.abs2(values), axis=axis) mean_spec = np.mean(math.abs2(values), axis=axis)
corr = np.zeros_like(mean_spec, dtype=complex) corr = np.zeros_like(mean_spec, dtype=complex)
mask = mean_spec > 1e-15 * mean_spec.max() mask = mean_spec > rel_cutoff * mean_spec.max()
corr[mask] = _g12_fast(values[..., mask]) corr[mask] = _g12_fast(values[..., mask])
corr[mask] = corr[mask] / (n * (n - 1) / 2 * mean_spec[mask]) corr[mask] = corr[mask] / (n * (n - 1) / 2 * mean_spec[mask])
return np.abs(corr) return np.abs(corr)