o
    ?Hhk3                    @   sH  d dl Z d dlZd dlmZmZmZmZ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 d dlmZmZmZmZmZmZmZ d dlmZ d d	l m!Z! d d
l"m#Z$ d dl"m%Z& d dl"m'Z( G dd dZ)G dd dZ*G dd dZ+G dd dZ,G dd dZ-G dd dZ.G dd dZ/G dd dZ0dS )    N)assert_assert_allcloseassert_array_equalassert_equalassert_array_almost_equal_nulpsuppress_warnings)raises)signal)fftfreqrfftfreqfftirfft)	trapezoid)periodogramwelchlombscargle	coherencespectrogram
check_COLA
check_NOLA)hann)_spectral_helper)stft_compare)istft_compare)csd_comparec                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zejjejdkdddd Zejjejdkdddd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0S )1TestPeriodogramc                 C   sj   t d}d|d< t|\}}t|t ddd t d}d|d< |d  d  < |d }t|| d S )	N      r         ?	          @   npzerosr   r   linspaceonesselfxfpq r.   `/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/signal/tests/test_spectral.pytest_real_onesided_even   s   

z'TestPeriodogram.test_real_onesided_evenc                 C   s^   t d}d|d< t|\}}t|t dd  t d}d|d< |d9 }t||dd	 d S )
N   r   r          @      .@r"   ?V瞯<atol)r$   r%   r   r   aranger'   r(   r.   r.   r/   test_real_onesided_odd$   s   

z&TestPeriodogram.test_real_onesided_oddc                 C   sT   t d}d|d< t|dd\}}t|tdd t dd}d|d< t|| d S )Nr   r   r   Freturn_onesided      ?      ?)r$   r%   r   r   r
   fullr(   r.   r.   r/   test_real_twosided.   s   
z"TestPeriodogram.test_real_twosidedc                 C   sX   t d}d|d< t|dd\}}t|dd\}}t|t ddd t||d	  d S )
Nr   r   r   spectrumscalingdensityr   r   g      0@)r$   r%   r   r   r&   )r)   r*   r+   r,   gr-   r.   r.   r/   test_real_spectrum7   s   
z"TestPeriodogram.test_real_spectrumc                 C   sn   t jdtd}d|d< t|\}}t|t ddd t d}d|d< |d  d  < |d	 }t|| d S )
Nr   dtyper   r   r   r   r    r!   r"   )r$   r%   intr   r   r&   r'   r(   r.   r.   r/   test_integer_even?   s   
z!TestPeriodogram.test_integer_evenc                 C   sb   t jdtd}d|d< t|\}}t|t dd  t d}d|d< |d9 }t||d	d
 d S )Nr1   rF   r   r   r2   r3   r"   r4   r5   r6   )r$   r%   rH   r   r   r8   r'   r(   r.   r.   r/   test_integer_oddJ   s   
z TestPeriodogram.test_integer_oddc                 C   sX   t jdtd}d|d< t|dd\}}t|tdd t dd}d|d< t|| d S )	Nr   rF   r   r   Fr:   r<   r=   )r$   r%   rH   r   r   r
   r>   r(   r.   r.   r/   test_integer_twosidedT      z%TestPeriodogram.test_integer_twosidedc                 C   sX   t dt j}d|d< t|dd\}}t|tdd t dd}d|d< t|| d S )Nr         ?       @r   Fr:   r<         ?)r$   r%   
complex128r   r   r
   r>   r(   r.   r.   r/   test_complex]   rL   zTestPeriodogram.test_complexc                 C   s   t tttdtjdd d S )N   foorA   )assert_raises
ValueErrorr   r$   r%   rO   r)   r.   r.   r/   test_unk_scalingf   s   
z TestPeriodogram.test_unk_scalingl        zOn some 32-bit tolerance issue)reasonc                 C   s   t jdt jd}|d}d|d d d d df< t|\}}t|jd t|ddd d f |ddd d f d t|ddd d f \}}t|t jd d f |dd d f d d S )	N   rF      r   
   r<   r   rZ   r      r   <   )	r$   r%   float64reshaper   r   shaper   newaxisr)   r*   r+   r,   f0p0r.   r.   r/   test_nd_axis_m1j   s   
(*zTestPeriodogram.test_nd_axis_m1c                 C   s   t jdt jd}|d}d|dd d d d f< t|dd\}}t|jd t|d d ddf |d d ddf d	 t|d d ddf \}}t||d d ddf  d S )
NrX   rF   r[   rZ   r   r<   r   axisr]   rZ   r   r   r^   )r$   r%   r_   r`   r   r   ra   r   rc   r.   r.   r/   test_nd_axis_0x   s   
(zTestPeriodogram.test_nd_axis_0c                 C   sr   t d}d|d< t|dd\}}tdd}t|d|\}}t|| t|| tdd}ttt|d| d S )Nr   r   r   r[   r       )r$   r%   r   r	   
get_windowr   rS   rT   r)   r*   r+   r,   winfepewin_errr.   r.   r/   test_window_external   s   


z$TestPeriodogram.test_window_externalc                 C   sf   t d}d|d< t|\}}t|dd\}}t||d d d  t||d d d  t|jd d S )Nr   r   r   rl   nfftrZ   )   )r$   r%   r   r   r   ra   )r)   r*   r+   r,   fpppr.   r.   r/   test_padded_fft   s   
zTestPeriodogram.test_padded_fftc                 C   \   t g \}}t|jd t|jd dD ]}t t|\}}t|j| t|j| qd S Nr   r|      r   r      rZ   )r   r   ra   r$   emptyr)   r+   r,   ra   r.   r.   r/   test_empty_input      z TestPeriodogram.test_empty_inputc                 C   <   dD ]}t t|dd\}}t|j| t|j| qd S Nr~   r   r   rh   )r   r$   r   r   ra   r)   ra   r+   r,   r.   r.   r/   test_empty_input_other_axis   
   z+TestPeriodogram.test_empty_input_other_axisc                 C   sn   t d}d|d< t|dd\}}t|t ddd t d}d|d< |d  d	  < |d
 }t|| d S )N   r   r   r   rt   r   r   r    r!   r"   r#   r(   r.   r.   r/   test_short_nfft      

