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.

Set up analysis#

Setting up the analysis consists of loading Ansys libraries, connecting to the DPF server, and retrieving the example files.

# 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)

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.

# 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()
flute

Compute and plot STFT#

# 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()
STFT, Amplitude, Phase

Modify the STFT parameters using the setters of the Stft class. Display the new STFT colormap.

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()
STFT, Amplitude, Phase

Compute and plot ISTFT#

# 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()

Plot the output, which is the original signal.

istft.plot()
003 compute stft

Total running time of the script: (1 minutes 20.899 seconds)

Gallery generated by Sphinx-Gallery