23
23
"""Computes DIN 45681 tonality."""
24
24
import warnings
25
25
26
- from ansys .dpf .core import Field , GenericDataContainersCollection , Operator
26
+ from ansys .dpf .core import Field , GenericDataContainersCollection , Operator , types
27
27
import matplotlib .pyplot as plt
28
28
import numpy as np
29
29
30
30
from . import PsychoacousticsParent
31
31
from .._pyansys_sound import PyAnsysSoundException , PyAnsysSoundWarning
32
32
33
+ # Name of the DPF Sound operator used in this module.
34
+ ID_COMPUTE_TONALITY_DIN_45681 = "compute_tonality_din45681"
35
+
33
36
TONE_TYPES = ("" , "FG" )
34
37
35
38
@@ -46,7 +49,7 @@ def __init__(self, signal: Field = None, window_length: float = 3.0, overlap: fl
46
49
Parameters
47
50
----------
48
51
signal: Field, default: None
49
- Signal in Pa on which to calculate the tonality, as a DPF field .
52
+ Signal in Pa on which to calculate the tonality.
50
53
window_length: float, default: 3.0
51
54
Length, in s, of each slice of the signal used to calculate an average spectrum.
52
55
overlap: float, default: 0.0
@@ -58,24 +61,24 @@ def __init__(self, signal: Field = None, window_length: float = 3.0, overlap: fl
58
61
self .signal = signal
59
62
self .window_length = window_length
60
63
self .overlap = overlap
61
- self .__operator = Operator ("compute_tonality_din45681" )
64
+ self .__operator = Operator (ID_COMPUTE_TONALITY_DIN_45681 )
62
65
63
66
def __str__ (self ):
64
67
"""Overloads the __str__ method."""
65
68
return (
66
69
f"{ __class__ .__name__ } object.\n "
67
70
"Data\n "
68
- f'Signal name: "{ self .signal .name } "\n '
69
- f"Window length: { self .window_length } s\n "
70
- f"Overlap : { self .overlap } %\n "
71
- f"Mean difference DL: "
71
+ f'\t Signal name: "{ self .signal .name } "\n '
72
+ f"\t Window length: { self .window_length } s\n "
73
+ f"\t Overlap : { self .overlap } %\n "
74
+ f"Mean tonality ( difference DL) : "
72
75
f"{ self .get_mean_difference ():.1f} (+/-{ self .get_uncertainty ():.1f} ) dB\n "
73
- f"Tonal adjustment Kt: { self .get_tonal_adjustment ():.0f} dB\n "
76
+ f"Tonal adjustment Kt: { self .get_tonal_adjustment ():.0f} dB"
74
77
)
75
78
76
79
@property
77
80
def signal (self ) -> Field :
78
- """Signal in Pa, as a DPF field . Default is None."""
81
+ """Signal in Pa. Default is None."""
79
82
return self .__signal
80
83
81
84
@signal .setter
@@ -121,7 +124,7 @@ def process(self):
121
124
"""
122
125
if self .signal == None :
123
126
raise PyAnsysSoundException (
124
- f"No input signal defined. Use `` { __class__ .__name__ } .signal` `."
127
+ f"No input signal defined. Use `{ __class__ .__name__ } .signal`."
125
128
)
126
129
127
130
# Connect the operator input(s).
@@ -134,13 +137,13 @@ def process(self):
134
137
135
138
# Store the operator outputs in a tuple.
136
139
self ._output = (
137
- self .__operator .get_output (0 , " double" ),
138
- self .__operator .get_output (1 , " double" ),
139
- self .__operator .get_output (2 , " double" ),
140
- self .__operator .get_output (3 , " field" ),
141
- self .__operator .get_output (4 , " field" ),
142
- self .__operator .get_output (5 , " field" ),
143
- self .__operator .get_output (6 , " field" ),
140
+ self .__operator .get_output (0 , types . double ),
141
+ self .__operator .get_output (1 , types . double ),
142
+ self .__operator .get_output (2 , types . double ),
143
+ self .__operator .get_output (3 , types . field ),
144
+ self .__operator .get_output (4 , types . field ),
145
+ self .__operator .get_output (5 , types . field ),
146
+ self .__operator .get_output (6 , types . field ),
144
147
self .__operator .get_output (7 , GenericDataContainersCollection ),
145
148
)
146
149
@@ -150,29 +153,29 @@ def get_output(self) -> tuple:
150
153
Returns
151
154
-------
152
155
tuple
153
- First element (float) is the DIN 45681 tonality (mean difference DL), in dB.
156
+ - First element (float) is the DIN 45681 tonality (mean difference DL), in dB.
154
157
155
- Second element (float) is the DIN 45681 tonality uncertainty, in dB.
158
+ - Second element (float) is the DIN 45681 tonality uncertainty, in dB.
156
159
157
- Third element (float) is the DIN 45681 tonal adjustment Kt, in dB.
160
+ - Third element (float) is the DIN 45681 tonal adjustment Kt, in dB.
158
161
159
- Fourth element (Field) is the DIN 45681 tonality over time
160
- (decisive difference DLj), in dB.
162
+ - Fourth element (Field) is the DIN 45681 tonality over time
163
+ (decisive difference DLj), in dB.
161
164
162
- Fifth element (Field) is the DIN 45681 tonality uncertainty over time, in dB.
165
+ - Fifth element (Field) is the DIN 45681 tonality uncertainty over time, in dB.
163
166
164
- Sixth element (Field) is the DIN 45681 decisive frequency over time, in Hz.
167
+ - Sixth element (Field) is the DIN 45681 decisive frequency over time, in Hz.
165
168
166
- Seventh element (Field) is the DIN 45681 tonal adjustment Kt over time, in dB.
169
+ - Seventh element (Field) is the DIN 45681 tonal adjustment Kt over time, in dB.
167
170
168
- Eighth element (GenericDataContainer) is the DIN 45681 tonality details (individual
169
- tone data for each spectrum).
171
+ - Eighth element (GenericDataContainer) is the DIN 45681 tonality details (individual
172
+ tone data for each spectrum).
170
173
"""
171
174
if self ._output == None :
172
175
warnings .warn (
173
176
PyAnsysSoundWarning (
174
177
f"Output is not processed yet. "
175
- f"Use the `` { __class__ .__name__ } .process()` ` method."
178
+ f"Use the `{ __class__ .__name__ } .process()` method."
176
179
)
177
180
)
178
181
@@ -184,21 +187,21 @@ def get_output_as_nparray(self) -> tuple[np.ndarray]:
184
187
Returns
185
188
-------
186
189
tuple[numpy.ndarray]
187
- First element is the DIN 45681 tonality (mean difference DL), in dB.
190
+ - First element is the DIN 45681 tonality (mean difference DL), in dB.
188
191
189
- Second element is the DIN 45681 tonality uncertainty, in dB.
192
+ - Second element is the DIN 45681 tonality uncertainty, in dB.
190
193
191
- Third element is the DIN 45681 tonal adjustment Kt, in dB.
194
+ - Third element is the DIN 45681 tonal adjustment Kt, in dB.
192
195
193
- Fourth element is the DIN 45681 tonality over time (decisive difference DLj), in dB.
196
+ - Fourth element is the DIN 45681 tonality over time (decisive difference DLj), in dB.
194
197
195
- Fifth element is the DIN 45681 tonality uncertainty over time, in dB.
198
+ - Fifth element is the DIN 45681 tonality uncertainty over time, in dB.
196
199
197
- Sixth element is the DIN 45681 decisive frequency over time, in Hz.
200
+ - Sixth element is the DIN 45681 decisive frequency over time, in Hz.
198
201
199
- Seventh element is the DIN 45681 tonal adjustment Kt over time, in dB.
202
+ - Seventh element is the DIN 45681 tonal adjustment Kt over time, in dB.
200
203
201
- Eighth element is the time scale, in s.
204
+ - Eighth element is the time scale, in s.
202
205
"""
203
206
output = self .get_output ()
204
207
@@ -318,25 +321,25 @@ def get_spectrum_number(self) -> int:
318
321
"""
319
322
return len (self .get_output ()[3 ])
320
323
321
- def get_spectrum_details (self , spectrum_index : int = 0 ) -> tuple [float , float , float ]:
324
+ def get_spectrum_details (self , spectrum_index : int ) -> tuple [float ]:
322
325
"""Get the spectrum data for a specific spectrum.
323
326
324
327
Returns the data (decisive difference, uncertainty, and decisive frequency) corresponding
325
328
to a specific spectrum (time step).
326
329
327
330
Parameters
328
331
----------
329
- spectrum_index: int, default: 0
330
- Index of the spectrum to get .
332
+ spectrum_index: int
333
+ Index of the spectrum. The index is 0-based .
331
334
332
335
Returns
333
336
-------
334
- tuple[float,float,float ]
335
- Decisive difference DLj in dB.
337
+ tuple[float]
338
+ - First element is the decisive difference DLj in dB.
336
339
337
- Uncertainty in dB.
340
+ - Second element is the uncertainty in dB.
338
341
339
- Decisive frequency in Hz.
342
+ - Third element is the decisive frequency in Hz.
340
343
"""
341
344
# Check validity of the input spectrum index.
342
345
self .__check_spectrum_index (spectrum_index )
@@ -347,11 +350,16 @@ def get_spectrum_details(self, spectrum_index: int = 0) -> tuple[float, float, f
347
350
self .get_output_as_nparray ()[5 ][spectrum_index ],
348
351
)
349
352
350
- def get_tone_number (self , spectrum_index : int = 0 ) -> int :
353
+ def get_tone_number (self , spectrum_index : int ) -> int :
351
354
"""Get the number of tones for a specific spectrum.
352
355
353
356
Returns the number of tones detected in a specific spectrum (time step).
354
357
358
+ Parameters
359
+ ----------
360
+ spectrum_index: int
361
+ Index of the spectrum where the tone was detected. The index is 0-based.
362
+
355
363
Returns
356
364
-------
357
365
int
@@ -367,43 +375,42 @@ def get_tone_number(self, spectrum_index: int = 0) -> int:
367
375
368
376
return len (spectrum .get_property ("differences" ))
369
377
370
- def get_tone_details (
371
- self , spectrum_index : int = 0 , tone_index : int = 0
372
- ) -> tuple [float , float , float , str , float , float , float , float , float , float ]:
378
+ def get_tone_details (self , spectrum_index : int , tone_index : int ) -> tuple :
373
379
"""Get the tone data, for a specific spectrum.
374
380
375
381
Returns all data associated with a specific detected tone, in a specific spectrum (time
376
382
step).
377
383
378
384
Parameters
379
385
----------
380
- spectrum_index: int, default: 0
381
- Index of the spectrum where the tone was detected.
382
- tone_index: int, default: 0
383
- Index of the tone whose details are requested.
386
+ spectrum_index: int
387
+ Index of the spectrum where the tone was detected. The index is 0-based.
388
+ tone_index: int
389
+ Index of the tone whose details are requested. The index is 0-based.
384
390
385
391
Returns
386
392
-------
387
- tuple[float,float,float,str,float,float,float,float,float,float]
388
- Decisive difference DLj in dB.
393
+ tuple
394
+ - First element (float) is the decisive difference DLj in dB.
389
395
390
- Uncertainty , in dB.
396
+ - Second element (float) is the uncertainty , in dB.
391
397
392
- Decisive frequency, in Hz.
398
+ - Third element (float) is the decisive frequency, in Hz.
393
399
394
- Tone type ('' for individual tones, or 'FG' for groups of tones).
400
+ - Fourth element (str) is the tone type ('' for individual tones, or 'FG' for groups
401
+ of tones).
395
402
396
- Critical band lower limit, in Hz.
403
+ - Fifth element (float) is the critical band lower limit, in Hz.
397
404
398
- Critical band upper limit, in Hz.
405
+ - Sixth element (float) is the critical band upper limit, in Hz.
399
406
400
- Mean narrow-band masking noise level Ls, in dBA.
407
+ - Seventh element (float) is the mean narrow-band masking noise level Ls, in dBA.
401
408
402
- Tone level Lt, in dBA.
409
+ - Eighth element (float) is the tone level Lt, in dBA.
403
410
404
- Masking noise level Lg, in dBA.
411
+ - Ninth element (float) is the masking noise level Lg, in dBA.
405
412
406
- Masking index av, in dB.
413
+ - Tenth element (float) is the masking index av, in dB.
407
414
"""
408
415
# Check validities of input indexes.
409
416
self .__check_spectrum_index (spectrum_index )
@@ -440,7 +447,7 @@ def plot(self):
440
447
"""
441
448
if self ._output == None :
442
449
raise PyAnsysSoundException (
443
- f"Output is not processed yet. Use the `` { __class__ .__name__ } .process()` ` method."
450
+ f"Output is not processed yet. Use the `{ __class__ .__name__ } .process()` method."
444
451
)
445
452
446
453
# Get data to plot
0 commit comments