zTestPeriodogram.test_short_nfftc                 C   sn   t d}d|d< t|dd\}}t|t ddd t d}d|d< |d  d  < |d	 }t|| d S )
Nr   r   r   rt   r   r   r    r!   r"   r#   r(   r.   r.   r/   test_nfft_is_xshape   r   z#TestPeriodogram.test_nfft_is_xshapec                 C   s~   t dd}d|d< t|\}}t|t ddd t dd}d|d< |d  d  < |d	 }t|| t|j|jk d S )
Nr   r+   r   r   r   r   r    r!   r"   )r$   r%   r   r   r&   r'   r   rG   r(   r.   r.   r/   test_real_onesided_even_32   s   
z*TestPeriodogram.test_real_onesided_even_32c                 C   sr   t dd}d|d< t|\}}t|t dd  t dd}d|d< |d9 }t||d	d
 t|j|jk d S )Nr1   r+   r   r   r2   r3   r"   r4   Hz>r6   )r$   r%   r   r   r8   r'   r   rG   r(   r.   r.   r/   test_real_onesided_odd_32   s   z)TestPeriodogram.test_real_onesided_odd_32c                 C   sh   t dd}d|d< t|dd\}}t|tdd t ddd}d|d< t|| t|j|jk d S )	Nr   r+   r   r   Fr:   r<   r=   r$   r%   r   r   r
   r>   r   rG   r(   r.   r.   r/   test_real_twosided_32      
z%TestPeriodogram.test_real_twosided_32c                 C   sh   t dd}d|d< t|dd\}}t|tdd t ddd	}d|d< t|| t|j|jk d S )
Nr   FrM   r   Fr:   r<   rN   r+   r   r(   r.   r.   r/   test_complex_32   r   zTestPeriodogram.test_complex_32c                 C   s`   t d}d|d< tdd}d}tt|d t||d W d    d S 1 s)w   Y  d S )	Nr   r   r   r   r[   zOthe size of the window must be the same size of the input on the specified axismatchwindow)r$   r%   r	   rm   rS   rT   r   )r)   r*   ro   expected_msgr.   r.   r/   test_shorter_window_error   s   
"z)TestPeriodogram.test_shorter_window_errorN)__name__
__module____qualname__r0   r9   r?   rE   rI   rJ   rK   rP   rV   pytestmarkskipifsysmaxsizerf   rk   rs   ry   r   r   r   r   r   r   r   r   r   r.   r.   r.   r/   r      s>    
	
		



		

r   c                   @     e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAS )B	TestWelchc                 C   s`   t d}d|d< d|d< t|dd\}}t|t ddd t g d}t||d	d	d
 d S Nr   r   r   r"   npersegr   r   iKGUU?gS8?#q?r   #q?r   r7   rtolr$   r%   r   r   r&   arrayr(   r.   r.   r/   r0         
z!TestWelch.test_real_onesided_evenc                 C   s`   t d}d|d< d|d< t|dd\}}t|t dd  t g d	}t||d
d
d d S Nr   r   r   r"   r   r         @      "@g9?g?p0?r   r   r   r   )r$   r%   r   r   r8   r   r(   r.   r.   r/   r9      r   z TestWelch.test_real_onesided_oddc                 C   s^   t d}d|d< d|d< t|ddd\}}t|tdd t g d}t||d	d	d
 d S Nr   r   r   r"   Fr   r;   r<   r   S8?r   r   r   r   r   r   r   r   )r$   r%   r   r   r
   r   r(   r.   r.   r/   r?   
  s   
zTestWelch.test_real_twosidedc                 C   sb   t d}d|d< d|d< t|ddd\}}t|t ddd t g d	}t||d
d
d d S Nr   r   r   r"   r@   )r   rB   r   r   )g      ?g-UU?.iqUU?r   g-UU?r   r   r   r(   r.   r.   r/   rE        
zTestWelch.test_real_spectrumc                 C   sd   t jdtd}d|d< d|d< t|dd\}}t|t ddd t g d	}t||d
d
d d S Nr   rF   r   r   r"   r   r   r   r   r   r   )r$   r%   rH   r   r   r&   r   r(   r.   r.   r/   test_integer_onesided_even     z$TestWelch.test_integer_onesided_evenc                 C   sd   t jdtd}d|d< d|d< t|dd\}}t|t dd	  t g d
}t||ddd d S Nr   rF   r   r   r"   r   r   r   r   r   r   r   )r$   r%   rH   r   r   r8   r   r(   r.   r.   r/   test_integer_onesided_odd(  r   z#TestWelch.test_integer_onesided_oddc                 C   sb   t jdtd}d|d< d|d< t|ddd\}}t|tdd t g d	}t||d
d
d d S Nr   rF   r   r   r"   Fr   r<   r   r   r   )r$   r%   rH   r   r   r
   r   r(   r.   r.   r/   rK   2     zTestWelch.test_integer_twosidedc                 C   sb   t dt j}d|d< d|d< t|ddd\}}t|tdd t g d}t||d	d	d
 d S Nr   rM   r   r"   Fr   r<   )g&>?#q?n)t?r   r   r   r   r   r   r   )r$   r%   rO   r   r   r
   r   r(   r.   r.   r/   rP   <  r   zTestWelch.test_complexc                 C   s    t tttdtjddd d S NrQ   rR   )rB   r   )rS   rT   r   r$   r%   rO   rU   r.   r.   r/   rV   F  s   
zTestWelch.test_unk_scalingc                 C   s>   t jdt jdd }t|ddd\}}t|t |dd d S Nr[   rF   {Gz?linearr   detrendr5   r6   r$   r8   r_   r   r   
zeros_liker)   r*   r+   r,   r.   r.   r/   test_detrend_linearJ  s   zTestWelch.test_detrend_linearc                 C   s\   t jdt jdd }t|ddd\}}t|ddd d\}}t||dd	 t||dd	 d S )
Nr[   rF   r   Fr   c                 S      | S Nr.   r*   r.   r.   r/   <lambda>R      z.TestWelch.test_no_detrending.<locals>.<lambda>r5   r6   )r$   r8   r_   r   r   r)   r*   f1p1f2p2r.   r.   r/   test_no_detrendingO  s
   zTestWelch.test_no_detrendingc                 C   sB   t jdt jdd }t|ddd d\}}t|t |dd d S )	Nr[   rF   r   c                 S      t j| ddS Nl)typer	   r   segr.   r.   r/   r   Y      z1TestWelch.test_detrend_external.<locals>.<lambda>r   r5   r6   r   r   r.   r.   r/   test_detrend_externalV  s
   
zTestWelch.test_detrend_externalc                 C   sL   t jdt jdd }|d}t|ddd d\}}t|t |d	d
 d S )N(   rF   r   rZ   rZ   r[   r[   c                 S   r   r   r   r   r.   r.   r/   r   `  r   z7TestWelch.test_detrend_external_nd_m1.<locals>.<lambda>r   r5   r6   )r$   r8   r_   r`   r   r   r   r   r.   r.   r/   test_detrend_external_nd_m1\  s   

z%TestWelch.test_detrend_external_nd_m1c                 C   s\   t jdt jdd }|d}t |dd}t|dddd	 d
\}}t|t |dd d S )NrX   rF   r   rY   rZ   r   r[   c                 S      t j| dddS Nr   r   )ri   r   r   r   r.   r.   r/   r   h      z6TestWelch.test_detrend_external_nd_0.<locals>.<lambda>r   ri   r   r5   r6   )r$   r8   r_   r`   moveaxisr   r   r   r   r.   r.   r/   test_detrend_external_nd_0c  s   

z$TestWelch.test_detrend_external_nd_0c                 C   s   t jdt jdd }|d}t|dd\}}t|jd t|ddd d f |d	dd d f d
d
d t|ddd d f dd\}}t|t jd d f |d	d d f d
d
d d S NrX   rF   r   rY   r[   r   r\   r   r   vIh%<=r   )	r$   r8   r_   r`   r   r   ra   r   rb   rc   r.   r.   r/   rf   k  s   
,.zTestWelch.test_nd_axis_m1c                 C   s   t jdt jdd }|d}t|ddd\}}t|jd t|d d ddf |d d d	df d
d
d t|d d ddf dd\}}t||d d d	df d
d
d d S NrX   rF   r   rg   r[   r   )r   ri   rj   r   r   r   r   )r$   r8   r_   r`   r   r   ra   r   rc   r.   r.   r/   rk   t  s   
,"zTestWelch.test_nd_axis_0c                 C   s   t d}d|d< d|d< t|dddd\}}tdd}t|d|d d\}}t|| t|| t|jd t|jd tt	t|d|d	d tdd
}tt	t|d|d d d S )Nr   r   r   r"   r[   r   r   r   rQ   rl   )
r$   r%   r   r	   rm   r   r   ra   rS   rT   rn   r.   r.   r/   rs   }  s"   



