o
    Eb                     @   s   d dl Zd dlmZ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 d dlmZmZmZmZmZmZmZmZmZ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$dS )    N)assert_assert_approx_equalassert_allcloseassert_array_equalassert_equalassert_array_almost_equal_nulpsuppress_warnings)raises)signal)fftfreq)
periodogramwelchlombscarglecsd	coherencespectrogramstftistft
check_COLA
check_NOLA)_spectral_helperc                   @   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!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+S ),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*   B/usr/lib/python3/dist-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_spectrum.   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_even6   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 )Nr-   rB   r   r   r.   r/   r   r0   r1   r2   )r    r!   rD   r   r   r4   r#   r$   r*   r*   r+   test_integer_oddA   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   rB   r   r   Fr6   r8   r9   )r    r!   rD   r   r   r   r:   r$   r*   r*   r+   test_integer_twosidedK      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   Fr6   r8         ?)r    r!   
complex128r   r   r   r:   r$   r*   r*   r+   test_complexT   rH   zTestPeriodogram.test_complexc                 C   s   t tttdtjdd d S )N   foor=   )assert_raises
ValueErrorr   r    r!   rK   r%   r*   r*   r+   test_unk_scaling]   s   
z TestPeriodogram.test_unk_scalingc                 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   rB      r   
   r8   r   rU   r      r   <   )	r    r!   float64reshaper   r   shaper   newaxisr%   r&   r'   r(   Zf0Zp0r*   r*   r+   test_nd_axis_m1a   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 )
NrS   rB   rV   rU   r   r8   r   axisrX   rU   r   r   rY   )r    r!   rZ   r[   r   r   r\   r   r^   r*   r*   r+   test_nd_axis_0k   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   rV   hann    )r    r!   r   r
   
