stft¶
- paddle.signal. stft ( x, n_fft, hop_length=None, win_length=None, window=None, center=True, pad_mode='reflect', normalized=False, onesided=True, name=None ) [source]
-
Short-time Fourier transform (STFT).
The STFT computes the discrete Fourier transforms (DFT) of short overlapping windows of the input using this formula:
\[X_t[f] = \sum_{n = 0}^{N-1} \text{window}[n]\ x[t \times H + n]\ e^{-{2 \pi j f n}/{N}}\]Where: - \(t\): The \(t\)-th input window. - \(f\): Frequency \(0 \leq f < \text{n_fft}\) for onesided=False, or \(0 \leq f < \lfloor \text{n_fft} / 2 \rfloor + 1\) for onesided=True. - \(N\): Value of n_fft. - \(H\): Value of hop_length.
- Parameters
-
x (Tensor) – The input data which is a 1-dimensional or 2-dimensional Tensor with shape […, seq_length]. It can be a real-valued or a complex Tensor.
n_fft (int) – The number of input samples to perform Fourier transform.
hop_length (int, optional) – Number of steps to advance between adjacent windows and 0 < hop_length. Default: None (treated as equal to n_fft//4)
win_length (int, optional) – The size of window. Default: None (treated as equal to n_fft)
window (Tensor, optional) – A 1-dimensional tensor of size win_length. It will be center padded to length n_fft if win_length < n_fft. Default: None ( treated as a rectangle window with value equal to 1 of size win_length).
center (bool, optional) – Whether to pad x to make that the \(t \times hop\_length\) at the center of \(t\)-th frame. Default: True.
pad_mode (str, optional) – Choose padding pattern when center is True. See paddle.nn.functional.pad for all padding options. Default: “reflect”
normalized (bool, optional) – Control whether to scale the output by 1/sqrt(n_fft). Default: False
onesided (bool, optional) – Control whether to return half of the Fourier transform output that satisfies the conjugate symmetry condition when input is a real-valued tensor. It can not be True if input is a complex tensor. Default: True
name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.
- Returns
-
The complex STFT output tensor with shape […, n_fft//2 + 1, num_frames] (real-valued input and onesided is True) or […, n_fft, num_frames] (onesided is False)
Examples
>>> import paddle >>> from paddle.signal import stft >>> # real-valued input >>> x = paddle.randn([8, 48000], dtype=paddle.float64) >>> y1 = stft(x, n_fft=512) >>> print(y1.shape) [8, 257, 376] >>> y2 = stft(x, n_fft=512, onesided=False) >>> print(y2.shape) [8, 512, 376] >>> # complex input >>> x = paddle.randn([8, 48000], dtype=paddle.float64) + \ ... paddle.randn([8, 48000], dtype=paddle.float64)*1j >>> print(x.shape) [8, 48000] >>> print(x.dtype) paddle.complex128 >>> y1 = stft(x, n_fft=512, center=False, onesided=False) >>> print(y1.shape) [8, 512, 372]