zTestWelch.test_window_externalc                 C   rz   r{   )r   r   ra   r$   r   r   r.   r.   r/   r     r   zTestWelch.test_empty_inputc                 C   r   r   )r   r$   r   r   ra   r   r.   r.   r/   r     r   z%TestWelch.test_empty_input_other_axisc           
      C   s   t d}d|d< t !}d}|t| t|dd\}}t|ddd\}}W d    n1 s0w   Y  t|dd	\}}	t|| t||	 t|| t||	 d S 
Nr"   r   r   zBnperseg = 256 is greater than input length  = 8, using nperseg = 8r   r      r   r   r   )r$   r%   r   filterUserWarningr   r   
r)   r*   supmsgr+   r,   r   r   r   r   r.   r.   r/   test_short_data  s   



zTestWelch.test_short_datac              	   C   sF   t tttddtg d t tttddtdd d S NrQ   r   )r   r   r   r   r   r]   )rZ   r   )rS   rT   r   r$   r%   r   r8   r`   rU   r.   r.   r/   test_window_long_or_nd  s    z TestWelch.test_window_long_or_ndc                 C   sJ   t d}d|d d d< t|ddd\}}t g d}t||dd	 d S 
N@   r   r"   r   rQ   r   noverlap)	r   gUUUUUU?UUUUUU?皙?r  r  r  r  gUUUUUU?g-q=r6   )r$   r%   r   r   r   r(   r.   r.   r/   test_nondefault_noverlap  s
   
z"TestWelch.test_nondefault_noverlapc                 C   s   t tttddddd d S NrQ   r   r   rZ      )rS   rT   r   r$   r%   rU   r.   r.   r/   test_bad_noverlap  s   zTestWelch.test_bad_noverlapc                 C   s   t tttdddd d S N   r   rQ   )ru   r   )rS   rT   r   r$   r'   rU   r.   r.   r/   test_nfft_too_short  s   zTestWelch.test_nfft_too_shortc                 C   st   t dd}d|d< d|d< t|dd\}}t|t ddd t g d	d}t||d
d
d t|j|jk d S Nr   r+   r   r   r"   r   r   r   r   r   r   )r$   r%   r   r   r&   r   r   rG   r(   r.   r.   r/   r        
z$TestWelch.test_real_onesided_even_32c                 C   st   t dd}d|d< d|d< t|dd\}}t|t dd	  t g d
d}t||ddd t|j|jk d S Nr   r+   r   r   r"   r   r   r   r   )gx:?gyѲI?r   gw0?r   r   r   )r$   r%   r   r   r8   r   r   rG   r(   r.   r.   r/   r     r  z#TestWelch.test_real_onesided_odd_32c                 C   sr   t dd}d|d< d|d< t|ddd\}}t|tdd t g d	d}t||d
d
d t|j|jk d S Nr   r+   r   r   r"   Fr   r<   r   r   r   r$   r%   r   r   r
   r   r   rG   r(   r.   r.   r/   r     s   
zTestWelch.test_real_twosided_32c                 C   s   t dd}d|d< d|d< t|ddd\}}t|tdd t g d	d
}t||ddd t|j|jkd|j d|j  d S Nr   r   rM   r   r"   Fr   r<   )g%?A2q?d^?r  g~?r  r  r  r+   r   r   zdtype mismatch, , r  r(   r.   r.   r/   r     s   
zTestWelch.test_complex_32c                 C   s   t d}d}t|dd |d d  }|d  d9  < t|d|d\}}t|d	|d\}}t|| t|| d
}t|dd |d d  }t|d|d\}}t|d	|d\}}t|| t|| d S Nr     r<   rZ   r   r    r   r   ru   r]      )r$   r%   r
   r   r   )r)   r*   ru   r+   fodd_fevenr.   r.   r/   test_padded_freqs  s   



zTestWelch.test_padded_freqsc                 C   s   d}d}t |d }d}t || | }t|| }|tdtj | |  }dD ]7}t||||dd\}	}
t||||d	d\}}t|
| |d d
  ttt|||td d dd q+d S )NrX   g     @r[   ,  rZ   )r   bartlett)tukey皙?flattopr@   )fsr   r   rB   rC   r!   gMbP?r   )	rH   r$   r8   sinpir   r   sqrtr   )r)   Ar#  r   fsigiittr*   r   r  p_specfreqp_densr.   r.   r/   test_window_correction   s&   



 z TestWelch.test_window_correctionc           	      C   s   t jd t jd}t|\}}tdD ]8}dgd }d||< ||}t||d\}}t|||j d\}}t||	 |d t||	 ||j d qd S )N     r   r   r    rh   err_msg)
r$   randomseedrandnr   ranger`   ndimr   squeeze)	r)   x_flatr  p_flatanewshaper*   p_plusp_minusr.   r.   r/   test_axis_rolling  s   

zTestWelch.test_axis_rollingc                 C   st   t d}d|d< d|d< t|ddd\}}t|t ddd t g d	}t||d
d
d ttt|ddd d S )Nr   r   r   r"   median)r   averager   r   )r!  皙?        g&E    9rD  r   r   zunrecognised-average)r$   r%   r   r   r&   r   rS   rT   r(   r.   r.   r/   test_average'  s   


zTestWelch.test_averageN)#r   r   r   r0   r9   r?   rE   r   r   rK   rP   rV   r   r   r   r   r   rf   rk   rs   r   r   r   r   r  r	  r  r   r   r   r   r  r/  r@  rE  r.   r.   r.   r/   r      sB    







			r   c                   @   r   )BTestCSDc                 C   s\   t d}t d}t ddd}t jdt jd}t||dd\}}t|| t|| d S )Nr"   r  r   r   r  rF   r   r$   r%   r&   rO   csdr   r)   r*   yr+   cr   c1r.   r.   r/   test_pad_shorter_x5     


zTestCSD.test_pad_shorter_xc                 C   s\   t d}t d}t ddd}t jdt jd}t||dd\}}t|| t|| d S )Nr  r"   r   r   r  rF   r   rG  rI  r.   r.   r/   test_pad_shorter_y@  rN  zTestCSD.test_pad_shorter_yc                 C   sb   t d}d|d< d|d< t||dd\}}t|t ddd t g d}t||d	d	d
 d S r   r$   r%   rH  r   r&   r   r(   r.   r.   r/   r0   K  r   zTestCSD.test_real_onesided_evenc                 C   sb   t d}d|d< d|d< t||dd\}}t|t dd  t g d	}t||d
