12
12
)
13
13
from mosqito .utils .conversion import freq2bark , db2amp , amp2db , bark2freq
14
14
15
-
16
- def _roughness_dw_main_calc (spectrum , freqs , fs , gzi , hWeight ):
15
+ def _roughness_dw_main_calc (spec , freq_axis , fs , gzi , hWeight ):
17
16
"""
18
17
Daniel and Weber roughness main calculation
19
18
20
19
Parameters
21
20
----------
22
- spectrum : array
21
+ spec : array
23
22
An amplitude or complex spectrum.
24
- freqs : array
23
+ freq_axis : array
25
24
Frequency axis in [Hz].
26
25
fs : integer
27
26
Sampling frequency.
@@ -36,29 +35,31 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
36
35
Roughness computed for the given spectrum.
37
36
38
37
"""
39
-
40
- if len (spectrum ) != len (freqs ):
38
+ if len (spec ) != len (freq_axis ):
41
39
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 !"
43
41
)
44
42
45
- n = len (spectrum )
43
+ # convert spectrum to 2-sided
44
+ spec = np .concatenate ((spec , spec [len (spec )::- 1 ]))
45
+
46
+ n = len (spec )
46
47
# Frequency axis in Bark
47
- barks = freq2bark (freqs )
48
+ bark_axis = freq2bark (freq_axis )
48
49
# Highest frequency
49
- nZ = np .arange (1 , n + 1 , 1 )
50
+ nZ = np .arange (1 , n // 2 + 1 , 1 )
50
51
51
52
# Calculate Zwicker a0 factor (transfer characteristic of the outer and inner ear)
52
53
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
55
56
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 ] )
58
59
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" )
62
63
audible_index = np .where (spec_dB > threshold )[0 ]
63
64
# Number of audible frequencies
64
65
n_aud = len (audible_index )
@@ -73,7 +74,7 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
73
74
# upper slope [dB/Bark]
74
75
for k in np .arange (0 , n_aud , 1 ):
75
76
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 ]]),
77
78
0 ,
78
79
)
79
80
@@ -85,20 +86,20 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
85
86
zb = bark2freq (zi ) * n / fs
86
87
# Minimum excitation level
87
88
minExcitDB = np .interp (zb , nZ , threshold )
88
-
89
+
89
90
ch_low = np .zeros ((n_aud ))
90
91
ch_high = np .zeros ((n_aud ))
91
92
for i in np .arange (0 , n_aud ):
92
93
# 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
94
95
# 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
96
97
97
98
# Creation of the excitation pattern
98
99
slopes = np .zeros ((n_aud , n_channel ))
99
100
for k in np .arange (0 , n_aud ):
100
101
levDB = spec_dB [audible_index [k ]]
101
- b = barks [audible_index [k ]]
102
+ b = bark_axis [audible_index [k ]]
102
103
for j in np .arange (0 , int (ch_low [k ] + 1 )):
103
104
sl = (s1 * (b - ((j + 1 ) * 0.5 ))) + levDB
104
105
if sl > minExcitDB [j ]:
@@ -129,24 +130,22 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
129
130
else :
130
131
ampl = slopes [j , i - 1 ] / module [ind ]
131
132
132
- # reconstruction of the spectrum
133
- exc [ind ] = ampl * spectrum [ind ]
133
+ # reconstruction of the spec
134
+ exc [ind ] = ampl * spec [ind ]
134
135
135
136
# The temporal specific excitation functions are obtained by IFFT
136
137
temporal_excitation = np .abs (n * np .real (ifft (exc )))
137
-
138
138
# ------------------------------- stage 2 --------------------------------------
139
139
# ---------------------modulation depth calculation-----------------------------
140
140
141
141
# 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
143
143
h0 = np .mean (temporal_excitation )
144
144
envelope_spec = fft (temporal_excitation - h0 )
145
145
146
- # This spectrum is weighted to model the low-frequency bandpass
146
+ # This spec is weighted to model the low-frequency bandpass
147
147
# characteristic of the roughness on modulation frequency
148
148
envelope_spec = envelope_spec * hWeight [i , :]
149
-
150
149
# The time functions of the bandpass filtered envelopes hBPi(t)
151
150
# are calculated via inverse Fourier transform :
152
151
hBP [i , :] = 2 * np .real (ifft (envelope_spec ))
@@ -160,7 +159,6 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
160
159
mod_depth [i ] = 1
161
160
else :
162
161
mod_depth [i ] = 0
163
-
164
162
# ------------------------------- stage 3 --------------------------------------
165
163
# ----------------roughness calculation with cross correlation------------------
166
164
@@ -190,3 +188,6 @@ def _roughness_dw_main_calc(spectrum, freqs, fs, gzi, hWeight):
190
188
R = 0.25 * sum (R_spec )
191
189
192
190
return R , R_spec , zi
191
+
192
+
193
+
0 commit comments