On this page
torch.stft
torch.stft(input, n_fft, hop_length=None, win_length=None, window=None, center=True, pad_mode='reflect', normalized=False, onesided=None, return_complex=None)[source]-
Short-time Fourier transform (STFT).
Warning
From version 1.8.0,
return_complexmust always be given explicitly for real inputs andreturn_complex=Falsehas been deprecated. Strongly preferreturn_complex=Trueas in a future pytorch release, this function will only return complex tensors.Note that
torch.view_as_real()can be used to recover a real tensor with an extra last dimension for real and imaginary components.The STFT computes the Fourier transform of short overlapping windows of the input. This giving frequency components of the signal as they change over time. The interface of this function is modeled after (but not a drop-in replacement for) librosa stft function.
Ignoring the optional batch dimension, this method computes the following expression:
where is the index of the sliding window, and is the frequency for
onesided=False, or foronesided=True.inputmust be either a 1-D time sequence or a 2-D batch of time sequences.- If
hop_lengthisNone(default), it is treated as equal tofloor(n_fft / 4). - If
win_lengthisNone(default), it is treated as equal ton_fft. windowcan be a 1-D tensor of sizewin_length, e.g., fromtorch.hann_window(). IfwindowisNone(default), it is treated as if having everywhere in the window. If ,windowwill be padded on both sides to lengthn_fftbefore being applied.- If
centerisTrue(default),inputwill be padded on both sides so that the -th frame is centered at time . Otherwise, the -th frame begins at time . pad_modedetermines the padding method used oninputwhencenterisTrue. Seetorch.nn.functional.pad()for all available options. Default is"reflect".- If
onesidedisTrue(default for real input), only values for in are returned because the real-to-complex Fourier transform satisfies the conjugate symmetry, i.e., . Note if the input or window tensors are complex, thenonesidedoutput is not possible. - If
normalizedisTrue(default isFalse), the function returns the normalized STFT results, i.e., multiplied by . - If
return_complexisTrue(default if input is complex), the return is ainput.dim() + 1dimensional complex tensor. IfFalse, the output is ainput.dim() + 2dimensional real tensor where the last dimension represents the real and imaginary components.
Returns either a complex tensor of size if
return_complexis true, or a real tensor of size . Where is the optional batch size ofinput, is the number of frequencies where STFT is applied and is the total number of frames used.Warning
This function changed signature at version 0.4.1. Calling with the previous signature may cause error or return incorrect result.
- Parameters
-
- input (Tensor) – the input tensor of shape
(B?, L)whereB?is an optional batch dimension - n_fft (int) – size of Fourier transform
- hop_length (int, optional) – the distance between neighboring sliding window frames. Default:
None(treated as equal tofloor(n_fft / 4)) - win_length (int, optional) – the size of window frame and STFT filter. Default:
None(treated as equal ton_fft) - window (Tensor, optional) – the optional window function. Shape must be 1d and
<= n_fftDefault:None(treated as window of all s) - center (bool, optional) – whether to pad
inputon both sides so that the -th frame is centered at time . Default:True - pad_mode (str, optional) – controls the padding method used when
centerisTrue. Default:"reflect" - normalized (bool, optional) – controls whether to return the normalized STFT results Default:
False - onesided (bool, optional) – controls whether to return half of results to avoid redundancy for real inputs. Default:
Truefor realinputandwindow,Falseotherwise. return_complex (bool, optional) –
whether to return a complex tensor, or a real tensor with an extra last dimension for the real and imaginary components.
Changed in version 2.0:
return_complexis now a required argument for real inputs, as the default is being transitioned toTrue.Deprecated since version 2.0:
return_complex=Falseis deprecated, instead usereturn_complex=TrueNote that callingtorch.view_as_real()on the output will recover the deprecated output format.
- input (Tensor) – the input tensor of shape
- Returns
-
A tensor containing the STFT result with shape (B?, N, T, C?) where-
B?is an optional batch dimnsion from the inputNis the number of frequency samples,(n_fft // 2) + 1foronesided=True, or otherwisen_fft.Tis the number of frames,1 + L // hop_lengthforcenter=True, or1 + (L - n_fft) // hop_lengthotherwise.C?is an optional length-2 dimension of real and imaginary components, present whenreturn_complex=False.
- Return type
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.stft.html