d
d d S r   )r$   r%   rH  r   r8   r   r(   r.   r.   r/   r9   U  r   zTestCSD.test_real_onesided_oddc                 C   s`   t d}d|d< d|d< t||ddd\}}t|tdd t g d}t||d	d	d
 d S r   )r$   r%   rH  r   r
   r   r(   r.   r.   r/   r?   _  s   
zTestCSD.test_real_twosidedc                 C   sd   t d}d|d< d|d< t||ddd\}}t|t ddd t g d	}t||d
d
d d S r   rP  r(   r.   r.   r/   rE   i  s   
zTestCSD.test_real_spectrumc                 C   sf   t jdtd}d|d< d|d< t||dd\}}t|t ddd t g d	}t||d
d
d d S r   )r$   r%   rH   rH  r   r&   r   r(   r.   r.   r/   r   s     z"TestCSD.test_integer_onesided_evenc                 C   sf   t jdtd}d|d< d|d< t||dd\}}t|t dd	  t g d
}t||ddd d S r   )r$   r%   rH   rH  r   r8   r   r(   r.   r.   r/   r   }  rQ  z!TestCSD.test_integer_onesided_oddc                 C   sd   t jdtd}d|d< d|d< t||ddd\}}t|tdd t g d	}t||d
d
d d S r   )r$   r%   rH   rH  r   r
   r   r(   r.   r.   r/   rK        zTestCSD.test_integer_twosidedc                 C   sd   t dt j}d|d< d|d< t||ddd\}}t|tdd t g d}t||d	d	d
 d S r   )r$   r%   rO   rH  r   r
   r   r(   r.   r.   r/   rP     rR  zTestCSD.test_complexc                 C   s,   t tttdtjtdtjddd d S r   )rS   rT   rH  r$   r%   rO   r'   rU   r.   r.   r/   rV     s   
zTestCSD.test_unk_scalingc                 C   s@   t jdt jdd }t||ddd\}}t|t |dd d S r   r$   r8   r_   rH  r   r   r   r.   r.   r/   r     s   zTestCSD.test_detrend_linearc                 C   s`   t jdt jdd }t||ddd\}}t||ddd d\}}t||dd	 t||dd	 d S )
Nr[   rF   r   Fr   c                 S   r   r   r.   r   r.   r.   r/   r     r   z,TestCSD.test_no_detrending.<locals>.<lambda>r5   r6   )r$   r8   r_   rH  r   r   r.   r.   r/   r     s
   zTestCSD.test_no_detrendingc                 C   sD   t jdt jdd }t||ddd d\}}t|t |dd d S )	Nr[   rF   r   c                 S   r   r   r   r   r.   r.   r/   r     r   z/TestCSD.test_detrend_external.<locals>.<lambda>r   r5   r6   rS  r   r.   r.   r/   r     s
   
zTestCSD.test_detrend_externalc                 C   sN   t jdt jdd }|d}t||ddd d\}}t|t |d	d
 d S )Nr   rF   r   r   r[   c                 S   r   r   r   r   r.   r.   r/   r     r   z5TestCSD.test_detrend_external_nd_m1.<locals>.<lambda>r   r5   r6   )r$   r8   r_   r`   rH  r   r   r   r.   r.   r/   r     s   

z#TestCSD.test_detrend_external_nd_m1c                 C   s^   t jdt jdd }|d}t |dd}t||dddd	 d
\}}t|t |dd d S )NrX   rF   r   rY   rZ   r   r[   c                 S   r   r   r   r   r.   r.   r/   r     r   z4TestCSD.test_detrend_external_nd_0.<locals>.<lambda>r   r5   r6   )r$   r8   r_   r`   r   rH  r   r   r   r.   r.   r/   r     s   


z"TestCSD.test_detrend_external_nd_0c                 C   s   t jdt jdd }|d}t||dd\}}t|jd t|ddd d f |d	dd d f d
d
d t|ddd d f |ddd d f dd\}}t|t jd d f |d	d d f d
d
d d S r   )	r$   r8   r_   r`   rH  r   ra   r   rb   rc   r.   r.   r/   rf     s   
,..zTestCSD.test_nd_axis_m1c                 C   s   t jdt jdd }|d}t||ddd\}}t|jd t|d d ddf |d d d	df d
d
d t|d d ddf |d d ddf dd\}}t||d d d	df d
d
d d S r   )r$   r8   r_   r`   rH  r   ra   r   rc   r.   r.   r/   rk     s   
,."zTestCSD.test_nd_axis_0c              	   C   s   t d}d|d< d|d< t||ddd\}}tdd}t||d|d d\}}t|| t|| t|jd t|jd tt	t||d|d	d tdd
}tt	t||d|d d d S )Nr   r   r   r"   r[   r   r   r   r   rl   )
r$   r%   rH  r	   rm   r   r   ra   rS   rT   rn   r.   r.   r/   rs     s"   





zTestCSD.test_window_externalc                 C   s   t g td\}}t|jd t|jd t tdg \}}t|jd t|jd dD ]}t t|t|\}}t|j| t|j| q.t tdtd\}}t|jd t|jd t tdtd\}}t|jd t|jd d S )Nr[   r|   r}   )r   r   )rH  r$   r%   r   ra   r   r'   r   r.   r.   r/   r     s    zTestCSD.test_empty_inputc                 C   s   dD ]}t t|t|dd\}}t|j| t|j| qt tdtddd\}}t|jd t|jd t tdtddd\}}t|jd t|jd d S )Nr   r   rh   )r[   r[   r   )r[   r   r   )r[   r   r   )rH  r$   r   r   ra   r%   r   r.   r.   r/   r     s   z#TestCSD.test_empty_input_other_axisc           
      C   s   t d}d|d< t #}d}|t| t||dd\}}t||ddd\}}W d    n1 s2w   Y  t||dd	\}}	t|| t||	 t|| t||	 d S r   )r$   r%   r   r   r   rH  r   r   r.   r.   r/   r     s   



zTestCSD.test_short_datac              
   C   sV   t tttdtddtg d t tttdtddtdd d S r   )	rS   rT   rH  r$   r%   r'   r   r8   r`   rU   r.   r.   r/   r     s   zTestCSD.test_window_long_or_ndc                 C   sL   t d}d|d d d< t||ddd\}}t g d}t||dd	 d S r   )r$   r%   rH  r   r   r(   r.   r.   r/   r    s
   
z TestCSD.test_nondefault_noverlapc              	   C   s&   t tttdtddddd d S r  )rS   rT   rH  r$   r%   r'   rU   r.   r.   r/   r	  &  s   zTestCSD.test_bad_noverlapc                 C   s$   t tttdtdddd d S r
  )rS   rT   rH  r$   r'   r%   rU   r.   r.   r/   r  *  s   
zTestCSD.test_nfft_too_shortc                 C   sv   t dd}d|d< d|d< t||dd\}}t|t ddd t g d	d}t||d
d
d t|j|jk d S r  )r$   r%   rH  r   r&   r   r   rG   r(   r.   r.   r/   r   .     
z"TestCSD.test_real_onesided_even_32c                 C   sv   t dd}d|d< d|d< t||dd\}}t|t dd	  t g d
d}t||ddd t|j|jk d S r  )r$   r%   rH  r   r8   r   r   rG   r(   r.   r.   r/   r   9  rT  z!TestCSD.test_real_onesided_odd_32c                 C   st   t dd}d|d< d|d< t||ddd\}}t|tdd t g d	d}t||d
d
d t|j|jk d S r  r$   r%   rH  r   r
   r   r   rG   r(   r.   r.   r/   r   D  s   
zTestCSD.test_real_twosided_32c                 C   s   t dd}d|d< d|d< t||ddd\}}t|tdd t g d	d
}t||ddd t|j|jkd|j d|j  d S r  rU  r(   r.   r.   r/   r   P  s   
zTestCSD.test_complex_32c                 C   s   t d}t d}d}t|dd |d d  }|d  d9  < t||d|d\}}t||d	|d\}}t|| t|| d
}t|dd |d d  }t||d|d\}}t||d	|d\}}t|| t|| d S r  )r$   r%   r'   r
   rH  r   )r)   r*   rJ  ru   r+   r  r  r  r.   r.   r/   r  \  s   




zTestCSD.test_padded_freqsc                 C   s   t jd}| }t||dddd\}}t||dddd\}}t|| t||dddd\}}t||dddd\}}t|| d S )Nr  r"   meanF)r   rB  r;   rA  )r$   r4  r6  copyrH  r   )r)   r*   rJ  r  p_samep_copiedr.   r.   r/   test_copied_datao  s    








