o
    i                     @   s  d dl Z 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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*   o/var/www/html/eduruby.in/lip-sync/lip-sync-env/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_spectrum0   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_even8   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_oddC   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_twosidedM      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_complexV   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_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   rB      r   
   r8   r   rV   r      r   <   )	r    r!   float64reshaper   r   shaper   newaxisr%   r&   r'   r(   Zf0p0r*   r*   r+   test_nd_axis_m1c   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 )
NrT   rB   rW   rV   r   r8   r   axisrY   rV   r   r   rZ   )r    r!   r[   r\   r   r   r]   r   r_   r*   r*   r+   test_nd_axis_0q   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   rW   hann    )r    r!   r   r
   
get_windowr   rO   rP   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   rh   nfftrV   )   )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      rV   )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_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   rg   rW   zOthe size of the window must be the same size of the input on the specified axismatchwindow)r    r!   r
   ri   rO   rP   r   )r%   r&   rk   Zexpected_msgr*   r*   r+   test_shorter_window_error   s   
"z)TestPeriodogram.test_shorter_window_errorN)__name__
__module____qualname__r,   r5   r;   rA   rE   rF   rG   rL   rR   pytestmarkZskipifsysmaxsizera   rf   ro   ru   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   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   5  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 NrW   rB   {Gz?Zlinearr   detrendr1   r2   r    r4   r[   r   r   
zeros_liker%   r&   r'   r(   r*   r*   r+   test_detrend_linearC  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 )
NrW   rB   r   Fr   c                 S      | S Nr*   r&   r*   r*   r+   <lambda>K      z.TestWelch.test_no_detrending.<locals>.<lambda>r1   r2   )r    r4   r[   r   r   r%   r&   f1p1f2p2r*   r*   r+   test_no_detrendingH  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 )	NrW   rB   r   c                 S      t j| ddS Nl)typer
   r   segr*   r*   r+   r   R      z1TestWelch.test_detrend_external.<locals>.<lambda>r   r1   r2   r   r   r*   r*   r+   test_detrend_externalO  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   rV   rV   rW   rW   c                 S   r   r   r   r   r*   r*   r+   r   Y  r   z7TestWelch.test_detrend_external_nd_m1.<locals>.<lambda>r   r1   r2   )r    r4   r[   r\   r   r   r   r   r*   r*   r+   test_detrend_external_nd_m1U  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 )NrT   rB   r   rU   rV   r   rW   c                 S      t j| dddS Nr   r   )rd   r   r   r   r*   r*   r+   r   a      z6TestWelch.test_detrend_external_nd_0.<locals>.<lambda>r   rd   r   r1   r2   )r    r4   r[   r\   moveaxisr   r   r   r   r*   r*   r+   test_detrend_external_nd_0\  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 NrT   rB   r   rU   rW   r   rX   r   r   vIh%<=r   )	r    r4   r[   r\   r   r   r]   r   r^   r_   r*   r*   r+   ra   d  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 NrT   rB   r   rb   rW   r   )r   rd   re   r   r   r   r   )r    r4   r[   r\   r   r   r]   r   r_   r*   r*   r+   rf   m  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   rW   rg   r   r}   rM   rh   )
r    r!   r   r
   ri   r   r   r]   rO   rP   rj   r*   r*   r+   ro   v  s"   



zTestWelch.test_window_externalc                 C   rv   rw   )r   r   r]   r    r~   r   r*   r*   r+   r     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   r      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   rY   )rV   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   rV      )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 d|j  d S Nr   r   rI   r   r   Fr   r8   )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     r8   rV   r   r   r}   r   rq   rY      )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 )NrT   g     @rW   ,  rV   )rg   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_spec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     r{   r   r   rc   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   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                   @   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   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_y8  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,   C  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   M  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;   W  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   a  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   k     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   u  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        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     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   r[   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 )
NrW   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   r[   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 )	NrW   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   rW   c                 S   r   r   r   r   r*   r*   r+   r     r   z5TestCSD.test_detrend_external_nd_m1.<locals>.<lambda>r   r1   r2   )r    r4   r[   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 )NrT   rB   r   rU   rV   r   rW   c                 S   r   r   r   r   r*   r*   r+   r     r   z4TestCSD.test_detrend_external_nd_0.<locals>.<lambda>r   r1   r2   )r    r4   r[   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   r[   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   r[   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   rW   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 )NrW   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   )rW   rW   r{   )rW   r   r   )rW   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   0  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 d|j  d S r
  rC  r$   r*   r*   r+   r   G  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  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   r2  r7   r1  )r    r'  r)  copyr   r   )r%   r&   r8  r  Zp_sameZp_copiedr*   r*   r+   test_copied_dataf  s    








