stft

paddle.signal. stft ( x: Tensor, n_fft: int, hop_length: int | None = None, win_length: int | None = None, window: Tensor | None = None, center: bool = True, pad_mode: Literal['reflect', 'constant'] = 'reflect', normalized: bool = False, onesided: bool | None = None, name: str | None = None ) Tensor [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|None, 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|None, optional) – The size of window. Default: None (treated as equal to n_fft)

  • window (Tensor|None, 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: None

  • name (str|None, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to api_guide_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]