zTestCSD.test_copied_dataN)#r   r   r   rM  rO  r0   r9   r?   rE   r   r   rK   rP   rV   r   r   r   r   r   rf   rk   rs   r   r   r   r   r  r	  r  r   r   r   r   r  rZ  r.   r.   r.   r/   rF  4  sB    







		rF  c                   @   s   e Zd Zdd Zdd ZdS )TestCoherencec                 C   sX   t jd}t |}t ddd}t d}t||dd\}}t|| t|| d S NrX   r   r   r]   r[   r   )r$   r4  r6  rW  r&   r'   r   r   r)   r*   rJ  r+   Cr   C1r.   r.   r/   test_identical_input  s   


z"TestCoherence.test_identical_inputc                 C   sT   t jd}| }t ddd}t d}t||dd\}}t|| t|| d S r\  )r$   r4  r6  r&   r'   r   r   r]  r.   r.   r/   test_phase_shifted_input  s   

z&TestCoherence.test_phase_shifted_inputN)r   r   r   r`  ra  r.   r.   r.   r/   r[    s    r[  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestSpectrogramc                 C   sh   t jd}d}d}d}d}t|||||\}}}t|||||\}	}
t||	 tt j|dd|
 d S )Nr1  r<   r   g      ?r   rZ   r    rh   )r$   r4  r6  r   r   r   rV  )r)   r*   r#  r   r   r  r+   r  PfwPwr.   r.   r/   test_average_all_segments  s   
z)TestSpectrogram.test_average_all_segmentsc                 C   s   t jd}d}d}d}d}t|||||\}}}tdd}	t|||	d dd\}
}}t|
jd t|jd tt	t|||	d	d
 tdd}tt	t|||d d
 d S )Nr1  r<   rc  r   rZ   r  )r   )r   I   r"   r   i   )
r$   r4  r6  r   r	   rm   r   ra   rS   rT   )r)   r*   r#  r   r   r  r+   r  rd  ro   rp   Perr   r.   r.   r/   rs     s"   
z$TestSpectrogram.test_window_externalc                 C   s   t jd}d}t||dd\}}}t }|td t||ddd\}}}W d    n1 s1w   Y  t||dd	\}	}}
t||dd	\}}}t||	 t||
 t|| t|| d S )
Nr1  r<   rc  r   zInperseg = 1025 is greater than input length  = 1024, using nperseg = 1024i  r   r   r   )r$   r4  r6  r   r   r   r   r   )r)   r*   r#  r+   r  r,   r   r   r   r   r   f3p3r.   r.   r/   r     s"   


zTestSpectrogram.test_short_dataN)r   r   r   rg  rs   r   r.   r.   r.   r/   rb    s    rb  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!S )"TestLombscarglec                 C   s  d}d}dt j }d}d}d}t jd}||}t dt j d	t j |||k }	|t ||	 |  }
t dd	|}t|	|
|}|d
 |d  }||t |  |d k sZJ t|	|
|t j	|	|j
dd}|d
 |d  }||t |  |d k sJ dS )zgTest if frequency location of peak corresponds to frequency of
        generated input signal.
        r!   r<   r   d     ffffff?# {Gz?      $@r   r   rF   weightsN)r$   r&  r4  RandomStaterandr&   r%  r   argmax	ones_likerG   )r)   amplwphininnoutr,   rngrtrJ  r+   rd  deltar.   r.   r/   test_frequency  s"   

""zTestLombscargle.test_frequencyc                 C   s   d}d}dt j }d}d}d}t jd}||}t dt j dt j |||k }	|t ||	 |  }
t dd|}t|	|
|}t d	| |	j	d
  }t
|||k |dd d S )Nr!   r<   r   rn  ro  rp  rq  rr  g      @r   rC  r$  )r$   r&  r4  ru  rv  r&   r%  r   r'  ra   r   )r)   ry  rz  r{  r|  r}  r,   r~  r  r  rJ  r+   pgramr.   r.   r/   test_amplitude  s   

"zTestLombscargle.test_amplitudec                 C   s   d}d}dt j }d}d}d}d}t jd}||}	t d	t j d
t j ||	|k }
|t ||
 |  | }t d	d
|}t|
||dd}t|
||  |dd}t	|| t|
||ddd}t|
||  |ddd}t	|| d S )Nr!   r<   r   rm  rn  ro  g333333?rp  rq  rr  T)	precenterF)r  floating_mean)
r$   r&  r4  ru  rv  r&   r%  r   rV  r   )r)   ry  rz  r{  r|  r}  r,   offsetr~  r  r  rJ  r+   r  pgram2r.   r.   r/   test_precenter  s$   

"
zTestLombscargle.test_precenterc                 C   s   d}d}dt j }d}d}d}t jd}||}t dt j d	t j |||k }	|t ||	 |  }
t dd	|}t|	|
|}t|	|
|d
d}t |	t	|	j
d  }||
 |
  }|}d||	j
d   }t|| | tt |d d S )Nr!   r<   r   rm  rn  ro  rp  rq  rr  T	normalizer   rZ   )r$   r&  r4  ru  rv  r&   r%  r   rx  floatra   sumr   max)r)   ry  rz  r{  r|  r}  r,   r~  r  r  rJ  r+   r  r  rt  YY_hatYYscale_to_user.   r.   r/   test_normalize@  s&   

"zTestLombscargle.test_normalizec                 C   s  t ddd}t ddd}t dddd }ttt||| t jt t ddddddd}t ddd}t dddd }ttt||| t ddd}t jt t ddddddd}t dddd }ttt||| t ddd}t ddd}t jt t ddddd ddd}ttt||| t ddd}t ddd}t dddd }t jt t ddddddd}ttt||||d d S )Nr   r   rZ   r   r!  rh   rs  )r$   r&   rS   rT   r   repeatexpand_dimsr)   r  rJ  r+   rt  r.   r.   r/   test_wrong_shaped  s*   ""&"z TestLombscargle.test_wrong_shapec                 C   sP   t jddddd}t d| }t jdddddd	 }t|||d
 t j  d S )Nr   r[   rn  F)endpointrQ   2   i  r!  rZ   )r$   r&   r%  r   r&  )r)   r  rJ  r+   r.   r.   r/   test_lombscargle_atan_vs_atan2  s   z.TestLombscargle.test_lombscargle_atan_vs_atan2c                 C   sT   t ddd}t ddd}t dddd }t ddd}ttt||||d d S )Nr   r   r   r!  rZ   rs  )r$   r&   rS   rT   r   r  r.   r.   r/   test_wrong_shape_weights  s
   z(TestLombscargle.test_wrong_shape_weightsc                 C   s@   t d}t d}t d}t d}ttt||||d d S Nr   rs  r$   r%   r'   rS   rT   r   r  r.   r.   r/   test_zero_division_weights  s
   



z*TestLombscargle.test_zero_division_weightsc                 C   sz  d}d}d}d}d}d}t jd}||}t dt j d	t j |||k }	|t ||	 |  }
t dd	|}t|	|
|d
d}t|	|
|dd}t|	|
|dd}t|	|
|dd}t|	|
|dd}t|| t|| t 	|
t
|
jd  }||
 |
  }|}t|d t
|	jd |  | t ||kd d }tt || | ttt|	|
|dd ttt|	|
|dd d S )Nr!   r<   r   rm  rn  ro  rp  rq  rr  Fr  Tpowerr  	amplitudelombrZ   )r$   r4  ru  rv  r&   r&  r%  r   r   rx  r  ra   r  whereabsrS   rT   )r)   ry  rz  r{  r|  r}  r,   r~  r  r  rJ  r+   pgram_false
pgram_truepgram_power
pgram_norm	pgram_amprt  r  r  f_ir.   r.   r/   test_normalize_parameter  s4   
"

 z(TestLombscargle.test_normalize_parameterc                 C   s   d}d}dt j }d}d}d}d}t jd}||}	t d	t j d
t j ||	|k }
|t ||
 |  }t d	d
|}t|
||dd}t|
|| |dd}t|| d S )Nr!   r<   r   rm  rn  ro  333333@rp  rq  rr  T)r  )	r$   r&  r4  ru  rv  r&   r%  r   r   r)   ry  rz  r{  r|  r}  r,   r  r~  r  r  rJ  r+   r  pgram_offsetr.   r.   r/   test_offset_removal  s   

"z#TestLombscargle.test_offset_removalc                 C   s   d}d}d}d}d}d}d}t jd}||}	t dt j d	t j ||	|k }
|t ||
 |  }t dd	|}t|
||d
dd}t|
|| |d
dd}|d dk sXJ |d dks`J d S )Nr!   r<   r   rn  ro  rZ   rp  rq  rr  TFr  r  r   r$   r4  ru  rv  r&   r&  cosr   r  r.   r.   r/   test_floating_mean_false  s$   
"z(TestLombscargle.test_floating_mean_falsec                 C   s   d}d}d}d}d}d}d}t jd}||}	t d	t j d
t j ||	|k }
|t ||
 |  | }t d	d
