.. 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-83 .. 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 84-88 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 88-107 .. 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 108-110 Calculate ISO 532-1 loudness for a stationary sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 112-114 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 114-117 .. code-block:: Python loudness_ISO532_1_stationary = LoudnessISO532_1_Stationary(signal=signal_flute) loudness_ISO532_1_stationary.process() .. GENERATED FROM PYTHON SOURCE LINES 118-119 Get the ISO 532-1 loudness (in sone) and loudness level (in phon). .. GENERATED FROM PYTHON SOURCE LINES 119-128 .. 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 129-130 Plot the ISO 532-1 specific loudness, that is, the loudness at each Bark band index. .. GENERATED FROM PYTHON SOURCE LINES 130-132 .. 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 133-135 Calculate ISO 532-1 loudness for a time-varying sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 137-138 Create a :class:`.LoudnessISO532_1_TimeVarying` object, set its signal, and compute the loudness. .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. 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 142-143 Get percentile loudness and loudness level values. .. GENERATED FROM PYTHON SOURCE LINES 143-157 .. 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 158-159 Plot the ISO 532-1 loudness as a function of time. .. GENERATED FROM PYTHON SOURCE LINES 159-161 .. 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 162-164 Calculate ISO 532-2 loudness for a stationary sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 166-168 Create a :class:`.LoudnessISO532_2` object, set its signal, and compute the loudness according to standard ISO 532-2. .. GENERATED FROM PYTHON SOURCE LINES 168-171 .. code-block:: Python loudness_ISO532_2 = LoudnessISO532_2(signal=signal_flute, field_type="Free") loudness_ISO532_2.process() .. GENERATED FROM PYTHON SOURCE LINES 172-173 Get the ISO 532-2 binaural and monaural loudness (in sone), and loudness level (in phon). .. GENERATED FROM PYTHON SOURCE LINES 173-186 .. 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 187-189 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 189-195 .. 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 196-198 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 198-201 .. 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 202-204 Calculate sharpness, roughness, and fluctuation strength ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 206-207 Calculate the sharpness. .. GENERATED FROM PYTHON SOURCE LINES 207-211 .. code-block:: Python sharpness = Sharpness(signal=signal_flute, field_type="Free") sharpness.process() sharpness_value = sharpness.get_sharpness() .. GENERATED FROM PYTHON SOURCE LINES 212-213 Calculate the sharpness over time and plot it. .. GENERATED FROM PYTHON SOURCE LINES 213-217 .. 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 218-219 Calculate the sharpness according to standard DIN 45692. .. GENERATED FROM PYTHON SOURCE LINES 219-223 .. 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 224-225 Calculate the sharpness according to standard DIN 45692, over time, and plot it. .. GENERATED FROM PYTHON SOURCE LINES 225-229 .. 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 230-231 Calculate the roughness, and plot the specific roughness and roughness over time. .. GENERATED FROM PYTHON SOURCE LINES 231-241 .. 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 242-243 Calculate the fluctuation strength. .. GENERATED FROM PYTHON SOURCE LINES 243-247 .. 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 248-249 Print the calculated indicators. .. GENERATED FROM PYTHON SOURCE LINES 249-256 .. 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 18.615 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 `_