
    Phz                     `   d Z ddlZddlmZ ddlmZmZmZ ddlZ	ddl
Z
ddlmZmZ ddlmZ ddlmZ ddlmZmZmZmZmZ dd	lmZmZmZmZ dd
lmZ d Z d Z!d Z"d Z#e
jH                  jK                  dd      defd       Z&d Z'e
jH                  jK                  dg d      de(fd       Z)d Z*e
jH                  jK                  dg d      ded   fd       Z+d Z,d Z-d Z.d  Z/d! Z0d" Z1e
jH                  jK                  d#d$g d%fd&g d%fd'g d(fd)g d*fg      d+efd,       Z2e
jH                  jK                  d-d.d/g      e
jH                  jK                  dd0d1g      e
jH                  jK                  d+d$d)g      d+efd2                     Z3d3 Z4e
jH                  jK                  d-d4d5g      d-e(fd6       Z5e
jH                  jK                  d7 e6d8            d9        Z7e
jH                  jK                  d:g d;      d:e(fd<       Z8e
jH                  jK                  d=d1d>g      d?        Z9d@ Z:dA Z;dB Z<dC Z=e
jH                  jK                  d+dD      d+efdE       Z>e
jH                  jK                  dFdG      dFe(dz  fdH       Z?e
jH                  jK                  dIg dJ      d-e(dKe(dLe(fdM       Z@e
jH                  jK                  dNdO      dP        ZAdQ ZBe
jH                  jK                  dRg dS      e
jH                  jK                  dT ee            dUe(dKe(dLe(dVe(fdW              ZCe
jH                  jK                  dXdY      e
jH                  jK                  dZd[      e
jH                  jK                  d\d]      e
jH                  jK                  d^d_      dXe(dZe(d\e(d^eDfd`                            ZEy)aaA  Unit tests for module `_short_time_fft`.

This file's structure loosely groups the tests into the following sequential
categories:

1. Test function `_calc_dual_canonical_window`.
2. Test for invalid parameters and exceptions in `ShortTimeFFT` (until the
    `test_from_window` function).
3. Test algorithmic properties of STFT/ISTFT. Some tests were ported from
   ``test_spectral.py``.

Notes
-----
* Mypy 0.990 does interpret the line::

        from scipy.stats import norm as normal_distribution

  incorrectly (but the code works), hence a ``type: ignore`` was appended.
    N)product)castget_argsLiteral)xp_assert_closexp_assert_equal)fftshift)norm)
get_windowwelchstftistftspectrogram)FFT_MODE_TYPE_calc_dual_canonical_windowShortTimeFFTPAD_TYPE)gaussianc                  h    t        ddd      } t        | d      }t        |d      }t        ||        y)zTest dual window calculation with a round trip to verify duality.

    Note that this works only for canonical window pairs (having minimal
    energy) like a Gaussian.

    The window is the same as in the example of `from ShortTimeFFT.from_dual`.
    3   
   T)stdsymN)r   r   r   )wind_winwin2s      qC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\scipy/signal/tests/test_short_time_fft.py*test__calc_dual_canonical_window_roundtripr   $   s4     224
(C'R0E&ub1DD#    c                     t        j                  t        d      5  t        t	        j
                  d      d       ddd       t        j                  t        d      5  t        t	        j                  g d      d       ddd       t        j                  t        d      5  t        t	        j
                  dt        	      d
       ddd       y# 1 sw Y   xY w# 1 sw Y   _xY w# 1 sw Y   yxY w)z6Raise all exceptions in `_calc_dual_canonical_window`.z!hop=5 is larger than window len.*match      Nz.* Transform not invertible!)g?皙?g333333?r   z"Parameter 'win' cannot be of int.*dtype   )pytestraises
ValueErrorr   nponesarrayint r   r   +test__calc_dual_canonical_window_exceptionsr1   2   s     
z)L	M#BGGAJ2 
N	z)G	H#BHH_$=qA 
I 
z)M	N#BGGAS$91= 
O	N 
N	M	H	H 
O	Ns#    C"C$&C+CC(+C4c                  l   t        j                  t        d      5  t        t	        j
                  d      dd       ddd       t        j                  t        d      5  t        t	        j                  dt        j                  d	d
g      dd       ddd       t        j                  t        d      5  t        t	        j
                  d      dd       ddd       t        j                  t        d      5  t        t	        j
                  d      dd       ddd       t        j                  t        d      5  t        t	        j
                  d      d	dt	        j
                  d             ddd       t        j                  t        d      5  t        t	        j
                  d
      d	dt	        j                  t        j                  d	d
g             ddd       y# 1 sw Y   xY w# 1 sw Y   exY w# 1 sw Y   -xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)z]Verify that exceptions get raised on invalid parameters when
    instantiating ShortTimeFFT. z1Parameter win must be 1d, but win.shape=\(2, 2\)!r!      r4   r#   r(   hopfsNz&Parameter win must have finite entriesr4      z'Parameter hop=0 is not an integer >= 1!r   z)Parameter hop=2.0 is not an integer >= 1!       @z2dual_win.shape=\(5,\) must equal win.shape=\(4,\)!r$   )r6   r7   dual_winz*Parameter dual_win must be a finite array!)	r)   r*   r+   r   r,   r-   r.   infnanr0   r   r   #test_invalid_initializer_parametersr=   ?   s    
