
    PhN                     D   d Z ddlZddlmZmZ ddlZddlmZ ddl	m
Z
 ddl	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d	lmZmZmZ 	 	 	 dd
Z	 	 	 ddeej8                  ej8                  eeef   f   fdZ	 	 	 ddZ	 	 	 ddZ 	 	 	 ddZ!	 	 	 ddZ"	 	 	 ddZ#	 	 	 ddZ$y)an  Helpers to utilize existing stft / istft tests for testing `ShortTimeFFT`.

This module provides the functions stft_compare() and istft_compare(), which,
compares the output between the existing (i)stft() and the shortTimeFFT based
_(i)stft_wrapper() implementations in this module.

For testing add the following imports to the file ``tests/test_spectral.py``::

    from ._scipy_spectral_test_shim import stft_compare as stft
    from ._scipy_spectral_test_shim import istft_compare as istft

and remove the existing imports of stft and istft.

The idea of these wrappers is not to provide a backward-compatible interface
but to demonstrate that the ShortTimeFFT implementation is at least as capable
as the existing one and delivers comparable results. Furthermore, the
wrappers highlight the different philosophies of the implementations,
especially in the border handling.
    N)castLiteral)assert_allclose)ShortTimeFFT)csd
get_windowstftistft)	const_exteven_extodd_extzero_ext)FFT_MODE_TYPE)_spectral_helper_triage_segments_median_biasc           	      h   |dvrt        d|d      t        t        t        t        dd}||vr,t        d| ddt        |j                                z         | j                  d	k(  r\t        j                  | j                        t        j                  | j                        t        j                  | j                        fS |t        |      }|d
k  rt        d      t        ||| j                  |
         \  }}||}n||k  rt        d      t        |      }||dz  }nt        |      }||k\  rt        d      ||z
  }| j                  |
   }|||   } || |dz  |
      } |	rt        j                  | |
d      } |dz  d
k(  r|dz  d
k(  r|dz  d
k(  r	| dddf   } | j                  d   |z
   |z  |z  }t        | j                  dd       |gz   }t        j                  | t        j                  |      fd      } t        j                  | d|
      } ddd|   }t        j                   |       r|rd}t#        t$        |rdnd      }t'        ||||||d      }|dz  }d	}|	r| j                  |
   n||z   d
z   }| j                  |
   |z
  |z  d
z   }|du rdn|}|j)                  | |||||
      }|j+                  |d	||z
  |d	n|      }| j,                  t        j.                  t        j0                  fv r|j3                  t        j0                        }|j4                  ||fS )aO  Wrapper for the SciPy `stft()` function based on `ShortTimeFFT` for
    unit testing.

    Handling the boundary and padding is where `ShortTimeFFT` and `stft()`
    differ in behavior. Parts of `_spectral_helper()` were copied to mimic
    the` stft()` behavior.

    This function is meant to be solely used by `stft_compare()`.
    )psdspectrumzParameter scaling=z not in ['spectrum', 'psd']!N)evenoddconstantzerosNzUnknown boundary option 'z', must be onez of: r      "nperseg must be a positive integerinput_length.nfft must be greater than or equal to nperseg.   #noverlap must be less than nperseg.axis.	magnituder   r   r   Fonesidedtwosidedfft_modemfftscale_tophase_shift)k_offsetr"   )r-   )
