
    ͑i^                       S SK Jr  S SKJrJr  S SKrS SKJr  S SKJrJ	r	J
r
  SSKJr  SSKJr  SS	KJrJrJr  SS
KJr  \(       a  S SKJr  \S   rSS/r  S           SS jjr S         SS jjr        S                     SS jjr         S                       SS jjrg)    )annotations)TYPE_CHECKINGLiteralN)_C_ops)in_dynamic_modein_dynamic_or_pir_modein_pir_mode   )check_variable_and_dtype)LayerHelper)fft_c2cfft_c2rfft_r2c)
is_complex)Tensorr   stftistftc                   US;  a  [        SU S35      e[        U[        5      (       a  US::  a  [        SU S35      e[        U[        5      (       a  US::  a  [        SU S35      e[        5       (       aH  XR                  U   :  a  [        SU S	U R                  U    S
35      e[
        R                  " XX#5      $ [        5       (       a  [
        R                  " XX#5      $ Sn[        U S/ SQU5        [        U40 [        5       D6nUR                  SS9nUR                  US9nUR                  USU 0UUUS.SU0S9  U$ )a
  
Slice the N-dimensional (where N >= 1) input into (overlapping) frames.

Args:
    x (Tensor): The input data which is a N-dimensional (where N >= 1) Tensor
        with shape `[..., seq_length]` or `[seq_length, ...]`.
    frame_length (int): Length of the frame and `0 < frame_length <= x.shape[axis]`.
    hop_length (int): Number of steps to advance between adjacent frames
        and `0 < hop_length`.
    axis (int, optional): Specify the axis to operate on the input Tensors. Its
        value should be 0(the first dimension) or -1(the last dimension). If not
        specified, the last axis is used by default.
    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 :ref:`api_guide_Name`.

Returns:
    The output frames tensor with shape `[..., frame_length, num_frames]` if `axis==-1`,
        otherwise `[num_frames, frame_length, ...]` where

        `num_frames = 1 + (x.shape[axis] - frame_length) // hop_length`

Examples:

    .. code-block:: pycon

        >>> import paddle
        >>> from paddle import signal

        >>> # 1D
        >>> x = paddle.arange(8)
        >>> y0 = signal.frame(x, frame_length=4, hop_length=2, axis=-1)
        >>> print(y0)
        Tensor(shape=[4, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[0, 2, 4],
         [1, 3, 5],
         [2, 4, 6],
         [3, 5, 7]])

        >>> y1 = signal.frame(x, frame_length=4, hop_length=2, axis=0)
        >>> print(y1)
        Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[0, 1, 2, 3],
         [2, 3, 4, 5],
         [4, 5, 6, 7]])

        >>> # 2D
        >>> x0 = paddle.arange(16).reshape([2, 8])
        >>> y0 = signal.frame(x0, frame_length=4, hop_length=2, axis=-1)
        >>> print(y0)
        Tensor(shape=[2, 4, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[[0 , 2 , 4 ],
          [1 , 3 , 5 ],
          [2 , 4 , 6 ],
          [3 , 5 , 7 ]],
         [[8 , 10, 12],
          [9 , 11, 13],
          [10, 12, 14],
          [11, 13, 15]]])

        >>> x1 = paddle.arange(16).reshape([8, 2])
        >>> y1 = signal.frame(x1, frame_length=4, hop_length=2, axis=0)
        >>> print(y1.shape)
        paddle.Size([3, 4, 2])

        >>> # > 2D
        >>> x0 = paddle.arange(32).reshape([2, 2, 8])
        >>> y0 = signal.frame(x0, frame_length=4, hop_length=2, axis=-1)
        >>> print(y0.shape)
        paddle.Size([2, 2, 4, 3])

        >>> x1 = paddle.arange(32).reshape([8, 2, 2])
        >>> y1 = signal.frame(x1, frame_length=4, hop_length=2, axis=0)
        >>> print(y1.shape)
        paddle.Size([3, 4, 2, 2])
r   Unexpected axis: . It should be 0 or -1.r   zUnexpected frame_length: #. It should be an positive integer.Unexpected hop_length: zKAttribute frame_length should be less equal than sequence length, but got (z) > (z).framex)int32int64float16float32float64input_param_namedtypeX)frame_length
hop_lengthaxisOuttypeinputsattrsoutputs)
ValueError
isinstanceintr   shaper   r   r	   r   r   localsinput_dtype"create_variable_for_type_inference	append_op)	r   r'   r(   r)   nameop_typehelperr%   outs	            M/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/signal.pyr   r   *   s|   d 7,TF2IJKKlC((LA,='~5XY
 	
 j#&&*/%j\1TU
 	
 ''$-'(>qwwt}oRA  ||AZ>>	||AZ>> sG	
 W11""C"877e7D8 ,(
 CL 	 		
 J    c                   US;  a  [        SU S35      e[        U[        5      (       a  US::  a  [        SU S35      eSn[        5       (       a  [        R
                  " XU5      nU$ [        U S/ S	QU5        [        U40 [        5       D6nUR                  SS
9nUR                  US9nUR                  USU 0XS.SU0S9  U$ )a	  
Reconstructs a tensor consisted of overlap added sequences from input frames.

Args:
    x (Tensor): The input data which is a N-dimensional (where N >= 2) Tensor
        with shape `[..., frame_length, num_frames]` or
        `[num_frames, frame_length ...]`.
    hop_length (int): Number of steps to advance between adjacent frames and
        `0 < hop_length <= frame_length`.
    axis (int, optional): Specify the axis to operate on the input Tensors. Its
        value should be 0(the first dimension) or -1(the last dimension). If not
        specified, the last axis is used by default.
    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 :ref:`api_guide_Name`.

Returns:
    The output frames tensor with shape `[..., seq_length]` if `axis==-1`,
        otherwise `[seq_length, ...]` where

        `seq_length = (n_frames - 1) * hop_length + frame_length`

Examples:

    .. code-block:: pycon

        >>> import paddle
        >>> from paddle.signal import overlap_add

        >>> # 2D
        >>> x0 = paddle.arange(16).reshape([8, 2])
        >>> print(x0)
        Tensor(shape=[8, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[0 , 1 ],
         [2 , 3 ],
         [4 , 5 ],
         [6 , 7 ],
         [8 , 9 ],
         [10, 11],
         [12, 13],
         [14, 15]])


        >>> y0 = overlap_add(x0, hop_length=2, axis=-1)
        >>> print(y0)
        Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True,
        [0 , 2 , 5 , 9 , 13, 17, 21, 25, 13, 15])

        >>> x1 = paddle.arange(16).reshape([2, 8])
        >>> print(x1)
        Tensor(shape=[2, 8], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ],
         [8 , 9 , 10, 11, 12, 13, 14, 15]])


        >>> y1 = overlap_add(x1, hop_length=2, axis=0)
        >>> print(y1)
        Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True,
        [0 , 1 , 10, 12, 14, 16, 18, 20, 14, 15])


        >>> # > 2D
        >>> x0 = paddle.arange(32).reshape([2, 1, 8, 2])
        >>> y0 = overlap_add(x0, hop_length=2, axis=-1)
        >>> print(y0.shape)
        paddle.Size([2, 1, 10])

        >>> x1 = paddle.arange(32).reshape([2, 8, 1, 2])
        >>> y1 = overlap_add(x1, hop_length=2, axis=0)
        >>> print(y1.shape)
        paddle.Size([10, 1, 2])
r   r   r   r   r   r   overlap_addr   )r   r   r   r    r!   uint16r"   r$   r&   )r(   r)   r*   r+   )r0   r1   r2   r   r   r?   r   r   r4   r5   r6   r7   )r   r(   r)   r8   r9   r;   r:   r%   s           r<   r?   r?      s    T 7,TF2IJKKj#&&*/%j\1TU
 	
 G  5" J 	!I		
 W11""C"877e7D8!+:CL	 	 	
 Jr=   c
           
     (   [        U R                  5      n
U
S;   d
   SU
 35       eU
S:X  a  U R                  S5      n Uc  [        US-  5      nUS:  d   SU S35       eUc  Un[	        5       (       a7  SUs=:  a  U R                  S	   ::  d  O   S
U R                  S	    SU S35       eSUs=:  a  U::  d  O   SU SU S35       eUbA  [        UR                  5      S:X  a  [        U5      U:X  d   SU SUR                   S35       eO [
        R                  " U4U R                  S9nX1:  a8  X-
  S-  nX-
  U-
  n[
        R                  R                  R                  XKU/SS9nU(       a_  US;   d   SU S35       eUS-  n[
        R                  R                  R                  U R                  S	5      X/USS9R                  S	5      n [        XUS	S9nUR                  / SQS9n[
        R                  " X5      nU(       a  SOSnUc  [        U5      (       + n[        U5      (       a  U(       a   S5       e[        U 5      (       d  [!        USS	USUU	S9nO[#        USS	USU	S 9nUR                  / SQS9nU
S:X  a  UR%                  S5        U$ )!a  

Short-time Fourier transform (STFT).

The STFT computes the discrete Fourier transforms (DFT) of short overlapping
windows of the input using this formula:

.. math::
    X_t[f] = \sum_{n = 0}^{N-1} \text{window}[n]\ x[t \times H + n]\ e^{-{2 \pi j f n}/{N}}

Where:
- :math:`t`: The :math:`t`-th input window.
- :math:`f`: Frequency :math:`0 \leq f < \text{n_fft}` for `onesided=False`,
or :math:`0 \leq f < \lfloor \text{n_fft} / 2 \rfloor + 1` for `onesided=True`.
- :math:`N`: Value of `n_fft`.
- :math:`H`: Value of `hop_length`.

Args:
    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
        :math:`t \times hop\_length` at the center of :math:`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 :ref:`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:
    .. code-block:: pycon

        >>> 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)
        paddle.Size([8, 257, 376])

        >>> y2 = stft(x, n_fft=512, onesided=False)
        >>> print(y2.shape)
        paddle.Size([8, 512, 376])

        >>> # complex input
        >>> x = paddle.randn([8, 48000], dtype=paddle.float64) + \
        ...         paddle.randn([8, 48000], dtype=paddle.float64)*1j
        >>> print(x.shape)
        paddle.Size([8, 48000])
        >>> print(x.dtype)
        paddle.complex128

        >>> y1 = stft(x, n_fft=512, center=False, onesided=False)
        >>> print(y1.shape)
        paddle.Size([8, 512, 372])

)r
      z9x should be a 1D or 2D real tensor, but got rank of x is r
   r   N   z"hop_length should be > 0, but got .r   z"n_fft should be in (0, seq_length()], but got "win_length should be in (0, n_fft(8expected a 1D window tensor of size equal to win_length(), but got window with shape r3   r%   rB   constantpadmode)rJ   reflectz5pad_mode should be "reflect" or "constant", but got "z".NLC)rL   rM   data_format)r   r'   r(   r)   r   rB   r
   permorthobackwardzBonesided should be False when input or window is a complex Tensor.T)r   nr)   normforwardonesidedr8   r   rV   r)   rW   rX   r8   )lenr3   	unsqueezer2   r   paddleonesr%   nn
functionalrL   squeezer   	transposemultiplyr   r   r   squeeze_)r   n_fftr(   
win_lengthwindowcenterpad_mode
normalizedrY   r8   x_rankpad_left	pad_right
pad_lengthx_framesrW   r;   s                    r<   r   r     s   n \F   L 
C6(KL 
 {KKN!_
>M?
|1MM>
5'AGGBK' 	
0\%PQR	
' z"U" 
,UG<
|1M" 6<< A%#f+*C 	
FzlRopvp|p|o}}~	
C*C J=@&1,&1	%%))9-J * 
  
 
 	P C8*BO	P 

 aZ
II  $$KKO(	 % 

 '"+ 	
 q"MH!! " H x0H 7jD!(++( 	
P	
| a==
 $RdDt
 --Y-
'C{QJr=   c           	     v   [        U SSS/S5        [        U R                  5      nUS;   d
   SU 35       eUS:X  a  U R                  S5      n Uc  [	        US
-  5      nUc  UnSUs=:  a  U::  d  O   SU SU S35       eSUs=:  a  U::  d  O   SU SU S35       eU R                  S   nU R                  S   n[        5       (       aQ  U R                  S:w  d   S5       eU(       a   XS-  S-   :X  d   SUS-  S-    SU S35       eOX:X  d   SU SU S35       eUbA  [        UR                  5      S:X  a  [        U5      U:X  d   SU SUR                   S35       eOdU R                  [        R                  [        R                  4;   a  [        R                  O[        R                  n[        R                  " U4US9nX1:  a8  X-
  S-  nX-
  U-
  n[        R                  R                  R                  XOU/SS9nU R!                  / SQS9n U(       a  SOSnU	(       a  U(       a   S 5       e[#        U S	SUS!S	S"9nO>[%        U5      (       a   S#5       eUS!L a  U S	S	2S	S	2S	US-  S-   24   n ['        U S	SUS!S	S"9n[        R(                  " UU5      R!                  / SQS9n[+        UUSS$9n[+        [        R,                  " [        R(                  " XD5      R                  S5      US/S%9R!                  SS/S9USS$9nUc&  U(       a  US	S	2US-  US-  * 24   nUUS-  US-  *  nO%U(       a  US-  nOSnUS	S	2UUU-   24   nUUUU-    n[        5       (       a;  UR/                  5       R1                  5       R3                  5       S&:  a  [5        S'5      eUU-  nUS:X  a  [        R6                  " USS(9nU$ ))a(  
Inverse short-time Fourier transform (ISTFT).

Reconstruct time-domain signal from the giving complex input and window tensor when
nonzero overlap-add (NOLA) condition is met:

.. math::
    \sum_{t = -\infty}^{\infty} \text{window}^2[n - t \times H]\ \neq \ 0, \ \text{for } all \ n

Where:
- :math:`t`: The :math:`t`-th input window.
- :math:`N`: Value of `n_fft`.
- :math:`H`: Value of `hop_length`.

    Result of `istft` expected to be the inverse of `paddle.signal.stft`, but it is
    not guaranteed to reconstruct a exactly realizable time-domain signal from a STFT
    complex tensor which has been modified (via masking or otherwise). Therefore, `istft`
    gives the `[Griffin-Lim optimal estimate] <https://ieeexplore.ieee.org/document/1164317>`_
    (optimal in a least-squares sense) for the corresponding signal.

Args:
    x (Tensor): The input data which is a 2-dimensional or 3-dimensional **complex**
        Tensor with shape `[..., n_fft, num_frames]`.
    n_fft (int): The size of Fourier transform.
    hop_length (int|None, optional): Number of steps to advance between adjacent windows
        from time-domain signal and `0 < hop_length < win_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`. It should be a
        real-valued tensor if `return_complex` is False. Default: `None`(treated as
        a rectangle window with value equal to 1 of size `win_length`).
    center (bool, optional): It means that whether the time-domain signal has been
        center padded. Default: `True`.
    normalized (bool, optional): Control whether to scale the output by :math:`1/sqrt(n_{fft})`.
        Default: `False`
    onesided (bool, optional): It means that whether the input STFT tensor is a half
        of the conjugate symmetry STFT tensor transformed from a real-valued signal
        and `istft` will return a real-valued tensor when it is set to `True`.
        Default: `True`.
    length (int|None, optional): Specify the length of time-domain signal. Default: `None`(
        treated as the whole length of signal).
    return_complex (bool, optional): It means that whether the time-domain signal is
        real-valued. If `return_complex` is set to `True`, `onesided` should be set to
        `False` cause the output is complex.
    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 :ref:`api_guide_Name`.

Returns:
    A tensor of least squares estimation of the reconstructed signal(s) with shape
    `[..., seq_length]`

Examples:
    .. code-block:: pycon

        >>> import numpy as np
        >>> import paddle
        >>> from paddle.signal import stft, istft

        >>> paddle.seed(0)

        >>> # STFT
        >>> x = paddle.randn([8, 48000], dtype=paddle.float64)
        >>> y = stft(x, n_fft=512)
        >>> print(y.shape)
        paddle.Size([8, 257, 376])

        >>> # ISTFT
        >>> x_ = istft(y, n_fft=512)
        >>> print(x_.shape)
        paddle.Size([8, 48000])

        >>> np.allclose(x, x_)
        True
r   	complex64
complex128r   )rB      z<x should be a 2D or 3D complex tensor, but got rank of x is rB   r   NrC   z'hop_length should be in (0, win_length(rE   rD   rF   r   z x should not be an empty tensor.r
   z+fft_size should be equal to n_fft // 2 + 1(z!) when onesided is True, but got z"fft_size should be equal to n_fft(z") when onesided is False, but got rG   rH   rI   rJ   rK   rQ   rR   rT   rU   zSonesided should be False when input(output of istft) or window is a complex Tensor.FrZ   zGData type of window should not be complex when return_complex is False.)r   r(   r)   )r   repeat_timesgdy=zAbort istft because Nonzero Overlap Add (NOLA) condition failed. For more information about NOLA constraint please see `scipy.signal.check_NOLA`(https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.check_NOLA.html).)r)   )r   r[   r3   r\   r2   r   sizer%   r]   r    rq   r!   r^   r_   r`   rL   rb   r   r   r   rc   r?   tileabsminitemr0   ra   )r   re   r(   rf   rg   rh   rj   rY   lengthreturn_complexr8   rk   n_framesfft_sizewindow_dtyperl   rm   rW   r;   window_envelopstarts                        r<   r   r     sm   r Qk<%@'J\F   O 
FfXNO 
 {KKN!_

 z'Z' 
1*\*UVW' z"U" 
,UG<
|1M" wwr{Hwwr{Hvv{>>>{zA~- =eqj1n=MMnownxxyz- $ 4UG;]^f]gghi$ 6<< A%#f+*C 	
FzlRopvp|p|o}}~	
C*C ww6>>6+;+;<< NN 	
 J=E&1,&1	%%))9-J * 
 	
 	 	A !7jD 	
a	
| Tu4Pf%% 	
U	
% u!Q(%1*q.(()ATu4P
//#v
&
0
0 1 C 
*2C !
++oof-77:"A
 )!Q)
 N ~a%1*%1*556C+UaZUaZ=INQJEE!UUV^++,'? ^//1557<<>F s
 	
 
C{nnSq)Jr=   )r   N)r   r   r'   r2   r(   r2   r)   _SignalAxesr8   
str | Nonereturnr   )
r   r   r(   r2   r)   r   r8   r   r   r   )NNNTrN   FNN)r   r   re   r2   r(   
int | Nonerf   r   rg   Tensor | Nonerh   boolri   zLiteral['reflect', 'constant']rj   r   rY   zbool | Noner8   r   r   r   )	NNNTFTNFN)r   r   re   r2   r(   r   rf   r   rg   r   rh   r   rj   r   rY   r   r{   r   r|   r   r8   r   r   r   )
__future__r   typingr   r   r]   r   paddle.frameworkr   r   r	   base.data_feederr   base.layer_helperr   fftr   r   r   tensor.attributer   r   r   __all__r   r?   r   r    r=   r<   <module>r      s   # )    7 * * * (%.K  zzz z 	z
 z z| LPfff&1f>HffX "! /8 rrr r 	r
 r r -r r r r rp "!  QQQ Q 	Q
 Q Q Q Q Q Q Q Qr=   