z *D 
ERWWV_!2
E 
z *: 
;RXXq"&&!Q/0aA>
; 
z *< 
=RWWQZQ1-
= 
z *< 
= 	RWWQZSQ/
= 
z *> 
?RWWQZQ1rwwqzB
? 
z *; 
<RWWQZQ1 hh1~6	8
< 
<!
E 
E
; 
;
= 
=
= 
=
? 
?
< 
<sH   "G+!4G88"H="H6HAH*+G58HHHH'*H3c                     t        t        j                  d      dd      } t        j                  t
        d      5  d| _        ddd       t        j                  t
        d	      5  d| _        ddd       t        j                  t
        d
      5  d| _        ddd       t        j                  t
        d      5  d| _        ddd       t        j                  t
        d      5  d| _	        ddd       t        j                  t
        d      5  | j                  d       ddd       t        j                  t
        d      5  d| _        ddd       t        j                  t
        d      5  d| j                  z  | _        ddd       t        j                  t
        d      5  | j                  t        j                  d      dddd      }t        |       ddd       t        j                  t
        d      5  | j                  t        j                  d      d       ddd       t        j                  t
        d      5  | j                  t        j                  d      t        j                          ddd       t        j                  t
        d      5  | j#                  ddd       ddd       t        j                  t
        d       5  | j%                  t        j                  | j&                  df      dd!       ddd       t        j                  t
        d"      5  | j%                  t        j                  d#             ddd       t        j                  t
        d$      5  | j%                  t        j                  | j&                  df             ddd       t        j                  t
        d%      5  | j%                  t        j                  | j&                  d&f      d'       ddd       t        j                  t
        d(      5  | j%                  t        j                  | j&                  d&f      dd)       ddd       t        j                  t
        d*      5  | j)                  dd+       ddd       t        j                  t
        d,      5  d-| _        | j)                  d.       ddd       y# 1 sw Y   xY w# 1 sw Y   vxY w# 1 sw Y   XxY w# 1 sw Y   :xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   `xY w# 1 sw Y   #xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   dxY w# 1 sw Y   )xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   OxY w# 1 sw Y   %xY w# 1 sw Y   yxY w)/zwVerify that exceptions get raised when setting properties or calling
    method of ShortTimeFFT to/with invalid values.   r#   r(   r5   z(Sampling interval T=-1 must be positive!r!   Nz*Sampling frequency fs=-1 must be positive!zSfft_mode='invalid_typ' not in \('twosided', 'centered', 'onesided', 'onesided2X'\)!invalid_typz7For scaling is None, fft_mode='onesided2X' is invalid.*
onesided2Xz9Attribute mfft=7 needs to be at least the window length.*   zscaling='invalid' not in.*invalidzphase_shift=3.0 has the unit .*g      @z*-mfft < phase_shift < mfft does not hold.*r4   z!Parameter padding='invalid' not.*   r   )k_offp0p1paddingz)Trend type must be 'linear' or 'constant')detrz2Parameter detr=nan is not a str, function or None!z Invalid Parameter p0=0, p1=200.*d      z&f_axis=0 may not be equal to t_axis=0!)t_axisf_axisz3S.shape\[f_axis\]=2 must be equal to self.f_pts=5.*r3   z5S.shape\[t_axis\]=1 needs to have at least 2 slices.*z'.*\(k1=100\) <= \(k_max=12\) is false!$r8   k1z6\(k1=1\) - \(k0=0\) = 1 has to be at least.* length 4!)k0rP   z3Parameter axes_seq='invalid' not in \['tf', 'ft'\]!)naxes_seqz"Attribute fft_mode=twosided must.*twosided)rR   )r   r,   r-   r)   r*   r+   Tr7   fft_modemfftscale_tophase_shift	_x_sliceszerosnextstft_detrendr<   p_ranger   f_ptsextent)SFTgs     r   "test_exceptions_properties_methodsrc   X   s_    rwwqzqQ
/C	z *5 
6
6 
z *5 
6
6 
z *H 
I %
I 
z *N 
O#
O 
z *H 
I
I 
z)E	FY 
G 
z)J	K 
L	z *; 
<CHH*
< 
z)L	MMM"((2,aA!YMOQ 
N 
z *9 
: 	"I6
: 
z *= 
> 	"BFF3
> 
z)K	LCC  
M 
z *5 
6		"((CIIq>*1Q	?
6 
z *> 
?		"((6"#
? 
z *@ 
A		"((CIIq>*+
A 
z *6 
7		"((CIIq>*s	3
7 
z *C 
D		"((CIIq>*qQ	7
D 
z *C 
D 	

S9
-
D 
z)M	N!

S
 
O	Nw
6 
6
6 
6
I 
I
O 
O
I 
I 
G	F 
L	K
< 
< 
N	M
: 
:
> 
> 
M	L
6 
6
? 
?
A 
A
7 
7
D 
D
D 
D 
O	Ns   S(S$S1>S>)TT	T%4T2,5T?'U5U&U&4U34%V <1V3V&4V'=V44WS!$S.1S;>TTT"%T/2T<?U	UU#&U03U= V
VV$'V14V>W
m)onesidedrB   c                 (   t        j                  t        d|  d      5  t        t	        j
                  d      dz  dd|        d	d	d	       t        t	        j
                  d      dz  ddd
      }t        j                  t        d|  d      5  | |_        d	d	d	       t        t	        j
                  d      dddd      }t        j                  t        d      5  |j                  t	        j
                  d      dz         d	d	d	       d|_        t        j                  t        d      5  |j                  t	        j
                  d      dz         d	d	d	       y	# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   uxY w# 1 sw Y   y	xY w)zVerify that one-sided spectra are not allowed with complex-valued
    windows or with complex-valued signals.

    The reason being, the `rfft` function only accepts real-valued input.
    z#One-sided spectra, i.e., fft_mode='z'.*r!   r?                 ?r#   r(   r6   r7   rV   NrT   psdre   r6   r7   rX   rV   z)Complex-valued `x` not allowed for self.*rB   )r)   r*   r+   r   r,   r-   rV   r   )rd   ra   s     r   $test_exceptions_fft_mode_complex_winrk      s3    
zB1#SI
KRWWQZ]a!<
K rwwqz"}!J
GC	zB1#SI
K
K rwwqzqQ
TC	z)T	UB 
VCL	z)T	UB 
V	U
K 
K