|}t ||kd d }t|
||ddd}t	t 
|| | t	t ||  | d S )Nr!   r<   Q?rm  rn  ro  r  rp  rq  rr  r   r  Tr  )r$   r4  ru  rv  r&   r&  r  r  r   r   r  angle)r)   ry  rz  r{  r|  r}  r,   r  r~  r  r  rJ  r+   f_indxr  r.   r.   r/   test_amplitude_is_correct  s    
"z)TestLombscargle.test_amplitude_is_correctc                 C   sB   t d}t d}t d}t d }ttt||||d d S r  r  r  r.   r.   r/   test_negative_weight5  s
   


z$TestLombscargle.test_negative_weightc                 C   sB   g d}g d}t ddd}dt j | }t|||ddd d S )	N)yg   ˈAg   ˈAg   ̈Ag   ͈Ag   ΈAg   ψAg   ЈAg   шAg   ҈Ag   ӈAg   ԈAg   ՈAg   ֈAg   ׈Ag   ؈Ag   وAg   ڈAg   ۈAg   ܈Ag   ݈Ag   Ag   Ag   Ag   Ag   Ag    Ag   Ag   Ag   Ag   Ag   Ag   Ag   Ag   Ag   Ag   Ag   Ag    Ag   Ag   Ag   "Ag   Ag   Ag   Ag   
Ag   Ag   Ag   Ag    Ag   !Ag   "Ag   #Ag   $Ag   %Ag   &Ag   'Ag   (Ag   )Ag   *Ag   +Ag   ZcAg   TdAg   NeAg   HfAg   BgAg   <hAg   6iAg   0jAg   *kAg   $lAg   mAg   nAg   oAg   pAg   qAg    rAg   rAg   sAg   tAg   uAg   wAg   xAg   yAg   zAg   {Ag   |Ag   }Ag   ~Ag   Ag   Ag   Ag   Ag   Ag   Ag   Ag   Ag   |Ag   vAg   pAg   jAg   Ag   
Ag   Ag   Ag   Ag   Ag   잉Ag   柉Ag   ࠉAg   ڡAg   ԢAg   ΣAg   ȤAg   ¥Ag   Ag   Ag   Ag   Ag   Ag   Ag   A)yg     @@g     ܨ@g     J@g     @g     9@g     @g     @g     2@g    @g     @g     i@g     @g    j@g    @g     @g    @g     @g     @g    ?@g    @g     n@g     ײ@g     .@g     @g     h@g     @g     ޫ@g     Ӱ@g     >@g     @g     @g     b@g     @g    H@g    @g     @g    @g    d@g     @g    @g     p@g     ̳@g     m@g     D@g     ҧ@g     @g     @g     @g     G@g     @g     @g     _@g     @g    @g    @g     "@g    @g    @g     M@g     n@g    @g     @g     0@g     @g      @g    D@g     w@g     9@g     y@g     µ@g     6@g     @g     \@g     F@g     @g     Ь@g     k@g     @g     @g     V@g     @g     @g     D@g    @g    J@g    @g     @g     @g    @g    @g    3@g    @g    @g    @g     @g     @g    s@g    @g    :@g    b@g     @g     @g     p@g     @g     @g     @g     Q@g     @g     \@g     !@g     @g    j@g     @g    F@g    @g    @g    @g     e@g    @g     @g    @i  x   rn  rZ   T)r  r  )r$   r&   r&  r   )r)   r  rJ  periodsangular_freqr.   r.   r/   test_list_input>  s
    zTestLombscargle.test_list_inputc                 C   s   d}d}d}d}d}d}d}t jd}||}	t d	t j d
t j ||	|k }
|t ||
 |  | }t dd
|}t|
||ddd}|d dk sOJ d S )Nr!   r<   r  rm  i  ro  r   rp  rq  rr  Tr  -C6?r  )r)   ry  rz  r{  r|  r}  r,   r  r~  r  r  rJ  r+   r  r.   r.   r/   test_zero_freq  s   
"zTestLombscargle.test_zero_freqc                 C   s   dd t ddD }tt|}dtj gd }t||| dd t ddD }tt|}tjd gd }t||| d S )Nc                 S   s   g | ]}|d  qS )r   r.   .0r  r.   r.   r/   
<listcomp>  s    z8TestLombscargle.test_simple_div_zero.<locals>.<listcomp>r   rl   r!   rZ   c                 S   s   g | ]}|d  d qS )rQ   r   r.   r  r.   r.   r/   r    s    )r7  r$   r'   lenr&  r   )r)   r  rJ  freqsr.   r.   r/   test_simple_div_zero  s   z$TestLombscargle.test_simple_div_zeroN)r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r.   r.   r.   r/   rl    s"    )!($!		1"$	Hrl  c                   @   s   e Zd Zejjdd Zdd Zdd Zdd Z	d	d
 Z
ejdddgdd Zejjdd Zdd Zdd Zejjejdddgdd Zdd Zdd Zdd Zdd Zd d! Zd"S )#TestSTFTc                 C   s2  dd }|d t ddd W d    n1 sw   Y  |d t ddd	 W d    n1 s3w   Y  |d
 t tddd W d    n1 sPw   Y  |d t td	dd W d    n1 smw   Y  |d tddd W d    n1 sw   Y  |d tddd	 W d    n1 sw   Y  |d
 ttddd W d    n1 sw   Y  |d ttd	dd W d    n1 sw   Y  |d tddd W d    n1 sw   Y  td}t|d }|d
 t|tdd W d    n	1 sw   Y  |d t|tddd W d    n	1 s=w   Y  |d t|dd W d    n	1 sXw   Y  |d t|ddd W d    n	1 stw   Y  |d t|ddd W d    n	1 sw   Y  |d t| W d    n	1 sw   Y  |d
 t|tdd W d    n	1 sw   Y  |d t|tddd W d    n	1 sw   Y  |d t|dd W d    n	1 sw   Y  |d t|ddd W d    n	1 sw   Y  |d t|ddd W d    n	1 s9w   Y  tjt	dd  t|dddd! W d    n	1 sYw   Y  |d" t|ddd# W d    n	1 suw   Y  |d$ t
