@@ -37,18 +37,17 @@ def _spectrum_smoothing(freqs_in, spec, noct, low_freq, high_freq, freqs_out):
37
37
38
38
"""
39
39
n = spec .shape [0 ]
40
- if len (spec .shape )> 1 :
40
+ if len (spec .shape ) > 1 :
41
41
m = spec .shape [1 ]
42
42
else :
43
43
m = spec .shape [0 ]
44
44
n = 1
45
45
spec = spec .ravel ()
46
- stop = np .arange (1 ,n + 1 ) * m
46
+ stop = np .arange (1 , n + 1 ) * m
47
47
freqs_in = freqs_in .ravel ()
48
48
49
49
# 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" ]
52
51
filter_freqs [len (filter_freqs ) - 1 , 2 ] = high_freq
53
52
filter_freqs [0 , 0 ] = low_freq
54
53
@@ -63,7 +62,6 @@ def _spectrum_smoothing(freqs_in, spec, noct, low_freq, high_freq, freqs_out):
63
62
(freqs_in >= filter_freqs [i , 0 ]) & (freqs_in <= filter_freqs [i , 2 ])
64
63
)[0 ]
65
64
66
-
67
65
# If the frequency bin is empty, it is deleted from the list
68
66
if len (bin_index ) == 0 :
69
67
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):
74
72
# The spectral components within the frequency bin are averaged on an energy basis
75
73
spec_sum = np .zeros ((n ))
76
74
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
+ )
80
96
nb_bands -= 1
81
97
i += 1
82
98
# Pose of the smoothed spectrum on the frequency-axis
83
99
low = np .zeros ((n , filter_freqs .shape [0 ]))
84
100
high = np .zeros ((n , filter_freqs .shape [0 ]))
85
-
101
+
86
102
# Index of the lower and higher limit of each frequency bin into the original spectrum
87
103
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 ]))
90
106
low = low .astype (int )
91
107
high = high .astype (int )
92
-
93
-
94
- smooth_spec = np .zeros ((n ,m ))
108
+
109
+ smooth_spec = np .zeros ((n , m ))
95
110
for i in range (n ):
96
111
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 ]
99
113
100
114
return smooth_spec
0 commit comments