ValueErrorr   r   r   r   listkeyssizenpemptyshapeintr   moveaxisconcatenater   iscomplexobjr   r   r   stft_detrendtdtypefloat32	complex64astypef)xfswindownpersegnoverlapnfftdetrendreturn_onesidedboundarypaddedr"   scalingboundary_funcswinnstepnext_funcnaddzeros_shaper+   r)   STk_offp0nnp1detrSxxr:   s                                wC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\scipy/signal/tests/_scipy_spectral_test_shim.py_stft_wrapperrZ   "   sY    )).gZ/KLMM '$"+' 	"N ~%4XJnM n&9&9&;!< =>? @ 	@vv{xx "((177"3RXXagg5FFFg,Q;ABB $FG12@LC |	IJJ4yA:x=7>??hE	A !(+Q
. KK4$ q5A:'A+*x!|q/@#tt)A''"+g%&.'91773B<(D61NNArxx45B?KK2t$ (6w?H	qoM:jQH	c5"xd'T
;B qLE	
B agaiB
''$-'
!e	+a	/Be#4D
//!T2rE/
EC
RBG8+?aUKAww2::r||,,jj&44C<    returnc           	         | j                   dk  rt        d      |	|k(  rt        d      | j                  |   }|rd| j                  |	   dz
  z  }n| j                  |	   }||}nt        |      }|dk  rt        d      ||r||dz   k(  r|}n|}n||k  rt        d      t        |      }||dz  }nt        |      }||k\  rt        d      ||z
  }t	        |t
              st        |      t        u rt        ||      }nXt        j                  |      }t        |j                        dk7  rt        d	      |j                  d
   |k7  rt        d|       ||dz
  |z  z   }t        t        |rdnd      }t        t        d   ddd|
         }t        ||||||d      }|r*|dz  d
k(  r|n|dz
  }|j                   |dz  z   }||z
  |z   }nt#        d      |j%                  | |||	|      }t        j&                  ||z
        |j(                  z  }|j+                  ||z
        d
   }|||j,                  d
   |ffS )a.  Wrapper for the SciPy `istft()` function based on `ShortTimeFFT` for
        unit testing.

    Note that only option handling is implemented as far as to handle the unit
    tests. E.g., the case ``nperseg=None`` is not handled.

    This function is meant to be solely used by `istft_compare()`.
    r   zInput stft must be at least 2d!z/Must specify differing time and frequency axes!r   Nr   r   r    zwindow must be 1-Dr   zwindow must have length of r&   r'   )r$   r   r$   r   r%   r(   z<boundary=False does not make sense withShortTimeFFT.istft()!)k0k1f_axist_axis)ndimr.   r4   r5   
isinstancestrtypetupler   r2   asarraylenr   r   r   r   k_minNotImplementedErrorr
   arangeTupper_border_beginlower_border_end)ZxxrA   rB   rC   rD   rE   input_onesidedrH   	time_axis	freq_axisrJ   nseg	n_defaultrM   rL   outputlengthr)   r+   rR   jr^   r_   r@   r:   k_his                            rY   _istft_wrapperrx      s    xx!|:;;IJKK99YDsyy+a/0	IIi(	 g,Q;ABB|w)a-7DD	IJJ4yA:x=7>??hE &#$v,%"7)jj syy>Q12299Q<7":7)DEEd1fe^+L M:ZPHG./!,U;GDFH 
c5"xd'T
;B {a'GWq[XX1$A"! #: ; 	; 	9YGA
		"r'RTT!A  b)!,Da"%%a($///r[   c                    t        | |||||||||	|
      \  }}}t        |j                        dk\  r|j                  dkD  r	|j                  d   dkD  r|dk(  rt	        |j                  d         }t        j                  |      rYt        j                  t        j                  |      d      dt        j                  t        j                  |      d      z  z   }nt        j                  |d      }||z  }||fS |dk(  r|j                  d      }||fS t        d	|       t        j                  ||j                  d
d       }||fS )zWWrapper for the `csd()` function based on `ShortTimeFFT` for
        unit testing.
    r   r   r#   r   medianr!   y              ?meanz(average must be "median" or "mean", got N)_csd_test_shimrh   r4   r1   r   r2   r8   rz   realimagr{   r.   reshape)r@   yrA   rB   rC   rD   rE   rF   rG   rJ   r"   averagefreqs_Pxybiass                   rY   _csd_wrapperr      s=    #1aVWh#*OWdLME1c 399~sxx!|99R=1("#CIIbM2??3'99RWWS\;"))BGGCLr"BBCC ))Cb1Ct #: F"hhBh' #:	 !#KG9!UVV**S#))CR.1C#:r[   c                    t        | |||||||||	|