K 
K
 
V	U 
V	Us/   &E#E0 (E<2(F#E-0E9<FFc                     t        t        j                  d      dd      } d| _        t	        j
                  t              5  | j                  }ddd       t	        j
                  t              5  | j                  t        j                  d             ddd       t	        j
                  t              5  | j                  t        j                  d             ddd       y# 1 sw Y   xY w# 1 sw Y   \xY w# 1 sw Y   yxY w)zBEnsure exception gets raised when property `fft_mode` is invalid. r?   r#   r(   r5   rA   N)
r   r,   r-   	_fft_moder)   r*   RuntimeErrorf	_fft_func
_ifft_func)ra   _s     r   "test_invalid_fft_mode_RuntimeErrorrs      s    
rwwqzqQ
/C!CM	|	$EE 
%	|	$bggaj! 
%	|	$rwwqz" 
%	$	 
%	$	$	$	$	$s$   C$0%C06%C<$C-0C9<Dzwin_params, Nx)))r   r9   	   )triangrC   ))kaiser      @rt   ))exponentialN      ?rt   )rw   rt   Nxc           
      T   t        | |d      d}}t        | |d      }t        |d|ddd	      }t        |      }||j                  z
  }t        j                  | |||dddd
      }t        j                  | |||dddd
      }	t        |j                  |j                         t        |	j                  |t        j                  t        |dz        |z        z         dD ]D  fd|||	fD        \  }
}}||
k(  sJ d d| d d|
        ||
k(  r3J d d| d d|
         y)zVerify that `from_window()` handles parameters correctly.

    The window parameterizations are documented in the `get_window` docstring.
    F)fftbinsg      0@Tr8   rT   ri   r(   )r6   r7   rV   rX   rY   )symmetric_winrV   rX   rY   r4   )r6   rU   rV   rW   scalingrY   c              3   6   K   | ]  }t        |        y w)N)getattr).0SFT_n_s     r   	<genexpr>z#test_from_window.<locals>.<genexpr>   s     G4FDgdB'4Fs   zSFT1.=z does not equal SFT0.zSFT2.N)r   r   lenr6   from_windowr   r   r   r,   sqrtsum)
win_paramsrz   w_symr7   w_perSFT0npersegnoverlapSFT1SFT2v0v1v2r   s                @r   test_from_windowr      sP    :r5932Ez2t4E1j!&A7D%jG!H##JGX26-2CD ##JGX27*-2CD DHHdhh'DHHebggc%(mb.@&AABHGT44FG
BRxH5AbT)>rd!B4HHxRxH5AbT)>rd!B4HHx Ir   c                      t        dddddd      } t        t        j                  d      fi | }t        j                  |j
                  fi | }t        |j
                  |j                         y)zVerify the duality of `win` and `dual_win`.

    Note that this test does not work for arbitrary windows, since dual windows
    are not unique. It always works for invertible STFTs if the windows do not
    overlap.
    r#   r(   rT   r?   Nr4   )r6   r7   rV   rW   rX   rY   )dictr   r,   r-   	from_dualr:   r   r   )kwr   r   s      r   test_dual_win_roundtripr      s]     
!JQ
B
)b)D!!$--626DDMM488,r   zscale_to, fac_psd, fac_mag))N      ?      ?)	magnituder9   r(   )ri   r(         ?rX   r   ri   c                 Z   t        t        j                  d      dz  dd|       }|j                  |k(  sJ |j                  |k(  sJ |j                  |k(  sJ |j                  |k(  sJ t        j
                  j                  g d      }|j                  |      }||j                  z  ||j                  z  }}|j                  d       |j                  |t        |            }t        ||       |j                  d       |j                  |t        |            }	t        |	|       y	)
zVerify scaling calculations.

    * Verify passing `scale_to`parameter  to ``__init__().
    * Roundtrip while changing scaling factor.
    r#   r4   r(   r6   r7   rX   )r   r   rC   r   r   r   r   r   rO   ri   N)r   r,   r-   fac_psdfac_magnitudefftirfftr   rX   r   r   r   )
rX   r   fac_magra   xSxSx_magSx_psdx_magx_psds
             r   test_scalingr      s    rwwqzA~1X
FC;;'!!!''';;'!!!'''
*+A	!B#+++R#++-=FFLLIIfQI(EE1LLIIfQI(EE1r   c                     t        t        j                  d      dz  ddd      } | j                  d       | j                  dk(  sJ | j
                  dk(  sJ | j                  dk(  sJ | j                  d       | j                  dk(  sJ | j
                  dk(  sJ | j                  d	k(  sJ | j                  d       t        d
d      D ]  \  }}t        t        j                  d      dz  ddd      } | j                  j                         }| j                  t        t        d
   |             t        | j                  ||z          y)zVerify `scale_to()` method.r#   r4   r(   Nr   r   r9   ri   r   r   )r?   r#   )r   r,   r-   rX   r~   r   r   zipr:   copyr   r   r   )ra   scales_facr:   s       r   test_scale_tor     s"   
rwwqzA~1T
BCLL;;+%%%;;#!!!LL;;%;;!###LL0&9u2771:>qQF<<$$&T'"45u=>h&67 :r   c                  ~   t        t        j                  d      dd      } t        j                  g dg dgt              }g dg d	gg d
g dggg dg d	gg dg dggg dg dgg dg dggg dg dgg dg dggd}|j                         D ]  \  }}| j                  t        j                  |      dddt        t        |            }t        j                  |D cg c]  }|j                          c}      }t        j                  |t        j                        }t        ||d| d        yc c}w )zVerify padding.

    The reference arrays were taken from  the docstrings of `zero_ext`,
    `const_ext`, `odd_ext()`, and `even_ext()` from the _array_tools module.
    r$   r#   r(   r5   )r(   r4   r8   r#   r$   )r   r(   r#   rt   rE   r&   )r   r   r(   r4   r8   )r   r   r   r(   r#   )r8   r#   r$   r   r   )r#   rt   rE   r   r   )r(   r(   r(   r4   r8   )r8   r#   r$   r$   r$   )r#   rt   rE   rE   rE   )r8   r4   r(   r4   r8   )r#   r(   r   r(   r#   )r8   r#   r$   r#   r8   )r#   rt   rE   rt   r#   )r@   r   r(   r4   r8   )r@   r   r(   r#   )r8   r#   r$      rC   )r#   rt   rE         )r[   edgeevenoddr   r4   rI   zFailed 'z
