.. 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. - Sharpness according to Zwicker and Fastl, "Psychoacoustics: Facts and models", 1990. - Roughness 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 Diplomarbeit", 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 50-54 Set up analysis ~~~~~~~~~~~~~~~ Setting up the analysis consists of loading Ansys libraries, connecting to the DPF server, and retrieving the example files. .. GENERATED FROM PYTHON SOURCE LINES 54-81 .. code-block:: Python # Load Ansys libraries. import os import numpy as np from ansys.sound.core.examples_helpers import ( download_accel_with_rpm_wav, download_flute_2_wav, download_flute_wav, ) from ansys.sound.core.psychoacoustics import ( FluctuationStrength, LoudnessISO532_1_Stationary, LoudnessISO532_1_TimeVarying, Roughness, Sharpness, ) 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 server or start a local server. server = connect_to_or_start_server() .. GENERATED FROM PYTHON SOURCE LINES 82-88 Calculate ISO 532-1 loudness for a stationary sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load a signal from a WAV file using the ``LoadWav`` class. It is returned as a DPF field container. For more information, see `fields_container `_ in the DPF-Core API documentation. .. GENERATED FROM PYTHON SOURCE LINES 88-95 .. code-block:: Python # Load example data from WAV file path_flute_wav = download_flute_wav() wav_loader = LoadWav(path_flute_wav) wav_loader.process() fc_signal = wav_loader.get_output() .. GENERATED FROM PYTHON SOURCE LINES 96-97 Create a ``LoudnessISO532_1_Stationary`` object, set its signal, and compute the loudness. .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: Python loudness_stationary = LoudnessISO532_1_Stationary(signal=fc_signal) loudness_stationary.process() .. GENERATED FROM PYTHON SOURCE LINES 101-102 Get the value in sone or in phon. .. GENERATED FROM PYTHON SOURCE LINES 102-111 .. code-block:: Python loudness_sone = loudness_stationary.get_loudness_sone() loudness_level_phon = loudness_stationary.get_loudness_level_phon() file_name = os.path.basename(path_flute_wav) print( f"\nThe loudness of sound file {file_name} " f"is{loudness_sone: .1f} sones " f"or{loudness_level_phon: .1f} phons." ) .. rst-class:: sphx-glr-script-out .. code-block:: none The loudness of sound file flute.wav is 39.6 sones or 93.1 phons. .. GENERATED FROM PYTHON SOURCE LINES 112-113 Plot the specific loudness. .. GENERATED FROM PYTHON SOURCE LINES 113-115 .. code-block:: Python loudness_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 116-119 Calculate ISO 532-1 loudness for several signals at once ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load another WAV file and store it along with the first one. .. GENERATED FROM PYTHON SOURCE LINES 119-129 .. code-block:: Python path_flute2_wav = download_flute_2_wav() wav_loader = LoadWav(path_flute2_wav) wav_loader.process() # Store the second signal as a second field in the DPF fields container. fc_two_signals = fc_signal fc_two_signals.add_field({"channel_number": 1}, wav_loader.get_output()[0]) .. GENERATED FROM PYTHON SOURCE LINES 130-131 Calculate the loudness for both signals at once. .. GENERATED FROM PYTHON SOURCE LINES 131-134 .. code-block:: Python loudness_stationary = LoudnessISO532_1_Stationary(signal=fc_two_signals) loudness_stationary.process() .. GENERATED FROM PYTHON SOURCE LINES 135-136 Get the values in sone or in phon. .. GENERATED FROM PYTHON SOURCE LINES 136-145 .. code-block:: Python loudness_sone2 = loudness_stationary.get_loudness_sone(1) loudness_level_phon2 = loudness_stationary.get_loudness_level_phon(1) file_name2 = os.path.basename(path_flute2_wav) print( f"In comparison, the loudness of sound file {file_name2} " f"is{loudness_sone2: .1f} sones " f"or{loudness_level_phon2: .1f} phons." ) .. rst-class:: sphx-glr-script-out .. code-block:: none In comparison, the loudness of sound file flute2.wav is 16.2 sones or 80.2 phons. .. GENERATED FROM PYTHON SOURCE LINES 146-148 Plot specific loudness for both signals in a single figure. Note how the first sound has a higher specific loudness than the second. .. GENERATED FROM PYTHON SOURCE LINES 148-150 .. code-block:: Python loudness_stationary.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_002.png :alt: Specific loudness :srcset: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 151-154 Calculate ISO 532-1 loudness for a non-stationary sound ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load a new signal (non-stationary) from a WAV file. .. GENERATED FROM PYTHON SOURCE LINES 154-159 .. code-block:: Python path_accel_wav = download_accel_with_rpm_wav() wav_loader = LoadWav(path_accel_wav) wav_loader.process() f_signal = wav_loader.get_output()[0] # Field 0 only, because the RPM profile is useless here. .. GENERATED FROM PYTHON SOURCE LINES 160-161 Create a ``LoudnessISO532_1_TimeVarying`` object, set its signal, and compute the loudness. .. GENERATED FROM PYTHON SOURCE LINES 161-164 .. code-block:: Python loudness_time_varying = LoudnessISO532_1_TimeVarying(signal=f_signal) loudness_time_varying.process() .. GENERATED FROM PYTHON SOURCE LINES 165-166 Get percentile loudness values. .. GENERATED FROM PYTHON SOURCE LINES 166-180 .. code-block:: Python N5 = loudness_time_varying.get_N5_sone() N10 = loudness_time_varying.get_N10_sone() L5 = loudness_time_varying.get_L5_phon() L10 = loudness_time_varying.get_L10_phon() file_name3 = os.path.basename(path_accel_wav) print( f"\nThe sound file {file_name3} has the following percentile loudness values: \n" f"- N5 = {np.round(N5, 1)} sones.\n" f"- N10 = {np.round(N10, 1)} sones.\n" f"- L5 = {np.round(L5, 1)} phons.\n" f"- L10 = {np.round(L10, 1)} phons." ) .. rst-class:: sphx-glr-script-out .. code-block:: none The sound file accel_with_rpm.wav has the following percentile loudness values: - N5 = 16.4 sones. - N10 = 15.6 sones. - L5 = 80.4 phons. - L10 = 79.6 phons. .. GENERATED FROM PYTHON SOURCE LINES 181-182 Plot loudness as a function of time. .. GENERATED FROM PYTHON SOURCE LINES 182-184 .. code-block:: Python loudness_time_varying.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_003.png :alt: Time-varying loudness, Time-varying loudness level :srcset: /examples/gallery_examples/images/sphx_glr_007_calculate_psychoacoustic_indicators_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 185-188 Calculate sharpness, roughness, and fluctuation strength ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Calculate sharpness, roughness, and fluctuation strength for the two sounds. .. GENERATED FROM PYTHON SOURCE LINES 190-191 Calculate the sharpness. .. GENERATED FROM PYTHON SOURCE LINES 191-195 .. code-block:: Python sharpness = Sharpness(signal=fc_two_signals) sharpness.process() sharpness_values = (sharpness.get_sharpness(0), sharpness.get_sharpness(1)) .. GENERATED FROM PYTHON SOURCE LINES 196-197 Calculate the roughness. .. GENERATED FROM PYTHON SOURCE LINES 197-201 .. code-block:: Python roughness = Roughness(signal=fc_two_signals) roughness.process() roughness_values = (roughness.get_roughness(0), roughness.get_roughness(1)) .. GENERATED FROM PYTHON SOURCE LINES 202-203 Calculate the fluctuation strength. .. GENERATED FROM PYTHON SOURCE LINES 203-210 .. code-block:: Python fluctuation_strength = FluctuationStrength(signal=fc_two_signals) fluctuation_strength.process() fluctuation_strength_values = ( fluctuation_strength.get_fluctuation_strength(0), fluctuation_strength.get_fluctuation_strength(1), ) .. GENERATED FROM PYTHON SOURCE LINES 211-212 Print the results. .. GENERATED FROM PYTHON SOURCE LINES 212-222 .. code-block:: Python print( f"\nThe sharpness of sound file {file_name} " f"is{sharpness_values[0]: .2f} acum, " f"its roughness is{roughness_values[0]: .2f} asper, " f"and its fluctuation strength is{fluctuation_strength_values[0]: .2f} vacil.\n" f"For sound file {file_name2}, these indicators' values are, respectively, " f"{sharpness_values[1]: .2f} acum, " f"{roughness_values[1]: .2f} asper, " f"and{fluctuation_strength_values[1]: .2f} vacil.\n" ) .. rst-class:: sphx-glr-script-out .. code-block:: none The sharpness of sound file flute.wav is 1.19 acum, its roughness is 0.06 asper, and its fluctuation strength is 0.60 vacil. For sound file flute2.wav, these indicators' values are, respectively, 1.15 acum, 0.03 asper, and 0.45 vacil. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 52.138 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>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_