d      \  }}}t        | |||||||||	|
      \  }}t        j                  j	                  ||       |j
                  r-t        t        j                  |      j                         d      nd}t        j                  |j                        j                  |z  }t        j                  j	                  |||       |||fS )aV  Compare output of  _spectral_helper() and ShortTimeFFT, more
    precisely _spect_helper_csd() for used in csd_wrapper().

   The motivation of this function is to test if the ShortTimeFFT-based
   wrapper `_spect_helper_csd()` returns the same values as `_spectral_helper`.
   This function should only be usd by csd() in (unit) testing.
   r   )moder   )atol)r   _spect_helper_csdr2   testingr   r1   maxabsfinfor;   
resolution)r@   r   rA   rB   rC   rD   rE   rF   rG   rJ   r"   r   r:   r   freqs1Pxy1amax_Pxyr   s                     rY   r|   r|     s     %Q2vw$%,ow*/1ME1c %Q2vw$%,owNLFD JJvu-,/HHs266#;??$a(!H88CII))H4D JJtSt4!S=r[   c           	         || u }t        |
      }
t        j                  |       } |st        j                  |      }|st        | j                        }t        |j                        }|j                  |
       |j                  |
       	 t        j                  t        j                  |      t        j                  |            j                  }|rM| j                  dk(  rt        j                  | j                        t        j                  | j                        fS | j                  dk(  s|j                  dk(  rYt        | j                  |
   |j                  |
   g      fz   }t        j                  t        j                  |      d|
      }||fS |t        |      }|dk  rt        d      |r| j                  |
   n%t        | j                  |
   |j                  |
         }t        |||      \  }}||}n||k  rt        d      t        |      }||d	z  }nt        |      }||k\  rt        d
      ||z
  }t        j                  |       r|rd}t        t         |rdnd      }ddd|	   }t#        ||||||d      }|j%                  || |du rdn|d||z
  |j&                  z  |d	z  |
      j)                         }|rl|
dk  r|j*                  dz
  |
z   n|
}t        j                  ||d      }|dd|j,                  d	z  dk(  rdndfxx   d	z  cc<   t        j                  |d|      }|j.                  |fS # t        $ r}t        d      |d}~ww xY w)zWrapper for replacing _spectral_helper() by using the ShortTimeFFT
      for use by csd().

    This function should be only used by _csd_test_shim() and is only useful
    for testing the ShortTimeFFT implementation.
    z%x and y cannot be broadcast together.Nr   r#   r   r   r   r   r   r    Fr&   r'   r$   r   )r   densityr(   )rW   rT   rV   r-   r"   .)r5   r2   rg   r/   r4   pop	broadcastr3   r.   r1   minr6   r   r   r8   r   r   r   spectrogramhopconjrb   r*   r?   )r@   r   rA   rB   rC   rD   rE   rF   rG   rJ   r"   	same_dataxouteryouter
