@@ -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
0 commit comments