zTestCSD.test_copied_dataN)#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  rF  r*   r*   r*   r+   r5  ,  sB    







		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 NrT   r   r   rY   rW   r   )r    r'  r)  rE  r"   r#   r   r   r%   r&   r8  r'   Cr   ZC1r*   r*   r+   test_identical_inputx  s   


z"TestCoherence.test_identical_inputc                 C   sT   t jd}| }t ddd}t d}t||dd\}}t|| t|| d S rH  )r    r'  r)  r"   r#   r   r   rI  r*   r*   r+   test_phase_shifted_input  s   

z&TestCoherence.test_phase_shifted_inputN)r   r   r   rK  rL  r*   r*   r*   r+   rG  w  s    rG  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   rV   r   rc   )r    r'  r)  r   r   r   rD  )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 )Nr$  r8   rN  r   rV   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   ZPern   r*   r*   r+   ro     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   rN  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   rR  ro   r   r*   r*   r*   r+   rM    s    rM  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'   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   rU  rV  rW  rX  rY  rZ  rM   r   rV   Zsignificant)r    r  r'  r(  r[  r"   r  r   r  r]   r   max)r%   r\  r]  r^  r_  r`  r(   ra  rb  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   rU  rV  rW  g333333?rX  rY  rZ  T)Z	precenterF)
r    r  r'  r(  r[  r"   r  r   rD  r   )r%   r\  r]  r^  r_  r`  r(   offsetra  rb  r&   r'   rg  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   rU  rV  rW  rX  rY  rZ  T)	normalizerV   re  )r    r  r'  r(  r[  r"   r  r   r   dotr   rf  )r%   r\  r]  r^  r_  r`  r(   ra  rb  r&   r'   rg  rj  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   rV   r{   )r    r"   rO   rP   r   r%   rb  r&   r'   r*   r*   r+   test_wrong_shapeA  s   z TestLombscargle.test_wrong_shapec                 C   s2   t d}t d}t d}ttt||| d S )Nr   )r    r!   rO   ZeroDivisionErrorr   ro  r*   r*   r+   test_zero_divisionG  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   rW   rV  F)ZendpointrM   2   i  r  rV   )r    r"   r  r   r  ro  r*   r*   r+   test_lombscargle_atan_vs_atan2M  s   z.TestLombscargle.test_lombscargle_atan_vs_atan2N)
r   r   r   rd  rh  rk  rn  rp  rr  rt  r*   r*   r*   r+   rT    s     !rT  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zej	
dddgdd Zdd Zdd Zdd Z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	   rP   r   r*   r*   r+   chk_VEY  s   z.TestSTFT.test_input_validation.<locals>.chk_VEz"nperseg must be a positive integerrg   ir   z#noverlap must be less than nperseg.rW   rT   zwindow must be 1-D)rV   rV   z"window must have length of npersegz"noverlap must be less than npersegz&noverlap must be a nonnegative integerr   ir$  rV   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!Z	time_axisZ	freq_axisz=Unknown value for mode foo, must be one of: \{'psd', 'stft'\}rN   )modez'x and y must be equal if mode is 'stft'   r   z[Unknown boundary option 'foo', must be one of: \['even', 'odd', 'constant', 'zeros', None\])boundaryZ	not_validzParameter scaling=z not in \['spectrum', 'psd'\]!r=   )r   r    r#   r   r!   r   r   r   warnsr   r   )r%   rv  r&   zr>   r*   r*   r+   test_input_validationW  s   
























 
$zTestSTFT.test_input_validationc                 C   s2   g d}|D ]}dj | }tdt| |d qd S )N)boxcarrW   r   r  rW   r   r  3      rg   r      rg   r      blackmanr     r  r   r   r   rg   r      
{}, {}, {}Tr%  )formatr   r   )r%   settingssettingmsgr*   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  rW   r   r  )r  r  rW   r  r  )rg   r   %   r  )r  r  {   r  )r  r   &   r  )rg   r   '   r  Tr%  r   r   rV   )rg   r   r   F)r  r   r   r    r#   len)r%   Zsettings_passr  r  w_failZsettings_failr*   r*   r+   test_check_NOLA  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   rV   r   rc   )	r    r'  r(  r)  r   r   r   rD  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   rx  )	r    r'  r(  r)  r   r\   r   Tr   )r%   r&   r  r   r   r   r   t1ZZ1r   t2ZZ2t3x1Zt4Zx2r*   r*   r+   test_permute_axes  s&   