||d%d& W d    n	1 sw   Y  |d' t
|d d( |d(d  d)d& W d    n	1 sw   Y  |d* t
||d%d+ W d    n	1 sw   Y  d,}|d-|d. t||d/ W d    n	1 sw   Y  |d-|d. t||d/ W d    d S 1 sw   Y  d S )0Nc                 S   s   t jt| dS )zAssert for a ValueError matching regexp `match`.

            This little wrapper allows a more concise code layout.
            r   )r   r   rT   r   r.   r.   r/   chk_VE  s   z.TestSTFT.test_input_validation.<locals>.chk_VEz"nperseg must be a positive integerr   ir   z#noverlap must be less than nperseg.r[   rX   zwindow must be 1-D)rZ   rZ   z"window must have length of npersegz"noverlap must be less than npersegz&noverlap must be a nonnegative integerr  ir1  rZ   r   z>value specified for nperseg is different from length of windowr   r   i r   r  z.nfft must be greater than or equal to nperseg.r"   r  zInput stft must be at least 2d!zwindow must have length of 256z1NOLA condition failed, STFT may not be invertibler   r   r  r   z/Must specify differing time and frequency axes!	time_axis	freq_axisz=Unknown value for mode foo, must be one of: \{'psd', 'stft'\}rR   )modez'x and y must be equal if mode is 'stft'   stftz[Unknown boundary option 'foo', must be one of: \['even', 'odd', 'constant', 'zeros', None\])boundary	not_validzParameter scaling=z not in \['spectrum', 'psd'\]!rA   )r   r$   r'   r   r%   r  istftr   warnsr   r   )r)   r  r*   zrB   r.   r.   r/   test_input_validation  s   
























 
$zTestSTFT.test_input_validationc                 C   s2   g d}|D ]}dj | }tdt| |d qd S )N)boxcarr[   r   r  r[   r   r  3      r   r      r   r      blackmanr     r   r   r   r  r   r      
{}, {}, {}Tr2  )formatr   r   )r)   settingssettingr   r.   r.   r/   test_check_COLA  s
   
zTestSTFT.test_check_COLAc                 C   s   g d}|D ]}dj | }tdt| |d qtd}d|d d d< |t|t|d fdg}|D ]}dj | }td	t| |d q2d S )
N)r  r  )r  r[   r  r  )r  r  r[   r  r  )r   r   %   r  )r  r  {   r  )r  r   &   r  )r   r   '   r  Tr2  r   r   rZ   )r   r  r   F)r  r   r   r$   r'   r  )r)   settings_passr  r   w_failsettings_failr.   r.   r/   test_check_NOLA  s   


zTestSTFT.test_check_NOLAc              
   C   s   t jd}|d}d}d}d}d}t|||||ddd d\}}}	t|||||dd	dd
\}
}t||
 tt jt |	d dd| d S )Nr0  r1  r<   r   r   r"   F)paddedr;   r  r@   )r;   rB   r   rZ   r    rh   )	r$   r4  ru  r6  r  r   r   rV  r  )r)   r~  r*   r#  r   r   r  r+   r  Zre  rf  r.   r.   r/   rg  :  s   


"z"TestSTFT.test_average_all_segmentsc              	   C   s   t jd}|d}d}d}d}d}t|||||\}}}	t|d||||dd	\}
}}t|	||||\}}t|j||||dd
d\}}t||
 t|| t|| t|	|d d ddd d f  t||d d ddf  d S )Nr0  r1  r<   r   r   r"   )r    r   r   r   rh   r    r  )	r$   r4  ru  r6  r  r`   r  Tr   )r)   r~  r*   r#  r   r   r  r   t1Z1r   t2Z2t3x1t4x2r.   r.   r/   test_permute_axesN  s&   




zTestSTFT.test_permute_axesrB   r@   psdc              
   C   s   t jd}g d}|D ]A\}}}}t |}d||j }	t|	|||d d|d\}
}
}t|||||d\}}| d| }t|||d t|	||d qd S )	Nr0  r  rm  r[   r   r  rm  r[   r   )r  e   r  r  r   r1  r   r  )r    r   r  )r   r1  r   r  r[   F)r   r  r   r   r  rB   )r   r  r   rB   r  r2  	r$   r4  ru  r8   r6  sizer  r  r   )r)   rB   r~  r  r   Nr   r  r  r*   r  zztrxrr   r.   r.   r/   test_roundtrip_reale  s    	

zTestSTFT.test_roundtrip_realc              
   C   s>  t jd}t d}d|d d d< |dt|t|d fdg}|D ]z\}}}}| d| d| d| }t|||r?J |t |}	d||	j }
t	|
|||d d	d
d\}}}t
jtdd t||||d	d\}}W d    n1 sww   Y  t |	|d t|	 sJ |t |
|d t|
 rJ |q"d S )Nr0  r   r   rZ   r   )r   r   r  r   r  r[   Tr%   r   r  r   r   r  r  NOLAr   r   r  r   r  )r$   r4  ru  r'   r  r   r8   r6  r  r  r   r  r   r  allclose)r)   r~  r  r  r   r  r   r  r   r  r*   r  r  r  r  r.   r.   r/   test_roundtrip_not_nola  s.   

 "z TestSTFT.test_roundtrip_not_nolac              
   C   s   t jd}g d}|D ]k\}}}}| d| d| }t|||s&J |t|||r0J |t |}d||j }	t|	|||d ddd\}
}
}t	||||dd\}}| d| }t
||d t| |d	 t
|	|d t|	 |d	 qd S )
Nr0  ))r  rm  r[   r   )r  r   r  r  )r   r1  r      )r  r  r      )r   r1  r   r   r  r[   Tr%   r
  r  r2  )r$   r4  ru  r   r   r8   r6  r  r  r  r   r  )r)   r~  r  r   r  r   r  r   r  r*   r  r  r  r  r.   r.   r/   test_roundtrip_nola_not_cola  s&   

z%TestSTFT.test_roundtrip_nola_not_colac              	   C   s   t jd}dg}|D ]O\}}}}t |}d||j }|t j}t||||d dd\}	}	}
t	|
|||d\}}| d| }t
|||d t
|||d	d
d t|j|jk qd S )Nr0  r  r[   Fr   r  r   r   r  r  r  r2  r  gh㈵>)r3  r   r7   )r$   r4  ru  r8   r6  r  astypefloat32r  r  r   r   rG   r)   r~  r  r   r  r   r  r  r*   r  r  r  r  r   r.   r.   r/   test_roundtrip_float32  s"   

zTestSTFT.test_roundtrip_float32c                 C   sR  t jd}g d}|D ]N\}}}}t |}d||j d||j  }	t|	|||d dd|d\}
}
}t||||d|d\}}| d| d| }t|||d	 t|	||d	 qt	 }|
td
 t|	|||d dd|d\}
}
}W d    n1 s~w   Y  t||||d|d\}}| d| d| }t|||d	 t|	||d	 d S )Nr0  r  r[   y              $@F)r   r  r   r   r  r;   rB   )r   r  r   input_onesidedrB   r  r2  z9Input data is complex, switching to return_onesided=FalseT)r$   r4  ru  r8   r6  r  r  r  r   r   r   r   )r)   rB   r~  r  r   r  r   r  r  r*   r  r  r  r  r   r   r.   r.   r/   test_roundtrip_complex  s>   	
 

