.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\gallery_examples\007_calculate_psychoacoustic_indicators.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gallery_examples_007_calculate_psychoacoustic_indicators.py: .. _calculate_psychoacoustic_indicators: Calculate psychoacoustic indicators ----------------------------------- This example shows how to calculate psychoacoustic indicators. The following indicators are included: - Loudness of stationary sounds according to ISO 532-1. - Loudness of time-varying sounds, according to ISO 532-1. - Monaural and binaural loudness of stationary sounds according to ISO 532-2. - Sharpness according to Zwicker and Fastl, "Psychoacoustics: Facts and models", 1990. - Sharpness according to Zwicker and Fastl, over time. - Sharpness according to DIN 45692. - Sharpness according to DIN 45692, over time. - Roughness and roughness over time according to Daniel and Weber, "Psychoacoustical Roughness: Implementation of an Optimized Model, 1997. - Fluctuation strength according to Sontacchi, "Entwicklung eines Modulkonzeptes für die psychoakustische Geräuschanalyse under MatLab", 1998. The example shows how to perform these operations: - Set up the analysis. - Calculate indicators on loaded WAV files. - Get calculation outputs. - Plot some corresponding curves. .. GENERATED FROM PYTHON SOURCE LINES 54-57 Set up analysis ~~~~~~~~~~~~~~~ Setting up the analysis consists of loading libraries, and connecting to the DPF server. .. GENERATED FROM PYTHON SOURCE LINES 57-86 .. code-block:: Python # Load standard libraries. import os # Load Ansys libraries. from ansys.sound.core.examples_helpers import ( download_accel_with_rpm_wav, download_flute_wav, ) from ansys.sound.core.psychoacoustics import ( FluctuationStrength, LoudnessISO532_1_Stationary, LoudnessISO532_1_TimeVarying, LoudnessISO532_2, Roughness, Sharpness, SharpnessDIN45692, SharpnessDIN45692OverTime, SharpnessOverTime, ) from ansys.sound.core.psychoacoustics.roughness import Roughness from ansys.sound.core.psychoacoustics.sharpness import Sharpness from ansys.sound.core.server_helpers import connect_to_or_start_server from ansys.sound.core.signal_utilities import LoadWav # Connect to a remote DPF server or start a local DPF server. my_server, lic_context = connect_to_or_start_server(use_license_context=True) .. GENERATED FROM PYTHON SOURCE LINES 87-91 Load the WAV files used in this example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load the signals from WAV files using the :class:`.LoadWav` class. They are returned as :class:`FieldsContainer ` objects. .. GENERATED FROM PYTHON SOURCE LINES 91-110 .. code-block:: Python # Load a first signal from a WAV file: recording of a flute. path_flute_wav = download_flute_wav(server=my_server) wav_loader = LoadWav(path_flute_wav) wav_loader.process() signal_flute = wav_loader.get_output()[0] # Load another signal from a WAV file: recording of an acceleration inside a car cabin. path_accel_wav = download_accel_with_rpm_wav(server=my_server) wav_loader = LoadWav(path_accel_wav) wav_loader.process() signal_car_acceleration = wav_loader.get_output()[0] # Note: This signal contains a second field (RPM profile) that is useless in this example. # Get file names, for later use. file_name_flute = os.path.basename(path_flute_wav) file_name_car_acceleration = os.path.basename(path_accel_wav) .. GENERATED FROM PYTHON SOURCE LINES 111-113 Calculate ISO 532-1 loudness for a stationary sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 115-117 Create a :class:`.LoudnessISO532_1_Stationary` object, set its signal, and compute the loudness according to standard ISO 532-1. .. GENERATED FROM PYTHON SOURCE LINES 117-120 .. code-block:: Python loudness_ISO532_1_stationary = LoudnessISO532_1_Stationary(signal=signal_flute) loudness_ISO532_1_stationary.process() .. GENERATED FROM PYTHON SOURCE LINES 121-122 Get the ISO 532-1 loudness (in sone) and loudness level (in phon). .. GENERATED FROM PYTHON SOURCE LINES 122-131 .. code-block:: Python loudness_ISO532_1_sone = loudness_ISO532_1_stationary.get_loudness_sone() loudness_ISO532_1_level_phon = loudness_ISO532_1_stationary.get_loudness_level_phon() print( f"\nThe ISO 532-1 loudness of sound file {file_name_flute} is " f"{loudness_ISO532_1_sone:.1f} sones " f"and its loudness level is {loudness_ISO532_1_level_phon:.1f} phons." ) .. rst-class:: sphx-glr-script-out .. code-block:: none The ISO 532-1 loudness of sound file flute.wav is 39.6 sones and its loudness level is 93.1 phons. .. GENERATED FROM PYTHON SOURCE LINES 132-133 Plot the ISO 532-1 specific loudness, that is, the loudness at each Bark band index. .. GENERATED FROM PYTHON SOURCE LINES 133-135 .. code-block:: Python loudness_ISO532_1_stationary.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_001.png :alt: Specific loudness :srcset: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 136-138 Calculate ISO 532-1 loudness for a time-varying sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 140-141 Create a :class:`.LoudnessISO532_1_TimeVarying` object, set its signal, and compute the loudness. .. GENERATED FROM PYTHON SOURCE LINES 141-144 .. code-block:: Python loudness_ISO532_1_time_varying = LoudnessISO532_1_TimeVarying(signal=signal_car_acceleration) loudness_ISO532_1_time_varying.process() .. GENERATED FROM PYTHON SOURCE LINES 145-146 Get percentile loudness and loudness level values. .. GENERATED FROM PYTHON SOURCE LINES 146-160 .. code-block:: Python N5 = loudness_ISO532_1_time_varying.get_N5_sone() N10 = loudness_ISO532_1_time_varying.get_N10_sone() L5 = loudness_ISO532_1_time_varying.get_L5_phon() L10 = loudness_ISO532_1_time_varying.get_L10_phon() print( f"\nThe sound file {file_name_car_acceleration} has the following percentile " f"loudness and loudness level values:\n" f"- N5 = {N5:.1f} sones.\n" f"- N10 = {N10:.1f} sones.\n" f"- L5 = {L5:.1f} phons.\n" f"- L10 = {L10:.1f} phons." ) .. rst-class:: sphx-glr-script-out .. code-block:: none The sound file accel_with_rpm.wav has the following percentile loudness and loudness level values: - N5 = 16.4 sones. - N10 = 15.6 sones. - L5 = 80.4 phons. - L10 = 79.6 phons. .. GENERATED FROM PYTHON SOURCE LINES 161-162 Plot the ISO 532-1 loudness as a function of time. .. GENERATED FROM PYTHON SOURCE LINES 162-164 .. code-block:: Python loudness_ISO532_1_time_varying.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_002.png :alt: Instantaneous loudness, Instantaneous loudness level :srcset: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 165-167 Calculate ISO 532-2 loudness for a stationary sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 169-171 Create a :class:`.LoudnessISO532_2` object, set its signal, and compute the loudness according to standard ISO 532-2. .. GENERATED FROM PYTHON SOURCE LINES 171-174 .. code-block:: Python loudness_ISO532_2 = LoudnessISO532_2(signal=signal_flute, field_type="Free") loudness_ISO532_2.process() .. GENERATED FROM PYTHON SOURCE LINES 175-176 Get the ISO 532-2 binaural and monaural loudness (in sone), and loudness level (in phon). .. GENERATED FROM PYTHON SOURCE LINES 176-189 .. code-block:: Python loudness_ISO532_2_sone_monaural = loudness_ISO532_2.get_monaural_loudness_sone()[0] loudness_ISO532_2_phon_monaural = loudness_ISO532_2.get_monaural_loudness_level_phon()[0] loudness_ISO532_2_sone_binaural = loudness_ISO532_2.get_binaural_loudness_sone() loudness_ISO532_2_phon_binaural = loudness_ISO532_2.get_binaural_loudness_level_phon() print( f"\nThe ISO 532-2 loudness of sound file {file_name_flute} is:\n" f"\tMonaural loudness and loudness level: {loudness_ISO532_2_sone_monaural:.1f} sones, " f"{loudness_ISO532_2_phon_monaural:.1f} phons.\n" f"\tBinaural loudness and loudness level: {loudness_ISO532_2_sone_binaural:.1f} sones, " f"{loudness_ISO532_2_phon_binaural:.1f} phons." ) .. rst-class:: sphx-glr-script-out .. code-block:: none The ISO 532-2 loudness of sound file flute.wav is: Monaural loudness and loudness level: 28.2 sones, 87.7 phons. Binaural loudness and loudness level: 42.3 sones, 93.2 phons. .. GENERATED FROM PYTHON SOURCE LINES 190-192 For comparison, display the values previously obtained with the ISO 532-1 standard. The two standard rely on similar auditory principles but differ in the way they estimate the loudness. .. GENERATED FROM PYTHON SOURCE LINES 192-198 .. code-block:: Python print( f"\nThe ISO 532-1 loudness of sound file {file_name_flute} is " f"{loudness_ISO532_1_sone:.1f} sones " f"and its loudness level is {loudness_ISO532_1_level_phon:.1f} phons." ) .. rst-class:: sphx-glr-script-out .. code-block:: none The ISO 532-1 loudness of sound file flute.wav is 39.6 sones and its loudness level is 93.1 phons. .. GENERATED FROM PYTHON SOURCE LINES 199-201 Plot the ISO 532-2 binaural specific loudness, that is, the binaural loudness at each center frequency of equivalent rectangular bandwidth (ERB). .. GENERATED FROM PYTHON SOURCE LINES 201-204 .. code-block:: Python loudness_ISO532_2.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_003.png :alt: Binaural specific loudness :srcset: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 205-207 Calculate sharpness, roughness, and fluctuation strength ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 209-210 Calculate the sharpness. .. GENERATED FROM PYTHON SOURCE LINES 210-214 .. code-block:: Python sharpness = Sharpness(signal=signal_flute, field_type="Free") sharpness.process() sharpness_value = sharpness.get_sharpness() .. GENERATED FROM PYTHON SOURCE LINES 215-216 Calculate the sharpness over time and plot it. .. GENERATED FROM PYTHON SOURCE LINES 216-220 .. code-block:: Python sharpness_over_time = SharpnessOverTime(signal=signal_flute, field_type="Free") sharpness_over_time.process() sharpness_over_time.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_004.png :alt: Sharpness (Zwicker & Fastl) :srcset: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 221-222 Calculate the sharpness according to standard DIN 45692. .. GENERATED FROM PYTHON SOURCE LINES 222-226 .. code-block:: Python sharpness_DIN45692 = SharpnessDIN45692(signal=signal_flute, field_type="Free") sharpness_DIN45692.process() sharpness_DIN45692_value = sharpness_DIN45692.get_sharpness() .. GENERATED FROM PYTHON SOURCE LINES 227-228 Calculate the sharpness according to standard DIN 45692, over time, and plot it. .. GENERATED FROM PYTHON SOURCE LINES 228-232 .. code-block:: Python sharpness_DIN45692_over_time = SharpnessDIN45692OverTime(signal=signal_flute, field_type="Free") sharpness_DIN45692_over_time.process() sharpness_DIN45692_over_time.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_005.png :alt: Sharpness DIN 45692 :srcset: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 233-234 Calculate the roughness, and plot the specific roughness and roughness over time. .. GENERATED FROM PYTHON SOURCE LINES 234-244 .. code-block:: Python # Calculate the specific roughness, the roughness over time, and the overall roughness. roughness = Roughness(signal=signal_flute) roughness.process() roughness_value = roughness.get_roughness() # Plot the specific roughness and the roughness over time. roughness_over_time = roughness.get_roughness_over_time() roughness.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_006.png :alt: Specific roughness, Roughness over time :srcset: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 245-246 Calculate the fluctuation strength. .. GENERATED FROM PYTHON SOURCE LINES 246-250 .. code-block:: Python fluctuation_strength = FluctuationStrength(signal=signal_flute) fluctuation_strength.process() fluctuation_strength_values = fluctuation_strength.get_fluctuation_strength() .. GENERATED FROM PYTHON SOURCE LINES 251-252 Print the calculated indicators. .. GENERATED FROM PYTHON SOURCE LINES 252-259 .. code-block:: Python print( f"\nThe sharpness of sound file {file_name_flute} " f"is {sharpness_value:.2f} acum,\n" f"its sharpness according to DIN 45692 is {sharpness_DIN45692_value:.2f} acum,\n" f"its roughness is {roughness_value:.2f} asper,\n" f"and its fluctuation strength is {fluctuation_strength_values:.2f} vacil." ) .. rst-class:: sphx-glr-script-out .. code-block:: none The sharpness of sound file flute.wav is 1.19 acum, its sharpness according to DIN 45692 is 1.20 acum, its roughness is 0.06 asper, and its fluctuation strength is 0.60 vacil. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 19.153 seconds) .. _sphx_glr_download_examples_gallery_examples_007_calculate_psychoacoustic_indicators.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 007_calculate_psychoacoustic_indicators.ipynb <007_calculate_psychoacoustic_indicators.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 007_calculate_psychoacoustic_indicators.py <007_calculate_psychoacoustic_indicators.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 007_calculate_psychoacoustic_indicators.zip <007_calculate_psychoacoustic_indicators.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_