' padding.err_msgN)r   r,   r-   r.   floatitemsrZ   r   r   r   asarrayfloat64r   )ra   r   dp_xxgeny_yys           r   test_x_slices_paddingr   &  s    rwwqzqQ
/C
/#34EBA#_5#%568"O4"$679"O4"$457"$56!#568	9A '')BmmBHHQKAq$x:LmMXX3/3Rrwwy3/0ZZ"**-B(2$j(AB	 /s   "D:
c                      t        t        j                  d      dd      } | j                  sJ t        t        j                  d      dd      } | j                  rJ y)zVerify `invertible` property. r?   r#   r(   r5   rt   N)r   r,   r-   
invertiblera   s    r   test_invertibler   =  sI    
rwwqzqQ
/C>>>
rwwqzqQ
/C~~~r   c                  V   t        t        j                  d      dd      } | j                  dk(  sJ | j                  dk(  sJ | j
                  dk(  sJ | j
                  dk(  sJ | j                  d      dk(  sJ | j                  d      d	k(  sJ | j                  d      d
k(  sJ t        j                  t        d      5  | j                  d       ddd       t        j                  t        d      5  | j                  d       ddd       y# 1 sw Y   ?xY w# 1 sw Y   yxY w)z=Ensure that minimum and maximum values of slices are correct.r?   r#   r(   r5   r   r   )r#   r(   r   rE   )r#   r4   z^Parameter n must ber!   r8   N)r   r,   r-   p_mink_minlower_border_endp_maxk_maxupper_border_beginr)   r*   r+   _post_paddingr   s    r   test_border_valuesr   E  s    
rwwqzqQ
/C99>>99??6)))6)))99R=A99R=B!!"%///	z)?	@q! 
A	z)?	@! 
A	@ 
A	@	@	@s   D8DDD(c                     t        j                  g d      } t        | dd      }|j                  dk(  sJ t        t        j                  |       dd      }|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d	      d
k(  sJ |j                  d      d
k(  sJ |j                  d      d
k(  sJ d|_        t        j                  t              5  |j                  d      }ddd       t        j                  t              5  |j                  }ddd       y# 1 sw Y   8xY w# 1 sw Y   yxY w)zLEnsure that the border calculations are correct for windows with
    zeros. )r   r   r   r   r   r   r   ry   r(   r5   )r   r      r#   )rE   r(   r$   r   )$   r4         r@   N)r,   r.   r   r   flipr   _hopr)   r*   rn   r   r   )wra   rr   s      r   test_border_values_exoticr   U  s    	*+A
qaA
&C6)))
rwwqzra
0C!!!$///!!!$///!!"%000!!"%000!!"%000CH	|	$IIaL 
%	|	$II 
%	$ 
%	$	$	$s   &D/D;/D8;Ec                  *   t        t        j                  d      dd      } | j                  dk(  sJ | j                  dk(  sJ | j
                  dk(  sJ t        j                  d| j                  d            | j
                  z  }t        | j                  d      |       t        | j                  dd	d
      |d	d
        d| _        | j                  dk(  sJ | j                  dk(  sJ d| _        | j                  dk(  sJ | j                  dk(  sJ y)z1Verify that the times of the slices are correct. r?   r#   r4   r5   r   r9   r   r   r(   r8   r   r   N)
r   r,   r-   rU   r7   delta_taranger   r   t)ra   t_stfts     r   test_tr   j  s    
rwwqzqQ
/C55C<<66R<<;;'!!!YYq#))B-(3;;6FCEE"Iv&CEE"aOVAa[1CE55C<<66Q;;CF66S==55A::r   zfft_mode, fre   )        ry   r9   rB   rT   )r   ry   r9                centered)r   r   r   ry   r9   rV   c                 v    t        t        j                  d      dd| d      }t        |j                  |       y)z)Verify the frequency values property `f`.r$   r#   ri   )r6   r7   rV   rX   N)r   r,   r-   r   ro   )rV   ro   ra   s      r   test_fr   {  s/     rwwqzqQ %'CCEE1r   rR   r      r$   r   c                 x   t        t        j                  |      |||      }|j                  |       d   }|j                  |       d   |j                  z   }||j                  dz  z
  ||j                  dz  z
  }}|j
                  d   }|j
                  d   |j                  z   }	||j                  dz  z
  |	|j                  dz  z
  }}
