.. 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: Spectrogram: compute the STFT and ISTFT --------------------------------------- This example shows how to compute the short-time Fourier transform (STFT) of a signal, that is, its `spectrogram`. It also shows how to use the inverse short-time Fourier transform (ISTFT) to get the signal corresponding to a given spectrogram. .. GENERATED FROM PYTHON SOURCE LINES 36-40 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 40-51 .. 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 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 55-61 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 61-73 .. code-block:: Python # Return the input data of the example file path_flute_wav = download_flute_wav(server=my_server) # 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 74-76 Compute and plot STFT ~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 76-88 .. 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 89-91 Modify the STFT parameters using the setters of the ``Stft`` class. Display the new STFT colormap. .. GENERATED FROM PYTHON SOURCE LINES 91-102 .. code-block:: Python stft.fft_size = 4096 stft.window_overlap = 0.95 stft.window_type = "TRIANGULAR" # 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 103-105 Compute and plot ISTFT ~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 105-117 .. 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 118-119 Plot the output, which is the original signal. .. GENERATED FROM PYTHON SOURCE LINES 119-121 .. 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:** (0 minutes 38.114 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>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 003_compute_stft.zip <003_compute_stft.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_