.. 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-58 Set up analysis ~~~~~~~~~~~~~~~ Setting up the analysis consists of loading the required libraries, and connecting to the DPF server. .. GENERATED FROM PYTHON SOURCE LINES 58-82 .. 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.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, my_license_context = connect_to_or_start_server(use_license_context=True) .. GENERATED FROM PYTHON SOURCE LINES 83-87 Load the WAV files used in this example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load the signals from WAV files using the :class:`.LoadWav` class. They are returned as a list of :class:`Field ` objects, where each field corresponds to a channel. .. GENERATED FROM PYTHON SOURCE LINES 87-106 .. 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 107-109 Calculate ISO 532-1 loudness for a stationary sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 111-113 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 113-116 .. code-block:: Python loudness_ISO532_1_stationary = LoudnessISO532_1_Stationary(signal=signal_flute) loudness_ISO532_1_stationary.process() .. GENERATED FROM PYTHON SOURCE LINES 117-118 Get the ISO 532-1 loudness (in sone) and loudness level (in phon). .. GENERATED FROM PYTHON SOURCE LINES 118-127 .. 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 128-129 Plot the ISO 532-1 specific loudness, that is, the loudness at each Bark band index. .. GENERATED FROM PYTHON SOURCE LINES 129-131 .. 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 132-134 Calculate ISO 532-1 loudness for a time-varying sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 136-137 Create a :class:`.LoudnessISO532_1_TimeVarying` object, set its signal, and compute the loudness. .. GENERATED FROM PYTHON SOURCE LINES 137-140 .. 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 141-142 Get percentile loudness and loudness level values. .. GENERATED FROM PYTHON SOURCE LINES 142-156 .. 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 157-158 Plot the ISO 532-1 loudness as a function of time. .. GENERATED FROM PYTHON SOURCE LINES 158-160 .. 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 161-163 Calculate ISO 532-2 loudness for a stationary sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 165-167 Create a :class:`.LoudnessISO532_2` object, set its signal, and compute the loudness according to standard ISO 532-2. .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: Python loudness_ISO532_2 = LoudnessISO532_2(signal=signal_flute, field_type="Free") loudness_ISO532_2.process() .. GENERATED FROM PYTHON SOURCE LINES 171-172 Get the ISO 532-2 binaural and monaural loudness (in sone), and loudness level (in phon). .. GENERATED FROM PYTHON SOURCE LINES 172-185 .. 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 186-188 For comparison, display the values previously obtained with the ISO 532-1 standard. The two standards rely on similar auditory principles but differ in the way they estimate the loudness. .. GENERATED FROM PYTHON SOURCE LINES 188-194 .. 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 195-197 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 197-200 .. 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 201-203 Calculate sharpness, roughness, and fluctuation strength ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 205-206 Calculate the sharpness. .. GENERATED FROM PYTHON SOURCE LINES 206-210 .. code-block:: Python sharpness = Sharpness(signal=signal_flute, field_type="Free") sharpness.process() sharpness_value = sharpness.get_sharpness() .. GENERATED FROM PYTHON SOURCE LINES 211-212 Calculate the sharpness over time and plot it. .. GENERATED FROM PYTHON SOURCE LINES 212-216 .. 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 217-218 Calculate the sharpness according to standard DIN 45692. .. GENERATED FROM PYTHON SOURCE LINES 218-222 .. 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 223-224 Calculate the sharpness according to standard DIN 45692, over time, and plot it. .. GENERATED FROM PYTHON SOURCE LINES 224-228 .. 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 229-230 Calculate the roughness, and plot the specific roughness and roughness over time. .. GENERATED FROM PYTHON SOURCE LINES 230-240 .. 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 241-242 Calculate the fluctuation strength. .. GENERATED FROM PYTHON SOURCE LINES 242-246 .. 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 247-248 Print the calculated indicators. .. GENERATED FROM PYTHON SOURCE LINES 248-255 .. 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.346 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 `_