zTestSTFT.test_roundtrip_complexc                 C   s   t jd}ddg}|D ]`\}}}}t |}d||j }t||||d dd d\}	}	}
t|
||dd\}	}d	D ]2}t||||d d|d\}	}	}t|||dd\}	}| d
| d
| }t|||d t|||d q9qd S )Nr0  r  r  r[   Tr
  F)r  r   r  )evenoddconstantr%   r  r2  r  )r)   r~  r  r   r  r   r  r  r*   r  r  r  r  zz_extxr_extr   r.   r.   r/   !test_roundtrip_boundary_extension  s2   

z*TestSTFT.test_roundtrip_boundary_extensionc              	   C   s   t jd}ddg}|D ]H\}}}}t |}d||j }t||||d dd\}	}	}
t|
||d\}}| d| }t||d |j |d	 t||d |j |d	 qd S )
Nr0  )r  r   r[   r   )r   rn  r   r  r[   Tr  )r  r   r  r2  r  r  r.   r.   r/   test_roundtrip_padded_signal  s   
z%TestSTFT.test_roundtrip_padded_signalc                 C   s   t jd}g d}|D ]p\}}}}}t |}d||j }	|	t dt j d  }
t|	||||d dd\}}}t|
||||d ddd	\}}}t	|||||d
\}}t	|||||dd\}}| d| }t
|||d t
|	||d t
|
||d qd S )Nr0  ))r   r1  r   r  r  )r   r1  r   r  i  )r  rm  r[   r   !   )r  r  r   r  r1  r[   y              ?rQ   T)r   r  ru   r   r   r  F)r   r  ru   r   r   r  r;   )r   r  ru   r   )r   r  ru   r   r  r  r2  )r$   r4  ru  r8   r6  r  expr&  r  r  r   )r)   r~  r  r   r  r   r  ru   r  r*   xcr  r  zcr  r  xcrr   r.   r.   r/   test_roundtrip_padded_FFT4  s0   






z"TestSTFT.test_roundtrip_padded_FFTc                 C   s   t jd}|d}t|\}}}tdD ]:}dgd }d||< ||}t||d\}}}t|||j d\}}}	t||	 |d t||		 ||j d qt
|jddd	\}}
t
|jd
dd	\}}t||
dd t||dd d S )Nr0  r1  r   r   r    rh   r2  r  r   zistft transpose minuszistft transpose plus)r$   r4  ru  r6  r  r7  r`   r8  r   r9  r  r  r   )r)   r~  r:  r  z_flatr<  r=  r*   z_plusz_minusx_transpose_mx_transpose_pr.   r.   r/   r@  W  s   


zTestSTFT.test_axis_rollingc                 C   s6  t jdtd}d|d< t j|}t|d t| }t|dddd }t|d	ddd
 }t	|| t	t
|dddf d t	t
|dddf d
 t	t
|dddf d d|ddddf< t	|ddddf dt |jjd t|ddddd }t j|jd |jd  dd|jd  }t	|| t|dd	ddd
 }t	|| t|d	dddd }t |dddddf }	t	|ddddf | t	|ddddf |	 t j|jd |jd  ddt j|	jd |	jd  dd }
|
|jd |	jd   }t	|| t|d	d	ddd
 }t	|| dS )z&Verify behavior of scaling parameter. i  rF   r1  r   rZ   r  r@   )r  rB   Tr   ?   Nr    r   r  A   r   B   r6   Fr  )r;   r  rB   rh   )r  r  rB   r&     )r$   r%   complexr   r   r  r  r  r  r   r  finforG   
resolutionrealimagra   conj)r)   Xr*   power_xZsr  Zppsd_ZpZp0Zp1s2psd_Zp01r.   r.   r/   test_roundtrip_scalingq  s8   
((


zTestSTFT.test_roundtrip_scalingN)r   r   r   r   r   thread_unsafer  r  r  rg  r  parametrizer	  r  r  r  r  r  r  r%  r@  r?  r.   r.   r.   r/   r    s*    
R

+!#r  c                   @   s   e Zd ZU dZdZeed< dZeed< dZ	eed< dZ
eed< ejed	< ejed
< eed< eed< dd ZedefddZedefddZdd Zdd ZdS )"TestSampledSpectralRepresentationsa  Check energy/power relations from `Spectral Analysis` section in the user guide.

    A 32 sample cosine signal is used to compare the numerical to the expected results
    stated in :ref:`tutorial_SpectralAnalysis` in
    file ``doc/source/tutorial/signal.rst``
    rl   nr=   r  r   a_refl_ax_refX_refE_refP_refc                 C   st   t | j| j}t|}d| _| jd | j || j< t|| _t	| j| _
| j| jd  d | _| jd d | _dS )z5Create Cosine signal with amplitude a from spectrum. r   rZ   N)r   rC  r  r$   r   rE  rD  r   rF  r   rG  taurH  rI  )r)   r+   rG  r.   r.   r/   setup_method  s   

z/TestSampledSpectralRepresentations.setup_methodreturnc                 C   s   | j | j S )zDuration of signal. rC  r  rU   r.   r.   r/   rJ    s   z&TestSampledSpectralRepresentations.tauc                 C   s   d| j | j  S )z
Bin width r   rM  rU   r.   r.   r/   delta_f  s   z*TestSampledSpectralRepresentations.delta_fc                 C   s   t d| j t| jdd t | jt| jd  | j t| jj	d | jj
d  }t | j| j | | j t d| jd  | | j dS )z Test energy and power formulas. rZ   r!  r$  r   N)r   rD  r$   ptprF  r  r  rH  rG  r3  r4  rC  rI  )r)   sum_X_ref_squaredr.   r.   r/   test_reference_signal  s
   z8TestSampledSpectralRepresentations.test_reference_signalc                 C   s  t | jdd}tt|tt|jd |jd  }}t| j	| }t
| j|| j  | | j| j d  t
|| j | | jd  | j| j t|| d  }| jt|| d  }t
| jt| | j t
| jt| | j td| j |ddd}t| j	fddi|\}}	t| j	fddi|\}}
t|	jj}t
|	t|| d |t|	 d	 t
|
||t|
 d	 tdt| j	d
d|}t
t| j	fddi|d |	|t|	 d	 t
t| j	fddi|d |
|t|
 d	 dS )zVerify spectral representations of windowed DFT.

        Furthermore, the scalings of `periodogram` and `welch` are verified.
        F)symrZ   r   )r#  r   r   r;   rB   r@   rC   r6   r   r  Nr.   )r   rC  r  r  r$   r'  r3  r4  r   rF  r   rJ  rE  rD  r  rN  rH  rI  dictr   r1  rG   r2  r  r  r   )r)   rz  c_ampc_rmsXwX_ESDX_PSDkwr  P_magP_psd	float_resr.   r.   r/   test_windowed_DFT  s,   ,&"


z4TestSampledSpectralRepresentations.test_windowed_DFTN)r   r   r   __doc__rC  rH   __annotations__r  r  rD  rE  r$   ndarrayrK  propertyrJ  rN  rQ  r]  r.   r.   r.   r/   rB    s"   
 

rB  )1r   numpyr$   numpy.testingr   r   r   r   r   r   r   r   rS   scipyr	   	scipy.fftr
   r   r   r   scipy.integrater   scipy.signalr   r   r   r   r   r   r   scipy.signal.windowsr   scipy.signal._spectral_pyr   ,scipy.signal.tests._scipy_spectral_test_shimr   r  r   r  r   rH  r   r   rF  r[  rb  rl  r  rB  r.   r.   r.   r/   <module>   sB     $ ^  A  N5   m   