1212)
1313from mosqito .utils .conversion import freq2bark , db2amp , amp2db , bark2freq
1414
15-
16- def _roughness_dw_main_calc (spectrum , freqs , fs , gzi , hWeight ):
15+ def _roughness_dw_main_calc (spec , freq_axis , fs , gzi , hWeight ):
1716 """
1817 Daniel and Weber roughness main calculation
1918
2019 Parameters
2120 ----------
22- spectrum : array
21+ spec : array
2322 An amplitude or complex spectrum.
24- freqs : array
23+ freq_axis : array
2524 Frequency axis in [Hz].
2625 fs : integer
2726 Sampling frequency.
@@ -36,29 +35,31 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
3635 Roughness computed for the given spectrum.
3736
3837 """
39-
40- if len (spectrum ) != len (freqs ):
38+ if len (spec ) != len (freq_axis ):
4139 raise ValueError (
42- "Spectrum and frequency axis should have the same number of points !"
40+ "spectrum and frequency axis should have the same number of points !"
4341 )
4442
45- n = len (spectrum )
43+ # convert spectrum to 2-sided
44+ spec = np .concatenate ((spec , spec [len (spec )::- 1 ]))
45+
46+ n = len (spec )
4647 # Frequency axis in Bark
47- barks = freq2bark (freqs )
48+ bark_axis = freq2bark (freq_axis )
4849 # Highest frequency
49- nZ = np .arange (1 , n + 1 , 1 )
50+ nZ = np .arange (1 , n // 2 + 1 , 1 )
5051
5152 # Calculate Zwicker a0 factor (transfer characteristic of the outer and inner ear)
5253 a0 = np .zeros ((n ))
53- a0 [nZ - 1 ] = db2amp (_ear_filter_coeff (barks ), ref = 1 )
54- spectrum = a0 * spectrum
54+ a0 [nZ - 1 ] = db2amp (_ear_filter_coeff (bark_axis ), ref = 1 )
55+ spec = a0 * spec
5556
56- # Conversion of the spectrum into dB
57- module = np .abs (spectrum )
57+ # Conversion of the spec into dB
58+ module = np .abs (spec [ 0 : n // 2 ] )
5859 spec_dB = amp2db (module , ref = 2e-5 )
59-
60- # Find the audible components within the spectrum
61- threshold = LTQ (barks , reference = "roughness" )
60+
61+ # Find the audible components within the spec
62+ threshold = LTQ (bark_axis , reference = "roughness" )
6263 audible_index = np .where (spec_dB > threshold )[0 ]
6364 # Number of audible frequencies
6465 n_aud = len (audible_index )
@@ -73,7 +74,7 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
7374 # upper slope [dB/Bark]
7475 for k in np .arange (0 , n_aud , 1 ):
7576 s2 [k ] = min (
76- - 24 - (230 / freqs [audible_index [k ]]) + (0.2 * spec_dB [audible_index [k ]]),
77+ - 24 - (230 / freq_axis [audible_index [k ]]) + (0.2 * spec_dB [audible_index [k ]]),
7778 0 ,
7879 )
7980
@@ -85,20 +86,20 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
8586 zb = bark2freq (zi ) * n / fs
8687 # Minimum excitation level
8788 minExcitDB = np .interp (zb , nZ , threshold )
88-
89+
8990 ch_low = np .zeros ((n_aud ))
9091 ch_high = np .zeros ((n_aud ))
9192 for i in np .arange (0 , n_aud ):
9293 # Lower limit of the channel corresponding to each component
93- ch_low [i ] = math .floor (2 * barks [audible_index [i ]]) - 1
94+ ch_low [i ] = math .floor (2 * bark_axis [audible_index [i ]]) - 1
9495 # Higher limit
95- ch_high [i ] = math .ceil (2 * barks [audible_index [i ]]) - 1
96+ ch_high [i ] = math .ceil (2 * bark_axis [audible_index [i ]]) - 1
9697
9798 # Creation of the excitation pattern
9899 slopes = np .zeros ((n_aud , n_channel ))
99100 for k in np .arange (0 , n_aud ):
100101 levDB = spec_dB [audible_index [k ]]
101- b = barks [audible_index [k ]]
102+ b = bark_axis [audible_index [k ]]
102103 for j in np .arange (0 , int (ch_low [k ] + 1 )):
103104 sl = (s1 * (b - ((j + 1 ) * 0.5 ))) + levDB
104105 if sl > minExcitDB [j ]:
@@ -129,24 +130,22 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
129130 else :
130131 ampl = slopes [j , i - 1 ] / module [ind ]
131132
132- # reconstruction of the spectrum
133- exc [ind ] = ampl * spectrum [ind ]
133+ # reconstruction of the spec
134+ exc [ind ] = ampl * spec [ind ]
134135
135136 # The temporal specific excitation functions are obtained by IFFT
136137 temporal_excitation = np .abs (n * np .real (ifft (exc )))
137-
138138 # ------------------------------- stage 2 --------------------------------------
139139 # ---------------------modulation depth calculation-----------------------------
140140
141141 # The fluctuations of the envelope are contained in the low frequency part
142- # of the spectrum of specific excitations in absolute value
142+ # of the spec of specific excitations in absolute value
143143 h0 = np .mean (temporal_excitation )
144144 envelope_spec = fft (temporal_excitation - h0 )
145145
146- # This spectrum is weighted to model the low-frequency bandpass
146+ # This spec is weighted to model the low-frequency bandpass
147147 # characteristic of the roughness on modulation frequency
148148 envelope_spec = envelope_spec * hWeight [i , :]
149-
150149 # The time functions of the bandpass filtered envelopes hBPi(t)
151150 # are calculated via inverse Fourier transform :
152151 hBP [i , :] = 2 * np .real (ifft (envelope_spec ))
@@ -160,7 +159,6 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
160159 mod_depth [i ] = 1
161160 else :
162161 mod_depth [i ] = 0
163-
164162 # ------------------------------- stage 3 --------------------------------------
165163 # ----------------roughness calculation with cross correlation------------------
166164
@@ -190,3 +188,6 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
190188 R = 0.25 * sum (R_spec )
191189
192190 return R , R_spec , zi
191+
192+
193+
0 commit comments