get_windowr   rO   rP   r%   r&   r'   r(   winfeZpewin_errr*   r*   r+   test_window_externalu   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   rf   nfftrU   )   )r    r!   r   r   r   r\   )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   ru      r   r      rU   )r   r   r\   r    emptyr%   r'   r(   r\   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rw   ry   r   ra   )r   r    r{   r   r\   r%   r\   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   rm   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   rm   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   rC   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 )Nr-   r'   r   r   r.   r/   r   r0   Hz>r2   )r    r!   r   r   r4   r#   r   rC   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   Fr6   r8   r9   r    r!   r   r   r   r:   r   rC   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   FrI   r   Fr6   r8   rJ   r'   r   r$   r*   r*   r+   test_complex_32   r   zTestPeriodogram.test_complex_32N)__name__
__module____qualname__r,   r5   r;   rA   rE   rF   rG   rL   rR   r_   rd   rl   rr   r}   r   r   r   r   r   r   r   r*   r*   r*   r+   r      s,    
	
		

		
r   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!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   rz   iKGUU?gS8?#q?r   #q?r   r3   rtolr    r!   r   r   r"   arrayr$   r*   r*   r+   r,         
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   r4   r   r$   r*   r*   r+   r5      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   r7   r8   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   r>   r   rz   )g      ?g-UU?.iqUU?r   g-UU?r   r   r   r$   r*   r*   r+   rA         
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   rB   r   r   r   r   r   rz   r   r   r   )r    r!   rD   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   rB   r   r   r   r   r   r   r   r   r   r   )r    r!   rD   r   r   r4   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   rB   r   r   r   Fr   r8   r   r   r   )r    r!   rD   r   r   r   r   r$   r*   r*   r+   rG        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   rI   r   r   Fr   r8   )g&>?#q?n)t?r   r   r   r   r   r   r   )r    r!   rK   r   r   r   r   r$   r*   r*   r+   rL   "  r   zTestWelch.test_complexc                 C   s    t tttdtjddd d S NrM   rN   )r>   r   )rO   rP   r   r    r!   rK   rQ   r*   r*   r+   rR   ,  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 NrV   rB   {Gz?Zlinearr   detrendr1   r2   r    r4   rZ   r   r   
zeros_liker%   r&   r'   r(   r*   r*   r+   test_detrend_linear0  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 )
NrV   rB   r   Fr   c                 S      | S Nr*   r&   r*   r*   r+   <lambda>8      z.TestWelch.test_no_detrending.<locals>.<lambda>r1   r2   )r    r4   rZ   r   r   r%   r&   f1p1f2p2r*   r*   r+   test_no_detrending5  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 )	NrV   rB   r   c                 S      t j| ddS Nl)typer
   r   Zsegr*   r*   r+   r   ?      z1TestWelch.test_detrend_external.<locals>.<lambda>r   r1   r2   r   r   r*   r*   r+   test_detrend_external<  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(   rB   r   rU   rU   rV   rV   c                 S   r   r   r   r   r*   r*   r+   r   F  r   z7TestWelch.test_detrend_external_nd_m1.<locals>.<lambda>r   r1   r2   )r    r4   rZ   r[   r   r   r   r   r*   r*   r+   test_detrend_external_nd_m1B  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 )NrS   rB   r   rT   rU   r   rV   c                 S      t j| dddS Nr   r   )rb   r   r   r   r*   r*   r+   r   N      z6TestWelch.test_detrend_external_nd_0.<locals>.<lambda>r   rb   r   r1   r2   )r    r4   rZ   r[   moveaxisr   r   r   r   r*   r*   r+   test_detrend_external_nd_0I  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 NrS   rB   r   rT   rV   r   rW   r   r   vIh%<=r   )	r    r4   rZ   r[   r   r   r\   r   r]   r^   r*   r*   r+   r_   Q  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 NrS   rB   r   r`   rV   r   )r   rb   rc   r   r   r   r   )r    r4   rZ   r[   r   r   r\   r   r^   r*   r*   r+   rd   Z  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   rV   re   r   rz   rM   rf   )
r    r!   r   r
   rg   r   r   r\   rO   rP   rh   r*   r*   r+   rl   c  s"   



zTestWelch.test_window_externalc                 C   rs   rt   )r   r   r\   r    r{   r|   r*   r*   r+   r}   t  r~   zTestWelch.test_empty_inputc                 C   r   r   )r   r    r{   r   r\   r   r*   r*   r+   r   }  r   z%TestWelch.test_empty_input_other_axisc           	      C   s   t d}d|d< t }|td t|dd\}}t|ddd\}}W d    n1 s.w   Y  t|dd	\}}t|| t|| t|| t|| d S 
Nr   r   r   zBnperseg = 256 is greater than input length  = 8, using nperseg = 8re   window   r   r   r   )r    r!   r   filterUserWarningr   r   	r%   r&   supr'   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 NrM   r   )r   r   r   r   r   rX   )rU   rx   )rO   rP   r   r    r!   r   r4   r[   rQ   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   rM   r   noverlap)	r   gUUUUUU?UUUUUU?皙?r   r   r   r   gUUUUUU?g-q=r2   )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 NrM   r   re   rU      )rO   rP   r   r    r!   rQ   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   rx   rM   )rn   r   )rO   rP   r   r    r#   rQ   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   rz   r   r   r   )r    r!   r   r   r"   r   r   rC   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   r4   r   r   rC   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   r8   r   r   r   r    r!   r   r   r   r   r   rC   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|jf  d S Nr   r   rI   r   r   Fr   r8   )g%?A2q?d^?r   g~?r   r   r   r'   r   r   zdtype mismatch, %s, %sr   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      r8   rU   r   r   rz   r   rn   rX      )r    r!   r   r   r   )r%   r&   rn   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 ]8}t||||dd\}	}
t||||d	d\}}t|
| |d d
  ttt|||td d dd q+d S )NrS   g     @rV   ,  rU   )re   bartlett)tukey皙?Zflattopr<   )fsr   r   r>   r?   r   gMbP?)r   )	rD   r    r4   sinpir   r   sqrtZtrapz)r%   Ar  r   Zfsigiittr&   r   r  Zp_specZfreqZ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     rx   r   r   ra   err_msg)
r    randomseedrandnr   ranger[   ndimr   squeeze)	r%   x_flatr  Zp_flatanewshaper&   Zp_plusZ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   Zmedian)r   Zaverager   rz   )r  g?        g&E    9r#  r   r   zunrecognised-average)r    r!   r   r   r"   r   rO   rP   r$   r*   r*   r+   test_average  s   


zTestWelch.test_averageN)#r   r   r   r,   r5   r;   rA   r   r   rG   rL   rR   r   r   r   r   r   r_   rd   rl   r}   r   r   r   r   r   r   r   r   r   r   r  r  r"  r$  r*   r*   r*   r+   r      sB    







			r   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!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?S )@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   rB   r   r    r!   r"   rK   r   r   r%   r&   yr'   cr   Zc1r*   r*   r+   test_pad_shorter_x     


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   rB   r   r&  r'  r*   r*   r+   test_pad_shorter_y%  r+  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!   r   r   r"   r   r$   r*   r*   r+   r,   0  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!   r   r   r4   r   r$   r*   r*   r+   r5   :  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!   r   r   r   r   r$   r*   r*   r+   r;   D  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   r-  r$   r*   r*   r+   rA   N  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!   rD   r   r   r"   r   r$   r*   r*   r+   r   X     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!   rD   r   r   r4   r   r$   r*   r*   r+   r   b  r.  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!   rD   r   r   r   r   r$   r*   r*   r+   rG   l     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!   rK   r   r   r   r   r$   r*   r*   r+   rL   v  r/  zTestCSD.test_complexc                 C   s,   t tttdtjtdtjddd d S r   )rO   rP   r   r    r!   rK   r#   rQ   r*   r*   r+   rR     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    r4   rZ   r   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 )
NrV   rB   r   Fr   c                 S   r   r   r*   r   r*   r*   r+   r     r   z,TestCSD.test_no_detrending.<locals>.<lambda>r1   r2   )r    r4   rZ   r   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 )	NrV   rB   r   c                 S   r   r   r   r   r*   r*   r+   r     r   z/TestCSD.test_detrend_external.<locals>.<lambda>r   r1   r2   r0  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   rB   r   r   rV   c                 S   r   r   r   r   r*   r*   r+   r     r   z5TestCSD.test_detrend_external_nd_m1.<locals>.<lambda>r   r1   r2   )r    r4   rZ   r[   r   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 )NrS   rB   r   rT   rU   r   rV   c                 S   r   r   r   r   r*   r*   r+   r     r   z4TestCSD.test_detrend_external_nd_0.<locals>.<lambda>r   r1   r2   )r    r4   rZ   r[   r   r   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    r4   rZ   r[   r   r   r\   r   r]   r^   r*   r*   r+   r_     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    r4   rZ   r[   r   r   r\   r   r^   r*   r*   r+   rd     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   rV   re   r   r   r   rf   )
r    r!   r   r
   rg   r   r   r\   rO   rP   rh   r*   r*   r+   rl     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 )NrV   ru   rv   )rz   r   )r   r    r!   r   r\   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   ra   )rV   rV   rx   )rV   r   r   )rV   r   rx   )r   r    r{   r   r\   r!   r   r*   r*   r+   r     s   z#TestCSD.test_empty_input_other_axisc           	      C   s   t d}d|d< t !}|td t||dd\}}t||ddd\}}W d    n1 s0w   Y  t||dd	\}}t|| t|| t|| t|| d S r   )r    r!   r   r   r   r   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   )	rO   rP   r   r    r!   r#   r   r4   r[   rQ   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!   r   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   )rO   rP   r   r    r!   r#   rQ   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   )rO   rP   r   r    r#   r!   rQ   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!   r   r   r"   r   r   rC   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!   r   r   r4   r   r   rC   r$   r*   r*   r+   r     r1  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!   r   r   r   r   r   rC   r$   r*   r*   r+   r   (  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|jf  d S r   r2  r$   r*   r*   r+   r   4  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   r   r   )r%   r&   r(  rn   r'   r  r  r  r*   r*   r+   r  @  s   




zTestCSD.test_padded_freqsN)"r   r   r   r*  r,  r,   r5   r;   rA   r   r   rG   rL   rR   r   r   r   r   r   r_   rd   rl   r}   r   r   r   r   r   r   r   r   r   r   r  r*   r*   r*   r+   r%    s@    







		r%  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 NrS   r   r   rX   rV   r   )r    r  r  copyr"   r#   r   r   r%   r&   r(  r'   Cr   ZC1r*   r*   r+   test_identical_inputT  s   


z"TestCoherence.test_identical_inputc                 C   sT   t jd}| }t ddd}t d}t||dd\}}t|| t|| d S r4  )r    r  r  r"   r#   r   r   r6  r*   r*   r+   test_phase_shifted_input_  s   

z&TestCoherence.test_phase_shifted_inputN)r   r   r   r8  r9  r*   r*   r*   r+   r3  S  s    r3  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 )Nr  r8   r  g      ?r   rU   r   ra   )r    r  r  r   r   r   mean)r%   r&   r  r   r   r   r'   r  PfwPwr*   r*   r+   test_average_all_segmentsl  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 )Nr  r8   r;  r   rU   r   )r   )r   I   r   r   i   )
r    r  r  r   r
   rg   r   r\   rO   rP   )r%   r&   r  r   r   r   r'   r  r=  ri   rj   ZPerk   r*   r*   r+   rl   y  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 )
Nr  r8   r;  r   zInperseg = 1025 is greater than input length  = 1024, using nperseg = 1024i  r   r   r   )r    r  r  r   r   r   r   r   )r%   r&   r  r'   r  r(   r   r   r   r   r   Zf3Zp3r*   r*   r+   r     s"   


zTestSpectrogram.test_short_dataN)r   r   r   r@  rl   r   r*   r*   r*   r+   r:  k  s    r:  c                   @   sD   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S )TestLombscarglec                 C   s   d}d}dt j }d}d}d}t jd t j|}t dt j d	t j |||k }|t || |  }	t dd	|}
t||	|
}|
d
 |
d  }t||
t 	|  |d k  dS )zgTest if frequency location of peak corresponds to frequency of
        generated input signal.
        r   r8   r   d     ffffff?# {Gz?      $@r   r   N)
r    r  r  r  randr"   r  r   r   Zargmax)r%   amplwphininnoutr(   rtr&   r'   r=  Zdeltar*   r*   r+   test_frequency  s   
""zTestLombscargle.test_frequencyc                 C   s   d}d}dt j }d}d}d}t jd t j|}t dt j d	t j |||k }|t || |  }	t dd	|}
t||	|
}t d
| |j	d  }t
t ||dd d S )Nr   r8   r   rC  rD  rE  rF  rG  rH  rM   r   rU   Zsignificant)r    r  r  r  rI  r"   r  r   r  r\   r   max)r%   rJ  rK  rL  rM  rN  r(   rO  rP  r&   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 j|}t d	t j d
t j |||k }	|t ||	 |  | }
t d	d
|}t|	|
|dd}t|	|
|
  |dd}t	|| d S )Nr   r8   r   rC  rD  rE  g333333?rF  rG  rH  T)Z	precenterF)
r    r  r  r  rI  r"   r  r   r<  r   )r%   rJ  rK  rL  rM  rN  r(   offsetrO  rP  r&   r'   rT  pgram2r*   r*   r+   test_precenter  s   
"zTestLombscargle.test_precenterc                 C   s   d}d}dt j }d}d}d}t jd t j|}t dt j d	t j |||k }|t || |  }	t dd	|}
t||	|
}t||	|
d
d}t|d t 	|	|	 | t
t |ddd d S )Nr   r8   r   rC  rD  rE  rF  rG  rH  T)	normalizerU   rR  )r    r  r  r  rI  r"   r  r   r   dotr   rS  )r%   rJ  rK  rL  rM  rN  r(   rO  rP  r&   r'   rT  rW  r*   r*   r+   test_normalize  s   
"zTestLombscargle.test_normalizec                 C   s>   t ddd}t ddd}t ddd}ttt||| d S )Nr   r   rU   rx   )r    r"   rO   rP   r   r%   rP  r&   r'   r*   r*   r+   test_wrong_shape  s   z TestLombscargle.test_wrong_shapec                 C   s2   t d}t d}t d}ttt||| d S )Nr   )r    r!   rO   ZeroDivisionErrorr   r\  r*   r*   r+   test_zero_division#  s   


z"TestLombscargle.test_zero_divisionc                 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   rV   rD  F)ZendpointrM   2   i  r  rU   )r    r"   r  r   r  r\  r*   r*   r+   test_lombscargle_atan_vs_atan2)  s   z.TestLombscargle.test_lombscargle_atan_vs_atan2N)
r   r   r   rQ  rU  rX  r[  r]  r_  ra  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S )TestSTFTc                 C   s  t ttddd t ttddd t tttddd t tttddd t ttddd t ttddd t tttddd t tttddd t ttddd td	}tjt|t	d
}t tt|tdd t tt|tddd t tt|dd t tt|dd	d t tt|ddd t tt
| t tt
|tdd t tt
|tddd t tt
|dd t tt
|dd	d t tt
|ddd t tt
|dddd t tt
|ddd t tt||dd t tt|d d |dd  dd t tt||dd d S )Nre   ir   rV   rS   )rU   rU   r   ir  rB   r   r   r   i r   r   r   r  r   r   r   Z	time_axisZ	freq_axisrN   )mode   r   )boundary)rO   rP   r   r    r#   r   r!   r   r   objectr   r   )r%   r&   zr*   r*   r+   test_input_validation3  s>   
zTestSTFT.test_input_validationc                 C   s2   g d}|D ]}dj | }tdt| |d qd S )N)boxcarrV   r   rl  rV   r   r
  3      re   r      re   r      blackmanr	     r  r   r   r   re   r      {0}, {1}, {2}Tr  )formatr   r   )r%   settingssettingmsgr*   r*   r+   test_check_COLAW  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)rk  rm  )rl  rV   r   rn  )r
  ro  rV   rq  rs  )re   r   %   ru  )rv  r	  {   rx  )ry  r   &   rz  )re   r   '   r|  Tr  r   r   rU   )re   r   r   F)r}  r   r   r    r#   len)r%   Zsettings_passr  r  w_failZsettings_failr*   r*   r+   test_check_NOLAg  s   


zTestSTFT.test_check_NOLAc              
   C   s   t jd t j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 )Nr  r  r8   re   r   r   F)paddedr7   rg  r<   )r7   r>   r   rU   r   ra   )	r    r  r  r  r   r   r   r<  abs)r%   r&   r  r   r   r   r'   r  Zr>  r?  r*   r*   r+   r@    s   

"z"TestSTFT.test_average_all_segmentsc              	   C   s   t jd t j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 )Nr  r  r8   re   r   r   )r   r   r   r   ra   r   rd  )	r    r  r  r  r   r[   r   Tr   )r%   r&   r  r   r   r   r   t1ZZ1r   t2ZZ2Zt3Zx1Zt4Zx2r*   r*   r+   test_permute_axes  s&   



zTestSTFT.test_permute_axesc              	   C   s   t jd g d}|D ]?\}}}}t |}dt j|j }t||||d dd\}}}	t|	|||d\}
}d||}t	||
|d t	|||d qd S )	Nr  rl  rC  rV   r   rl  rC  rV   r   )r
  e   ro  rp  re   r  r   rr  )ry    r   r   )re   r  r   r{  rV   Fr   r   r   r   r  rc  {0}, {1}r  
r    r  r  r4   r  sizer   r   r}  r   r%   r~  r   Nr   r   rP  r&   r  zztrxrr  r*   r*   r+   test_roundtrip_real  s   	

zTestSTFT.test_roundtrip_realc              
   C   s6  t jd t d}d|d d d< |dt|t|d fdg}|D ]v\}}}}d||||}t|||r:J |t |}dt j|j	 }	t
|	|||d d	d
d\}
}
}tjtdd t||||d	d\}}W d    n1 ssw   Y  t ||d t| sJ |t |	|d t|	 rJ |q"d S )Nr  r   r   rU   r   )re   r   r   r   z{0}, {1}, {2}, {3}rV   Tr!   r   r   r   r   r  rg  ZNOLA)matchr   r   r   rg  )r    r  r  r#   r  r}  r   r4   r  r  r   pytestZwarnsr   r   Zallclose)r%   r  r~  r   r  r   r   r  rP  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 ]h\}}}}d|||}t|||s#J |t|||r-J |t |}dt j|j }t	||||d ddd\}	}	}
t
|
|||dd\}}d	||}t||d t| |d
 t||d t| |d
 qd S )Nr  ))rl  rC  rV   rx   )r
  r  ro  r  )re   r  r      )ry  r  r      )re   r  r   rz   r|  rV   Tr!   r  r  r  r  )r    r  r  r}  r   r   r4   r  r  r   r   r   r  )r%   r~  r   r  r   r   r  rP  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t j|j }|t j}t||||d dd\}}}	t	|	|||d\}
}d
||}t|||d t|||d	d
d t|j|jk qd S )Nr  r  rV   Fr  rc  r  r  g-C6?gh㈵>)r  r   r3   )r    r  r  r4   r  r  ZastypeZfloat32r   r   r}  r   r   rC   r  r*   r*   r+   test_roundtrip_float32  s"   

zTestSTFT.test_roundtrip_float32c              
   C   sB  t jd g d}|D ]K\}}}}t |}dt j|j dt j|j  }t||||d ddd\}}}	t|	|||dd\}
}d|||}t	||
|d	 t	|||d	 qt
 }|td
 t||||d ddd\}}}	W d    n1 szw   Y  t|	|||dd\}
}d|||}t	||
|d	 t	|||d	 d S )Nr  r  rV   y              $@F)r   r   r   r   r  r7   )r   r   r   input_onesidedr|  r  z9Input data is complex, switching to return_onesided=FalseT)r    r  r  r4   r  r  r   r   r}  r   r   r   r   )r%   r~  r   r  r   r   rP  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t j|j }t||||d dd d\}}}	t|	||dd\}}
d	D ]/}t||||d d|d\}}}t|||dd\}}d
|||}t	||
|d t	|||d q:qd S )Nr  r  r  rV   Tr  F)r   r   rg  )ZevenZoddZconstantr!   r|  r  r  )r%   r~  r   r  r   r   rP  r&   r  r  r  rg  Zzz_extZxr_extr  r*   r*   r+   !test_roundtrip_boundary_extensionC  s2   

z*TestSTFT.test_roundtrip_boundary_extensionc              	   C   s   t jd ddg}|D ]H\}}}}t |}dt j|j }t||||d dd\}}}	t|	||d\}
}d||}t	||
d |j |d	 t	||d |j |d	 qd S )
Nr  )rl  r  rV   r   )re   rD  r   rr  rV   Tr  )r   r   r  r  r  r  r*   r*   r+   test_roundtrip_padded_signald  s   
z%TestSTFT.test_roundtrip_padded_signalc                 C   s   t jd g d}|D ]p\}}}}}t |}dt j|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 )Nr  ))re   r  r   rr  rf  )re   r  r   rr  i  )rl  rC  rV   r   !   )ry  r  r   r   r  rV   y              ?rM   T)r   r   rn   r   r   r  F)r   r   rn   r   r   r  r7   )r   r   rn   r   )r   r   rn   r   r  r  r  )r    r  r  r4   r  r  Zexpr  r   r   r}  r   )r%   r~  r   r  r   r   rn   rP  r&   Zxcr  ri  Zzcr  r  Zxcrr  r*   r*   r+   test_roundtrip_padded_FFTz  s0   






z"TestSTFT.test_roundtrip_padded_FFTc                 C   s   t jd t j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 )Nr  r  rx   r   r   ra   r  rd  r   zistft transpose minuszistft transpose plus)r    r  r  r  r   r  r[   r  r   r  r   r  r   )r%   r  r  Zz_flatr   r!  r&   Zz_plusZz_minusZx_transpose_mZx_transpose_pr*   r*   r+   r"    s   

zTestSTFT.test_axis_rollingN)r   r   r   rj  r  r  r@  r  r  r  r  r  r  r  r  r  r"  r*   r*   r*   r+   rb  2  s    $*!#rb  )%Znumpyr    Znumpy.testingr   r   r   r   r   r   r   r  r	   rO   Zscipyr
   Z	scipy.fftr   Zscipy.signalr   r   r   r   r   r   r   r   r   r   Zscipy.signal._spectral_pyr   r   r   r%  r3  r:  rB  rb  r*   r*   r*   r+   <module>   s*    $0 M  @  <4 