zTestSTFT.test_permute_axesr>   r<   psdc              
   C   s   t jd g d}|D ]B\}}}}t |}dt j|j }t||||d d|d\}	}	}
t|
||||d\}}| d| }t|||d t|||d qd S )	Nr#  r  rU  rW   r   r  rU  rW   r   )r  e   r  r  rg   r$  r   r  )r    r   r   )rg   r$  r   r  rW   F)r   r   r   r   r  r>   )r   r   r   r>   r  r%  	r    r'  r(  r4   r)  sizer   r   r   )r%   r>   r  r   Nr   r   rb  r&   r  zztrxrr  r*   r*   r+   test_roundtrip_real  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 ]{\}}}}| d| d| 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 sxw   Y  t ||d t| sJ |t |	|d t|	 rJ |q"d S )Nr#  r   r   rV   r   )rg   r   r   r   r  rW   Tr!   r   r   r   r   r  r{  ZNOLAr   r   r   r   r{  )r    r'  r(  r#   r  r   r4   r)  r  r   r   r|  r   r   Zallclose)r%   r  r  r   r  r   r   r  rb  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 ]l\}}}}| d| d| }t|||s&J |t|||r0J |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  rU  rW   r{   )r  r  r  r  )rg   r$  r      )r  r  r      )rg   r$  r   r}   r  rW   Tr!   r  r  r%  )r    r'  r(  r   r   r4   r)  r  r   r   r   r  )r%   r  r   r  r   r   r  rb  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 ]P\}}}}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  rW   Fr   r   r   r   r  rw  r  r%  g-C6?gh㈵>)r&  r   r3   )r    r'  r(  r4   r)  r  ZastypeZfloat32r   r   r   r   rC   r%   r  r   r  r   r   rb  r&   r  r  r  r  r  r*   r*   r+   test_roundtrip_float32X  s"   

zTestSTFT.test_roundtrip_float32c                 C   sV  t jd g d}|D ]P\}}}}t |}dt j|j dt j|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 sw   Y  t|
|||d|d\}}| d| d| }t|||d	 t|||d	 d S )Nr#  r  rW   y              $@F)r   r   r   r   r  r7   r>   )r   r   r   input_onesidedr>   r  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   rb  r&   r  r  r  r  r  r   r*   r*   r+   test_roundtrip_complexm  s>   	
$

zTestSTFT.test_roundtrip_complexc                 C   s   t jd ddg}|D ]a\}}}}t |}dt j|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 q:qd S )Nr#  r  r  rW   Tr  F)r   r   r{  )evenZoddZconstantr!   r  r%  r  )r%   r  r   r  r   r   rb  r&   r  r  r  r{  Zzz_extZ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 ]I\}}}}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  rW   r   )rg   rV  r   r  rW   Tr  )r   r   r  r%  r  r  r*   r*   r+   test_roundtrip_padded_signal  s   
z%TestSTFT.test_roundtrip_padded_signalc                 C   s   t jd g d}|D ]q\}}}}}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  rz  )rg   r$  r   r  i  )r  rU  rW   r   !   )r  r  r   r   r$  rW   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   rq   rb  r&   Zxcr  r}  Zzcr  r  Zxcrr  r*   r*   r+   test_roundtrip_padded_FFT  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%  rx  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+   r0    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  rB   r$  r   rV   r  r<   )r{  r>   Tr   ?   Nr   r   r   A   r   B   r2   Fr  )r7   r{  r>   rc   )r  r{  r>   r     )r    r!   complexZfftZirfftsumr  r   r   r   r  ZfinforC   
resolutionrealimagr]   Zconj)r%   Xr&   Zpower_xZsr  ZZpZpsd_ZpZZp0ZZp1s2Zpsd_Zp01r*   r*   r+   test_roundtrip_scaling  s8   
((


zTestSTFT.test_roundtrip_scalingN)r   r   r   r~  r  r  rR  r  r   r   Zparametrizer  r  r  r  r  r  r  r  r0  r  r*   r*   r*   r+   ru  V  s$    R

+!#ru  )&r   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   r5  rG  rM  rT  ru  r*   r*   r*   r+   <module>   s,    $0 ^  @  M4 