Skip to content

Commit e3eb3dc

Browse files
author
Martin Glesser
committed
[CC] avoid empty list in _spectrum_smoothing
1 parent e935a3c commit e3eb3dc

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

mosqito/sq_metrics/tonality/tone_to_noise_ecma/_spectrum_smoothing.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,17 @@ def _spectrum_smoothing(freqs_in, spec, noct, low_freq, high_freq, freqs_out):
3737
3838
"""
3939
n = spec.shape[0]
40-
if len(spec.shape)>1:
40+
if len(spec.shape) > 1:
4141
m = spec.shape[1]
4242
else:
4343
m = spec.shape[0]
4444
n = 1
4545
spec = spec.ravel()
46-
stop = np.arange(1,n+1)*m
46+
stop = np.arange(1, n + 1) * m
4747
freqs_in = freqs_in.ravel()
4848

4949
# n-th octave bands filter
50-
filter_freqs = _getFrequencies(
51-
low_freq, high_freq, noct, G=10, fr=1000)["f"]
50+
filter_freqs = _getFrequencies(low_freq, high_freq, noct, G=10, fr=1000)["f"]
5251
filter_freqs[len(filter_freqs) - 1, 2] = high_freq
5352
filter_freqs[0, 0] = low_freq
5453

@@ -63,7 +62,6 @@ def _spectrum_smoothing(freqs_in, spec, noct, low_freq, high_freq, freqs_out):
6362
(freqs_in >= filter_freqs[i, 0]) & (freqs_in <= filter_freqs[i, 2])
6463
)[0]
6564

66-
6765
# If the frequency bin is empty, it is deleted from the list
6866
if len(bin_index) == 0:
6967
smoothed_spectrum = np.delete(smoothed_spectrum, i, axis=1)
@@ -74,27 +72,43 @@ def _spectrum_smoothing(freqs_in, spec, noct, low_freq, high_freq, freqs_out):
7472
# The spectral components within the frequency bin are averaged on an energy basis
7573
spec_sum = np.zeros((n))
7674
for j in range(n):
77-
for k in bin_index[(bin_index<stop[j]) & (bin_index>(stop[j]-m))]:
78-
spec_sum[j] += 10 ** (spec[k] / 10)
79-
smoothed_spectrum[:,i] = 10 * np.log10(spec_sum / len(bin_index[(bin_index<stop[j]) & (bin_index>(stop[j]-m))]))
75+
if (
76+
len(bin_index[(bin_index < stop[j]) & (bin_index > (stop[j] - m))])
77+
!= 0
78+
):
79+
spec_sum[j] = np.mean(
80+
10
81+
** (
82+
spec[
83+
bin_index[
84+
(bin_index < stop[j]) & (bin_index > (stop[j] - m))
85+
]
86+
]
87+
/ 10
88+
)
89+
)
90+
else:
91+
spec_sum[j] = 1e-12
92+
smoothed_spectrum[:, i] = 10 * np.log10(
93+
spec_sum
94+
# / len(bin_index[(bin_index < stop[j]) & (bin_index > (stop[j] - m))])
95+
)
8096
nb_bands -= 1
8197
i += 1
8298
# Pose of the smoothed spectrum on the frequency-axis
8399
low = np.zeros((n, filter_freqs.shape[0]))
84100
high = np.zeros((n, filter_freqs.shape[0]))
85-
101+
86102
# Index of the lower and higher limit of each frequency bin into the original spectrum
87103
for i in range(len(filter_freqs)):
88-
low[:,i] = np.argmin(np.abs(freqs_out - filter_freqs[i, 0]))
89-
high[:,i] = np.argmin(np.abs(freqs_out - filter_freqs[i, 2]))
104+
low[:, i] = np.argmin(np.abs(freqs_out - filter_freqs[i, 0]))
105+
high[:, i] = np.argmin(np.abs(freqs_out - filter_freqs[i, 2]))
90106
low = low.astype(int)
91107
high = high.astype(int)
92-
93-
94-
smooth_spec = np.zeros((n,m))
108+
109+
smooth_spec = np.zeros((n, m))
95110
for i in range(n):
96111
for j in range(filter_freqs.shape[0]):
97-
smooth_spec[i,low[i,j]: high[i,j]] = smoothed_spectrum[i,j]
98-
112+
smooth_spec[i, low[i, j] : high[i, j]] = smoothed_spectrum[i, j]
99113

100114
return smooth_spec

tests/sq_metrics/tonality/test_tnr_ecma_tv.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
try:
44
import pytest
55
except ImportError:
6-
raise RuntimeError(
7-
"In order to perform the tests you need the 'pytest' package."
8-
)
6+
raise RuntimeError("In order to perform the tests you need the 'pytest' package.")
97

8+
import numpy as np
109

1110
# Local application imports
1211
from mosqito.utils import load
@@ -38,6 +37,9 @@ def test_tnr_ecma_tv():
3837

3938
# Compute tone-to-noise ratio
4039
t_tnr, tnr, prom, freq, time = tnr_ecma_tv(audio, fs, prominence=True)
40+
np.testing.assert_almost_equal(max(t_tnr), 34.995108238375025)
41+
assert np.count_nonzero(prom == True) == 6
42+
4143

4244
if __name__ == "__main__":
4345
test_tnr_ecma_tv()

0 commit comments

Comments
 (0)