outershapeeoutshapeemptyoutrN   rL   rM   r)   scaleSFTr   r`   s                             rY   r   r     s#    QIt9D 	

1AJJqM
 aggagg

4

4	Mbhhv&68HIOOJ 66Q;88AGG$bhhqww&77766Q;!&&A+!S!''$-)G%H$JJH{{288H#5r4@HX%%g,Q;ABB #AGGDM1774=(IA#FG!DLC|	IJJ4ya<x=7>??hE	qo M:#%H$7@E
sE2t %49C //!QW-=T7AhJ#87A:#  %%)TV  (,qA$dkk#vr*CA*2445:5kk#r6*55#:{  	MDE1L	Ms   AM 	M MM c                 :   t        | |||||||||	|
|      }t        d	i |\  }}}t        d	i |\  }}}d}t        ||d|        t        ||d|        t	        j
                  |j                        j                  dz  }t        |||d|        |||fS )
a  Assert that the results from the existing `stft()` and `_stft_wrapper()`
    are close to each other.

    For comparing the STFT values an absolute tolerance of the floating point
    resolution was added to circumvent problems with the following tests:
    * For float32 the tolerances are much higher in
      TestSTFT.test_roundtrip_float32()).
    * The TestSTFT.test_roundtrip_scaling() has a high relative deviation.
      Interestingly this did not appear in Scipy 1.9.1 but only in the current
      development version.
    )r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   r"   rJ   z* of `stft_wrapper()` differ from `stft()`.zFrequencies err_msgzTime slices r   zSTFT values )r   r    )dictr	   rZ   r   r2   r   r;   r   )r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   r"   rJ   kwr?   r:   ro   	f_wrapper	t_wrapperZxx_wrapper
e_msg_partr   s                        rY   stft_comparer   {  s     
b8/T7
LB 
r
IAq#(5(;(;%Iy+=JIqL*EFIqL*EF 88CII))A-DK4*:,79a9r[   c                 L   t        | |||||||||	|
      }t        di |\  }}|s||fS 	 t        di |\  }}\  }}d}t        ||d|        t        j                  |j                        j                  dz  }d	}|j                  t        j                  k(  rt        j                         d
k(  rd\  }}n"t        j                         dv rt        |d      }t        ||| ||| ||d|        ||fS # t        $ r"}|j                  d   dk(  r	||fcY d}~S |d}~ww xY w)a  Assert that the results from the existing `istft()` and
    `_istft_wrapper()` are close to each other.

    Quirks:
    * If ``boundary=False`` the comparison is skipped, since it does not
      make sense with ShortTimeFFT.istft(). Only used in test
      TestSTFT.test_roundtrip_boundary_extension().
    * If ShortTimeFFT.istft() decides the STFT is not invertible, the
      comparison is skipped, since istft() only emits a warning and does not
      return a correct result. Only used in
      ShortTimeFFT.test_roundtrip_not_nola().
    * For comparing the signals an absolute tolerance of the floating point
      resolution was added to account for the low accuracy of float32 (Occurs
      only in TestSTFT.test_roundtrip_float32()).
    )ro   rA   rB   rC   rD   rE   rp   rH   rq   rr   rJ   r   z,Short-time Fourier Transform not invertible!Nz+ of `istft_wrapper()` differ from `istft()`zSample times r   r   gHz>i686)g-C6?gh㈵>)aarch64i386r   g-q=zSignal values )r   rtolr   r   )r   r
   rx   r.   argsr   r2   r   r;   r   r<   platformmachiner   )ro   rA   rB   rC   rD   rE   rp   rH   rq   rr   rJ   r   r:   r@   r   	x_wrapperk_lorw   vr   r   r   s                         rY   istft_comparer     sK   $ 
#"VWd>9	
B
 ;2;DAq!t-;-Ab-A*	9ltT ?JAyM**FG 88AGG'')DD
 	ww"**!1!1!3v!=  
d				:	:4Id4(!D,T,ZL9;a4K1  66!9FFa4Ks#   C8 8	D#DD#DD#c                     t        | |||||||||	|
|      }t        di |\  }}t        di |\  }}t        ||       t        ||       t        ||       ||fS )zdAssert that the results from the existing `csd()` and `_csd_wrapper()`
    are close to each other. )r@   r   rA   rB   rC   rD   rE   rF   rG   rJ   r"   r   r   )r   r   r   r   )r@   r   rA   rB   rC   rD   rE   rF   rG   rJ   r"   r   r   freqs0Pxy0r   r   s                    rY   csd_comparer     ss    
 
Q2fgdG-wT
B 99LFD%"%LFDFF#D$FF#4<r[   )      ?hann   NNFTr   Tr#   r   )
r   r   NNNTTr#   r   )
r   r   NNNr   Tr   r#   r{   )	r   r   NNNr   Tr   r#   )%__doc__r   typingr   r   numpyr2   numpy.testingr   scipy.signalr   r   r   r	   r
   scipy.signal._arraytoolsr   r   r   r   scipy.signal._short_time_fftr   scipy.signal._spectral_pyr   r   r   rZ   rf   ndarrayr5   rx   r   r|   r   r   r   r   r   r[   rY   <module>r      s   &     ) % 5 5 K K 6  CG<@BLfR GKLN)3W0 	bjj"**eCHo56W0t FJ@D5;> HLBF+-2 KOEI.0[| BF;?AK> FJKM(24n EI?C4:r[   