|j                  | dd      ||||	fk(  sJ |j                  | dd      ||	||fk(  sJ |j                  | dd      |||
|fk(  sJ |j                  | dd      |
|||fk(  sJ y	)
z.Ensure that the `extent()` method is correct. rh   r   r@   r4   tfFftTN)r   r,   r-   r   r   ro   delta_fr`   )rR   rd   rV   ra   t0t1t0ct1cf0f1f0cf1cs               r   test_extentr     s@   
 rwwqzqQ
BC	q!B	q"	#BCKK!O#R#++/%9C	qB	rS[[	 BCKK!O#R#++/%9C::au%"b"b)9999::au%"b"b)9999::at$c3(<<<<::at$c3(<<<<r   c                     t        t        j                  d      dd      } t        j                  d      t        j                  d      }}| j	                  |      | j	                  |      }}t        | j                  |      |j                  dz  |j                  dz  z          t        | j                  ||      ||j                         z         y)z2Verify spectrogram and cross-spectrogram methods. r?   r#   r(   r5   r   r4   N)
r   r,   r-   r   r   r   r   realimagconj)ra   r   yXYs        r   test_spectrogramr    s    
rwwqzqQ
/C772;		"qA88A;qACOOA&	!&&!)(;<COOAq)1qvvx<8r   r?   rt   c                 f   t         j                  j                  d       t         j                  j                  |       }t        j                  |       d}}t        t        t              d| | dz   | dz   gg dd|  dz   d| dz  | dz
  g      }t        |j                          D ]y  \  }}}}|d	k(  r|t        ||| ||||
      }	|	j                  |      }
|	j                  |
      }t        |j                  |j                        |dd|d|d|d|z          { t        ||d      }	d|	_        t#        j$                  t&              5  |	j                  |       ddd       t#        j$                  t&              5  |	j                  |       ddd       y# 1 sw Y   =xY w# 1 sw Y   yxY w)z^Test roundtrip `ifft_func(fft_func(x)) == x` for all permutations of
    relevant parameters. i$ r#   Nr(   r4   )Nr   ri   r   )rV   rW   r~   rY   rB   )r7   rV   rW   rX   rY   z!_fft_func() roundtrip failed for zf_typ=z, mfft=z
, scaling=z, phase_shift=r   r7   invalid_fft)r,   randomseedrandr-   r   r   r   r   valuesr   rp   rq   r   astyper'   rm   r)   r*   rn   )rR   x0r   h_nppf_typrW   r~   rY   ra   X0x1s               r   test_fft_func_roundtripr    s    IINN7		BWWQZsA	-(AqsAaC *A2a4AFAaC0	
2B
 .5biik-B)tWkL W_1ca%d$+F]]2^^B		"((+R C!5((TG;gZ+H!I	J .C q#!
$C!CM	|	$b 
%	|	$r 
%	$ 
%	$	$	$s   F F'F$'F0i   c                    d}t        j                  d      d}}t        j                  |      }d|| <   t        ||ddd      }|j	                  |      }|j                  |dz        }|j	                  |d| d	      }|j	                  ||d d	      }	|j                  |      d   |j                  z
  }
|j                  d   |j                  z
  }t        |ddd|
f   |ddd|
f   d
|        t        |	dd|df   |dd||	j                  d   z
  df   d
|        t        j                  |ddd|
f   |dd|
df   |	ddd|f   z   |	dd|df   f      }t        ||dd
|        |j                  |d|      }t        ||dd
|        |j                  |d|dz        }t        |d|dz   |dd
|        |j                  ||dz  |      }t        ||dz  d |dd
|        y)z:Roundtrip for an impulse being at different positions `i`.r  r?   r8   r(   N)r6   r7   rX   rY   r4   r[   r   zi=r   g:0yE>atolr   r   )r,   r-   r[   r   r   nearest_k_pr   r   r   r   shapehstackr   )r  rR   r   r  r   ra   r   n_qSx0Sx1q0_ubq1_leSx01r   y0y1s                   r   test_impulse_roundtripr!    s    	AWWQZsA
AAaD
qca$D
IC	!B
//!q&
!C
((1Tc7G(
,C
((1ST7G(
,C""3'*SYY6E  #cii/EC6E6	NBq&5&yMbaT7CC56	NBq%		!*<*='=$>!qdG% 99c!VeV)n!UV)ns1fuf9~5!UV)n& 'D B4BA49		"aAAqttW5	2q!Q$	BAeq!tHbttW=	2qAvq	!BAa1fgJ"!w?r   r6   )r(   rC   r?   c                 x   t         j                  j                  d       t        j                  d      dz  }d|t	        |      dz  d t        || d      }dt         j                  j                  d	      z  }|j                  |      }|j                  |t	        |      
      }t        ||dd| dz          y)z6An asymmetric window could uncover indexing problems. iK[  rE   r?   r(   r4   Nr  r   @   rO   z%Roundtrip for asymmetric window with z hop= failed!r   )
r,   r  r  r   r   r   randnr   r   r   )r6   r   ra   r   r   r  s         r    test_asymmetric_window_roundtripr&    s     IINN5
		"AAc!faijM
q#!
$C
RYY__R  A	!B	2#a&	!BB$K&,vX$6%7 8r   m_numrC   c                    t        t        j                  |       | dz  d      }t        j                  | dz        }t        j                  |      }|j                  |      }|j                  ||      }t        ||d|dd|  dz   	       t        j                  t        d
|dz
   d      5  |j                  |dd        ddd       t        j                  t        d|j                  d   dz
   d|j                  d    d      5  |j                  |ddddf   |       ddd       y# 1 sw Y   nxY w# 1 sw Y   yxY w)z/Verify that the shortest allowed signal works. r4   r(   r  rO   z#Roundtrip minimal length signal (n=)z for z sample window failed!r   z	len\(x\)=z must be >= ceil.*r!   Nr@   zS.shape\[t_axis\]=z needs to have at least z slices)r   r,   r-   mathceilr   r   r   r)   r*   r+   r  )r'  ra   rR   r   r   r  s         r   test_minimal_length_signalr,    s4    rwwu~uaxA
6C		%'A

A	!B	2!	BB%Iqd!#L%*5'1G#H$I J	zIacU:L)M	N3B 
O	z,>rxx{1}o1"((1+g*G 
H		"QV*	#
H 
H 
O	N
H 
Hs   1D3D?3D<?Ec                  j   t        t        j                  d      dd      } | j                  dk(  sJ d| j                         | j                  dk(  sJ d| j                  d	       | j
                  d
k(  sJ d| j                  d       | j                  \  }}|dk(  sJ d|d       |dk(  sJ d|d       d}| j                  |      x}dk(  sJ d|d       | j                  |      x}dk(  sJ d|d       | j                  |      \  }}|dk(  sJ d|d       |dk(  sJ d|d       y) aH  Verify example in "Sliding Windows" subsection from the "User Guide".

    In :ref:`tutorial_stft_sliding_win` (file ``signal.rst``) of the
    :ref:`user_guide` the behavior the border behavior of
    ``ShortTimeFFT(np.ones(6), 2, fs=1)`` with a 50 sample signal is discussed.
    This test verifies the presented indexes.
    r   r4   r(   r  r8   z(Slice middle is not 3 but SFT.m_num_mid=r@   zLowest slice SFT.p_min=z
 is not -1zLowest slice sample SFT.p_min=z
 is not -5zFirst unaffected slice p_lb=z	 is not 2r$   zFirst unaffected sample k_lb=z	 is not 52      zLast slice p_max=z must be 277   zLast sample k_max=z must be 55r   zFirst upper border slice p_ub=z must be 24-   zFirst upper border slice k_ub=z must be 45N)
r   r,   r-   	m_num_midr   r   r   r   r   r   )ra   k_lbp_lbrR   r   r   k_ubp_ubs           r   test_tutorial_stft_sliding_winr8    sn    rwwqz1
+C ==AL!JCMM;KLL99?B6CII<zBB?99?I=399,jII?%%JD$19@5wi@@919A6yAA9
AYYq\!Eb(K,>x{*KK(YYq\!Eb(L,?*LL(''*JD$2:E84'EE:2:E84'EE:r   c            	         d\  } }t        j                  |      | z  }t        j                  dt         j                  z  dz  |d|dz  z  z
  z        }d\  }}dd| z  f}t	        || |||d	d
      \  }}}	t        |	d      }
t        j                  || ||ddd      }|j	                  |      }t        |
|ddddf          t        t        |dddf         j                         t        |dddf         j                         fd       t        |	| |||d	d
      \  }}|j                  ||      }t        |      |dz   k(  sJ t        |d| |       t        ||       t        |j                  |      t        |      dz         y)zVerify STFT example in "Comparison with Legacy Implementation" from the
    "User Guide".

    In :ref:`tutorial_stft_legacy_stft` (file ``signal.rst``) of the
    :ref:`user_guide` the legacy and the new implementation are compared.
    rL   i                 @F   r%   r4   r/  (   r   {Gz?Fspectrum)return_onesidedr~   r   axesr   r   NrV   rX   rY   r@   r(   )g<>g(!Gڪ>)input_onesidedr~   rO   rt   )r,   r   exppir   r	   r   r   r   absminmaxr   r   r   )r7   Nt_zzr   r   r   f0_ur   Sz0_uSz0ra   Sz1t0_rz0_rz1_rs                   r   test_tutorial_stft_legacy_stftrU  !  s    EB
))A,
C
r"%%x"}cC1Hn 456AGXtby
!C 1b#w+0*FOD"e
5q
!C 
"
"3GX,6,7TKC ((1+CCQ"W&SQT^'')3s1a4y>+=+=+?@AC ub#w)+JD$99SQ9Dt9AD!Ha D! COOA&CA6r   c                  T   d\  } }t        j                  |      | z  }t        j                  dt         j                  z  dz  |d|dz  z  z
  z        }d\  }}dd| z  f}t	        || |||d	d
dd	      \  }}}	t        |      t        |	d      }}
t        j                  || ||ddd	      }|j                  |d||z
  |j                  z  |dz        }|j                  |d||z
  |j                  z  |dz        }t        ||       t        |
|j                         t        ||       y	)zVerify spectrogram example in "Comparison with Legacy Implementation"
    from the "User Guide".

    In :ref:`tutorial_stft_legacy_stft` (file ``signal.rst``) of the
    :ref:`user_guide` the legacy and the new implementation are compared.
    r:  r;  r<  r%   r4   r=  r   r?  NFr@  complex)detrendrA  r~   moder   rB  r   r   rD  )rG   rH   k_offset)r,   r   rF  rG  r   r	   r   r   r   r6   r   r   ro   )r7   rK  rL  rM  r   r   r   f2_ut2Sz2_uf2Sz2ra   Sz3t3s                  r   %test_tutorial_stft_legacy_spectrogramrb  J  s3    EB
))A,
C
r"%%x{cCQJ./0AGXtby
!C "!Rgx27'02OD"e tnhu15B 
"
"3GX,6/35C ((1(
sww6A(
NC	qQAhJ3772W\	JBBBCr   c            
          d} t        t        j                  d      dz  d|       }t        j                  | t        j                        }|j                  |      }|j                  |j                  d   ddd|j                  d   f      }t        j                  |d	d
      }dt        j                  |j                  j                        j                  z  }t        d      D ]  }t        j                  |t        j                  | dddf|            }|j                  ||      }t        |t        j                  |d|             |j!                  || |      }	t        |	||       |j!                  || ||j"                  dz
        }
t        |
||       t        j                  ||dfd|f      }t        |t        j                  |d|             |j!                  || |d      }t        |||        y)zJVerify correctness of four-dimensional signal by permuting its
    shape. r   r?   r8   r5   r&   r   r(   r@   )r   r@   )r@   r   r4   r#   axis)rP   rN   r  rP   rN   rM   )rP   rM   rN   N)r   r,   r-   r   r   r   reshaper  moveaxisfinfor   r'   
resolutionrangerollr   r   ndim)rR   ra   r
  r  SxTr  r  r   Syyb0yb1SyTybTs                r   test_permute_axesru  k  s    	A
rwwqz!|q
1C	1BJJ	'B
((2,C
++syy|Q1ciim<
=C
++c7G
,Crxx&111D1XJJr277Aq!Q<34XXaaX BKKQ23iiqi+QT*iiq27719i=QT*kk"q"gAw/R[[a34ii!Bi7QT* r   )rT   r   re   rB   c                    d}t        j                  d|z  dz  t         j                        j                  d|d      }t	        t        dd      ddd|       }|j                  |d	      }|j                  ||dd
      }t        ||j                  |j                        d       t        t        |j                  d         t        |j                  d               D ]_  \  }}|j                  ||dd|ddf   |      }t        |||dd|f   j                  |j                        dd| d| d| dz          a y)zTest roundtrip of a multidimensional input signal versus its components.

    This test can uncover potential problems with `fftshift()`.
    rt   r#   r4   r&   hannr(   r   rj   rd  r@   rg  zMultidim. roundtrip failed!r   r   NrO   z"Multidim. roundtrip for component zx[z, :, z] and fft_mode=r$  )r,   r   r   rh  r   r   r   r   r   r	  r'   r   rl  r  )	rV   rR   r   ra   r   r   r  jr   s	            r   test_roundtrip_multidimensionalry    s(    	
A
		!A#a%rzz*221a;A
z&!,! +h@C	!!	B		"1R	0AAqxx(2OPaggaj)5+<=1YYr!Q1*~!Y,AaAgJ--bhh7 DQCuQC'7h[A!B	C >r   rY   )r   r#   Nc                 h   d}t        j                  ddddd|       }t        j                  d|z  t        	      j                  d|      }|j                  |      }|j                  ||
      }t        ||dt        j                  |j                  j                        j                  z  d       y)zTest roundtrip of a 2 channel input signal with `mfft` set with different
    values for `phase_shift`

    Tests for Issue https://github.com/scipy/scipy/issues/21671
    r   rw  r(      rC   rE   )r7   r   r   rW   rY   r4   r&   rO   z2-dim. roundtrip failed!r  N)r   r   r,   r   r   rh  r   r   r   rj  r   r'   rk  )rY   rR   ra   r   r   r   s         r   test_roundtrip_two_dimensionalr|    s     	A

"
"6aa(*EC
		!A#U#++Aq1A	!B		"	AAqq288CGGMM#:#E#EE68r   zwindow, n, nperseg, noverlap))boxcarrK   r   r   )r}  rK   r   rt   )bartlette   r      )rw           )tukeyr     r  r#  )rw  r  r     )r}  rK   r   r8   )r~  r  r   %   )rw  r  r     )r  r  r     )rw  r  r  r$   r   r   c                    t         j                  j                  d       t        | |      }t	        |||z
  ddd      }dt         j                  j                  |      z  dt         j                  j                  |      z  z   }|j                  |      }|j                  |t        |            }t        ||d	
       dt         j                  j                  |      z  }	|j                  |	      }
|j                  |
t        |            }t        |	j                  t         j                        |d
       |	j                  t         j                        }|j                  |      }|j                  |t        |            }|j                  }t        ||j                  t         j                        d
       t        |j                  t        j                  |j                        d
       y)zRoundtrip test adapted from `test_spectral.TestSTFT`.

    The parameters are taken from the methods test_roundtrip_real(),
    test_roundtrip_nola_not_cola(), test_roundtrip_float32(),
    test_roundtrip_complex().
    i$ r(   rT   N)r7   rV   rY   r   y              $@rO   z#Roundtrip for complex values failedr   z!Roundtrip for float values failedz(Roundtrip for 32 Bit float values failed)r,   r  r  r   r   r%  r   r   r   r   r	  
complex128float32r   r   
zeros_like)windowrR   r   r   r   ra   rM  Szz1r   r   r  x32Sx32x32_1x32_1_rs                   r   test_roundtrip_windowsr    so   ( IINN767#A
q'H,Z#')C 	RYY__Q#		(:"::A	!B	2#a&	!BAr#HI
RYY__QA	!B	2#a&	!BAHHR]]+R?A ((2::
C88C=DIIds3xI(EjjGC

3FHCHHbmmCHH5FHr   signal_type)r   rW  c                    t         j                  j                  d       t        j                  dt        j                  dt         j
                  d      z        }t        |ddd      }d	t         j                  j                  d
      z  }| dk(  r|d|z  z   }|j                  |      }|j                  |t        |            }t        |j                  t         j                        |d       y)zTest roundtrip for complex-valued window function

    The purpose of this test is to check if the dual window is calculated
    correctly for complex-valued windows.
    i r;  r   r?   r8   r(   rT   )r7   rV   r      rW  rO   z*Roundtrip for complex-valued window failedr   N)r,   r  r  rF  linspacerG  r   r%  r   r   r   r   r	  r  )r  r   ra   rM  r  r  s         r   test_roundtrip_complex_windowr    s     IINN7
&&BKK255!,,
-C
sA!j
9C
RYY__R  AiQJ	!B	2#a&	!BAHHR]]+RHJr   c            	         t         j                  j                  d      } d}d}d\  }}t        | ||||      \  }}t	        j
                  ||||ddd      }|j                  | d	d
t        |       |z
  |j                  z  |dz        }t        |j                  |       t        t        j                  |d      |       y)zCompare `welch` function with stft mean.

    Ported from `TestSpectrogram.test_average_all_segments` from file
    ``test__spectral.py``.
    r  ry   )r  r   )rE   r4   rB   ri   NrD  constantr   r4   )rJ   rG   rH   rZ  r@   rd  )r,   r  r%  r   r   r   r   r   r6   r   ro   mean)	r   r7   r  r   r   fwPwra   Ps	            r   test_average_all_segmentsr    s     			A	BFGX1b&'84FB

"
"62w,85/35C 	
qF8Ocgg5
 	 	LA CEE2BGGAB',r   z"window, N, nperseg, noverlap, mfft))rw  r  r  r  i   )rw  r  r  r  i  )r}  rK   r   r   !   )r  r  r  r#  r  )r}  r  r   r   N)rw    r  r  N)r}  rK   r   r   N)r}  rK   r   rt   NrI   rK  rW   c                    t        j                  |d      }|t        j                  dt        j                  z  dz        z  }t        j                  | d||d|      }|j                  ||      }	|j                  |	|	      }
t        |
|j                  t        j                        d
| d       |j                  ||      }|j                  ||	      }t        ||dd| dz          y)an  Test the parameter 'padding' of `stft` with roundtrips.

    The STFT parametrizations were taken from the methods
    `test_roundtrip_padded_FFT`, `test_roundtrip_padded_signal` and
    `test_roundtrip_boundary_extension` from class `TestSTFT` in  file
    ``test_spectral.py``. Note that the ShortTimeFFT does not need the
    concept of "boundary extension".
    i]  )sizerandom_staterg   r#   r(   rT   )rV   rW   r   rO   zFailed real roundtrip with 'z	' paddingr   zFailed complex roundtrip with z 'N)normal_distributionrvsr,   rF  rG  r   r   r   r   r   r	  r  )r  rK  r   r   rW   rI   r   rM  ra   r   r  r  r  s                r   test_stft_padding_roundtripr    s    . 	QT:A	BFF2:>""A

"
"61gx,6TCC	!W	%B	2!	BB/:7)9MO 
!W	%B	2!	BB#C	+$, -r   N_x)r     r  r  i9  w_size)r  r  t_step)r#   r#  f_c)g      @g      7@c           	         t        j                  t        j                  d|dz         |z  t         j                  z        }t	        ||ddd      }dt        j
                  |j                        j                  z  }t        | |      } t        j                  |       j                  t         j                        }t        j                  dt         j                  z  |z  |z  |j                  z        }t        j                  ||dz   g      }|j                  |      }	|j                  |	|       }
|j                  t        j                   t        j"                  t        j$                  |	d	         dz  d
               }|	j&                  d
   |j(                  k(  sJ t        j*                  |j                  dk\        sJ t        j$                  ||z
        dk  sJ t-        ||
|       t        j"                  |dz  d      |j                  z  }|	j.                  dz  |	j0                  j.                  dz  z   }t        j"                  t        j"                  |d      |j2                  z  d      |j4                  z  }t-        |||       t         j6                  j9                  d       t         j6                  j;                  d|       }|j                  |      }	|j                  |	|       }
|	j&                  d
   |j(                  k(  sJ t        j*                  |j                  dk\        sJ t        j$                  ||z
        dk  sJ t-        ||
|       t        j"                  |dz  d      |j                  z  }|	j.                  dz  |	j0                  j.                  dz  z   }t        j"                  t        j"                  |d      |j2                  z  d      |j4                  z  }t-        |||       t        j<                  d	| f      }|j                  |      }	|j                  |	|       }
|
j&                  |j&                  k(  sJ y)as  Test if a `psd`-scaled STFT conserves the L2 norm.

    This test is adapted from MNE-Python [1]_. Besides being battle-tested,
    this test has the benefit of using non-standard window including
    non-positive values and a 2d input signal.

    Since `ShortTimeFFT` requires the signal length `N_x` to be at least the
    window length `w_size`, the parameter `N_x` was changed from
    ``(127, 128, 255, 256, 1337)`` to ``(128, 129, 255, 256, 1337)`` to be
    more useful.

    .. [1] File ``test_stft.py`` of MNE-Python
        https://github.com/mne-tools/mne-python/blob/main/mne/time_frequency/tests/test_stft.py
    r   r  rB   ri   )r7   rV   rX   r4   ry   rO   r   r(   rd  r   rf  r@   iۂ$ N)r,   sinr   rG  r   rj  r'   rk  rJ  r	  r   rU   r.   r   r   ro   argmaxr   rH  r  r_   allr   r   r   r   r   r  r  r%  r[   )r  r  r  r  r  ra   r  r   r   r   xpmax_freqE_xaX2E_Xs                  r   test_energy_conservationr  /  sJ   & VVBIIb&2+.7"%%?@F
vv$ %'CRXXfll#...D
c6
C
		#bjj)A
q255y3"SUU*+A
!QVAA	1	BuuRYYrvvbffQqTla&7a@ABH771:"""66#%%2+66(S.!B&&&Ar% &&AB
#%%
'C
&&!)affkk1n
$C
&&"%3"
=
KCC4( IINN7
		3AA	1	B771:"""66#%%2+66(S.!B&&&Ar% &&AB
#%%
'C
&&A+q(
(C
&&"%3"
=
KCC4( 	!SAA	1	B88qwwr   )F__doc__r*  	itertoolsr   typingr   r   r   numpyr,   r)   scipy._lib._array_apir   r   	scipy.fftr	   scipy.statsr
   r  scipy.signalr   r   r   r   r   scipy.signal._short_time_fftr   r   r   r   scipy.signal.windowsr   r   r1   r=   rc   markparametrizerk   rs   r/   r   r   r   r   r   r   r   r   r   r   r   r  r  rl  r!  r&  r,  r8  rU  rb  ru  ry  r|  r  r  r  r  r   r  r0   r   r   <module>r     sm  &   * *   B  3 D D8 8 )
>82AH 89 M   : .
# ) ,6 7
IS I7
I6- 5+,7#56 	,480C. *" %|4'6%'=>%'=>@A
] A
 r2h'q!f%j*%=>= = ? & (=$9 q!f%s  &> eBi(@ )@@ 	*8# 8 +8 1a&)$ *$ F4&7RB+: KMCm CMC& 68D 8 78  723Hc HC H3 H3HD (;<J =J&-. =	89 HX$67-3 - - -&)- 89-4  :;:.7+	*=# =s =C =e = + , / <=r   