torch.stft¶
-
torch.stft(input: torch.Tensor, n_fft: int, hop_length: Optional[int] = None, win_length: Optional[int] = None, window: Optional[torch.Tensor] = None, center: bool = True, pad_mode: str = 'reflect', normalized: bool = False, onesided: bool = True) → torch.Tensor[source]¶ Short-time Fourier transform (STFT).
Ignoring the optional batch dimension, this method computes the following expression:
where is the index of the sliding window, and is the frequency that . When
onesidedis the default valueTrue,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), only values for in are returned because the real-to-complex Fourier transform satisfies the conjugate symmetry, i.e., .If
normalizedisTrue(default isFalse), the function returns the normalized STFT results, i.e., multiplied by .
Returns the real and the imaginary parts together as one tensor of size , where is the optional batch size of
input, is the number of frequencies where STFT is applied, is the total number of frames used, and each pair in the last dimension represents a complex number as the real part and the imaginary part.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
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. Default:
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:Truepad_mode (string, optional) – controls the padding method used when
centerisTrue. Default:"reflect"normalized (bool, optional) – controls whether to return the normalized STFT results Default:
Falseonesided (bool, optional) – controls whether to return half of results to avoid redundancy Default:
True
- Returns
A tensor containing the STFT result with shape described above
- Return type