.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\gallery_examples\003_compute_stft.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_003_compute_stft.py: .. _compute_stft_example: Compute the STFT and ISTFT -------------------------- This example shows how to compute the short-time Fourier transform (STFT) of a signal. It also shows how to compute the inverse short-time Fourier transform (ISTFT) from a STFT matrix and get a signal. .. GENERATED FROM PYTHON SOURCE LINES 35-39 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 39-49 .. code-block:: Python # Load Ansys libraries. from ansys.sound.core.examples_helpers import download_flute_wav from ansys.sound.core.server_helpers import connect_to_or_start_server from ansys.sound.core.signal_utilities import LoadWav from ansys.sound.core.spectrogram_processing import Istft, Stft # Connect to a remote server or start a local server my_server = connect_to_or_start_server(use_license_context=True) .. GENERATED FROM PYTHON SOURCE LINES 50-56 Load a signal ~~~~~~~~~~~~~ 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 56-68 .. code-block:: Python # Return the input data of the example file path_flute_wav = download_flute_wav() # Load the WAV file wav_loader = LoadWav(path_flute_wav) wav_loader.process() fc_signal = wav_loader.get_output() # Plot the input signal wav_loader.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_003_compute_stft_001.png :alt: flute :srcset: /examples/gallery_examples/images/sphx_glr_003_compute_stft_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-71 Compute and plot STFT ~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 71-83 .. code-block:: Python # Instantiate an instance of the ``Stft`` class using the previously loaded signal # as an input. Use an FFT size of 1024 points and then display the STFT colormap. stft = Stft(fc_signal, fft_size=1024) # Process the STFT stft.process() # Plot the output stft.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_003_compute_stft_002.png :alt: STFT, Amplitude, Phase :srcset: /examples/gallery_examples/images/sphx_glr_003_compute_stft_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 84-86 Modify the STFT parameters using the setters of the ``Stft`` class. Display the new STFT colormap. .. GENERATED FROM PYTHON SOURCE LINES 86-97 .. code-block:: Python stft.fft_size = 4096 stft.window_overlap = 0.95 stft.window_type = "BARTLETT" # Reprocess the STFT with the new parameters stft.process() # Plot the modified output stft.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_003_compute_stft_003.png :alt: STFT, Amplitude, Phase :srcset: /examples/gallery_examples/images/sphx_glr_003_compute_stft_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 98-100 Compute and plot ISTFT ~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 100-112 .. code-block:: Python # Obtain a time-domain signal using the ``Istft`` class. # The input of the ``Istft`` class is the output STFT object previously computed. fc_stft = stft.get_output() # Instantiate the class istft = Istft(fc_stft) # Process the ISTFT istft.process() .. GENERATED FROM PYTHON SOURCE LINES 113-114 Plot the output, which is the original signal. .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: Python istft.plot() .. image-sg:: /examples/gallery_examples/images/sphx_glr_003_compute_stft_004.png :alt: 003 compute stft :srcset: /examples/gallery_examples/images/sphx_glr_003_compute_stft_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 20.899 seconds) .. _sphx_glr_download_examples_gallery_examples_003_compute_stft.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 003_compute_stft.ipynb <003_compute_stft.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 003_compute_stft.py <003_compute_stft.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_