o
    tBh                     @   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*   w/var/www/html/riverr-enterprise-integrations-main/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_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(   f0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fe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   rh   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   rx      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rz   r|   r   rc   )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   rp   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   rp   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   ra   rf   ro   ru   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   r}   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   r}   )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   r}   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?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   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   )rd   r   r   r   r*   r*   r+   r   N      z6TestWelch.test_detrend_external_nd_0.<locals>.<lambda>r   rd   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+   ra   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   rb   rV   r   )r   rd   re   r   r   r   r   )r    r4   rZ   r[   r   r   r\   r   r^   r*   r*   r+   rf   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   rg   r   r}   rM   rh   )
r    r!   r   r
   ri   r   r   r\   rO   rP   rj   r*   r*   r+   ro   c  s"   



zTestWelch.test_window_externalc                 C   rv   rw   )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 = 8rg   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   r{   )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   rg   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   r{   rM   )rq   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   r}   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   r}   r   rq   rX      )r    r!   r   r   r   )r%   r&   rq   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   )rg   bartlett)tukey皙?flattopr<   )fsr   r   r>   r?   r   gMbP?)r   )	rD   r    r4   sinpir   r   sqrttrapz)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   rc   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?        g&E    9r3  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   ra   rf   ro   r   r   r   r   r   r   r   r   r   r   r   r  r  r0  r4  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   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   r6  r7  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   rA  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+   ra     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+   rf     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   rg   r   r   r   rh   )
r    r!   r   r
   ri   r   r   r\   rO   rP   rj   r*   r*   r+   ro     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   rx   ry   )r}   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   rc   )rV   rV   r{   )rV   r   r   )rV   r   r{   )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     rB  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  rC  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&   r8  rq   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   ra   rf   ro   r   r   r   r   r   r   r   r   r   r   r   r  r*   r*   r*   r+   r5    s@    







		r5  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&   r8  r'   Cr   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 rE  )r    r$  r&  r"   r#   r   r   rG  r*   r*   r+   test_phase_shifted_input_  s   

z&TestCoherence.test_phase_shifted_inputN)r   r   r   rJ  rK  r*   r*   r*   r+   rD  S  s    rD  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   rc   )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   rM  r   rU   r   )r   )r   I   r   r   i   )
r    r$  r&  r   r
   ri   r   r\   rO   rP   )r%   r&   r  r   r   r   r'   r  rO  rk   rl   Pern   r*   r*   r+   ro   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   rM  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   f3p3r*   r*   r+   r     s"   


zTestSpectrogram.test_short_dataN)r   r   r   rR  ro   r   r*   r*   r*   r+   rL  k  s    rL  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   argmax)r%   amplwphininnoutr(   rtr&   r'   rO  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   rX  rY  rZ  r[  r\  r]  rM   r   rU   significant)r    r  r$  r%  r^  r"   r  r   r  r\   r   max)r%   r`  ra  rb  rc  rd  r(   re  rf  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   rX  rY  rZ  g333333?r[  r\  r]  T)	precenterF)
r    r  r$  r%  r^  r"   r  r   rN  r   )r%   r`  ra  rb  rc  rd  r(   offsetre  rf  r&   r'   rl  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   rX  rY  rZ  r[  r\  r]  T)	normalizerU   ri  )r    r  r$  r%  r^  r"   r  r   r   dotr   rk  )r%   r`  ra  rb  rc  rd  r(   re  rf  r&   r'   rl  rp  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   r{   )r    r"   rO   rP   r   r%   rf  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   ru  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   rY  F)endpointrM   2   i  r  rU   )r    r"   r  r   r  ru  r*   r*   r+   test_lombscargle_atan_vs_atan2)  s   z.TestLombscargle.test_lombscargle_atan_vs_atan2N)
r   r   r   rh  rm  rq  rt  rv  rx  r{  r*   r*   r*   r+   rW    s     !rW  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 )Nrg   ir   rV   rS   )rU   rU   r   ir!  rB   r   r   r   i r   r   r   r  r   r   r   	time_axis	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   r  rV   r   r  3      rg   r      rg   r      blackmanr     r  r   r   r   rg   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)r  r  )r  rV   r   r  )r  r  rV   r  r  )rg   r   %   r  )r  r  {   r  )r  r   &   r  )rg   r   '   r  Tr"  r   r   rU   )rg   r   r   F)r  r   r   r    r#   len)r%   settings_passr  r  w_fail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   rg   r   r   F)paddedr7   r  r<   )r7   r>   r   rU   r   rc   )	r    r$  r%  r&  r   r   r   rN  abs)r%   r&   r  r   r   r   r'   r  ZrP  rQ  r*   r*   r+   rR    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   rg   r   r   )r   r   r   r   rc   r   r~  )	r    r$  r%  r&  r   r[   r   Tr   )r%   r&   r  r   r   r   r   t1Z1r   t2Z2t3x1t4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   r  rX  rV   r   r  rX  rV   r   )r  e   r  r  rg   r!  r   r  )r    r   r   )rg   r!  r   r  rV   Fr   r   r   r   r  r}  {0}, {1}r"  
r    r$  r%  r4   r&  sizer   r   r  r   r%   r  r   Nr   r   rf  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   )rg   r   r   r   z{0}, {1}, {2}, {3}rV   Tr!   r   r   r   r   r  r  NOLA)matchr   r   r   r  )r    r$  r%  r#   r  r  r   r4   r&  r  r   pytestwarnsr   r   allclose)r%   r  r  r   r  r   r   r  rf  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   ))r  rX  rV   r{   )r  r  r  r  )rg   r!  r      )r  r  r      )rg   r!  r   r}   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  rf  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  r}  r  r"  g-C6?gh㈵>)r#  r   r3   )r    r$  r%  r4   r&  r  astype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   rf  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   r  )evenoddconstantr!   r  r"  r  )r%   r  r   r  r   r   rf  r&   r  r  r  r  zz_ext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   )r  r  rV   r   )rg   rY  r   r  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   ))rg   r!  r   r  r  )rg   r!  r   r  i  )r  rX  rV   r   !   )r  r  r   r   r!  rV   y              ?rM   T)r   r   rq   r   r   r  F)r   r   rq   r   r   r  r7   )r   r   rq   r   )r   r   rq   r   r  r  r"  )r    r$  r%  r4   r&  r  expr  r   r   r  r   )r%   r  r   r  r   r   rq   rf  r&   xcr  r  zcr  r  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!  r{   r   r   rc   r"  r~  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_flatr,  r-  r&   z_plusz_minusx_transpose_mx_transpose_pr*   r*   r+   r0    s   

zTestSTFT.test_axis_rollingN)r   r   r   r  r  r  rR  r  r  r  r  r  r  r  r  r  r0  r*   r*   r*   r+   r|  2  s    $*!#r|  )%numpyr    numpy.testingr   r   r   r   r   r   r   r  r	   rO   scipyr
   	scipy.fftr   scipy.signalr   r   r   r   r   r   r   r   r   r   scipy.signal._spectral_pyr   r   r   r5  rD  rL  rW  r|  r*   r*   r*   r+   <module>   s*    $0 M  @  <4 