a
    Re                     @   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*   T/var/www/sunrise/env/lib/python3.9/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   s    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   s    zTestPeriodogram.test_complexc                 C   s   t tttdtjdd d S )N   foor=   )assert_raises
ValueErrorr   r    r!   rJ   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   rU   r      r   <   )	r    r!   float64reshaper   r   shaper   newaxisr%   r&   r'   r(   Zf0Z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 )
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_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   rV   hann    )r    r!   r   r
   
get_windowr   rN   rO   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   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   s\   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   s    z TestPeriodogram.test_empty_inputc                 C   s<   dD ]2}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   s    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   rn   r   r   r   r   r   r   r$   r*   r*   r+   test_short_nfft   s    

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   rn   r   r   r   r   r   r   r$   r*   r*   r+   test_nfft_is_xshape   s    

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   s    
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   FrH   r   Fr6   r8   rI   r'   r   r$   r*   r*   r+   test_complex_32   s    
zTestPeriodogram.test_complex_32c                 C   s^   t d}d|d< tdd}d}tt|d t||d W d    n1 sP0    Y  d S )	Nr   r   r   re   rV   zOthe size of the window must be the same size of the input on the specified axismatchwindow)r    r!   r
   rg   rN   rO   r   )r%   r&   ri   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   rK   rQ   pytestmarkZskipifsysmaxsizer_   rd   rm   rs   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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,      s    
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      s    
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     s    
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  s    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!  s    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   +  s    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   rH   r   r   Fr   r8   )g&>?#q?n)t?r   r   r   r   r   r   r   )r    r!   rJ   r   r   r   r   r$   r*   r*   r+   rK   5  s    zTestWelch.test_complexc                 C   s    t tttdtjddd d S NrL   rM   )r>   r   )rN   rO   r   r    r!   rJ   rP   r*   r*   r+   rQ   ?  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_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 )
NrV   rB   r   Fr   c                 S   s   | S Nr*   r&   r*   r*   r+   <lambda>K      z.TestWelch.test_no_detrending.<locals>.<lambda>r1   r2   )r    r4   rZ   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 )	NrV   rB   r   c                 S   s   t j| ddS Nl)typer
   r   segr*   r*   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   rU   rU   rV   rV   c                 S   s   t j| ddS r   r   r   r*   r*   r+   r   Y  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_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 )NrS   rB   r   rT   rU   r   rV   c                 S   s   t j| dddS Nr   r   )rb   r   r   r   r*   r*   r+   r   a  r   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_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 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_   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 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   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   rV   re   r   rz   rL   rf   )
r    r!   r   r
   rg   r   r   r\   rN   rO   rh   r*   r*   r+   rm   v  s"    


zTestWelch.test_window_externalc                 C   s\   t g \}}t|jd t|jd dD ].}t t|\}}t|j| t|j| q(d S rt   )r   r   r\   r    r{   r|   r*   r*   r+   r}     s    zTestWelch.test_empty_inputc                 C   s<   dD ]2}t t|dd\}}t|j| t|j| qd S r~   )r   r    r{   r   r\   r   r*   r*   r+   r     s    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\0    Y  t|dd	\}}t|| t|| t|| t|| d S 
Nr   r   r   zBnperseg = 256 is greater than input length  = 8, using nperseg = 8re   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    
0


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 NrL   r   )r   r   r   r   r   rX   )rU   rx   )rN   rO   r   r    r!   r   r4   r[   rP   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   rL   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 NrL   r   re   rU      )rN   rO   r   r    r!   rP   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   rL   )ro   r   )rN   rO   r   r    r#   rP   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     s    
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     s    
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   rH   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   rU   r   r   rz   r   ro   rX      )r    r!   r   r   r   )r%   r&   ro   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 ]p}t||||dd\}	}
t||||d	d\}}t|
| |d d
  ttt|||td d dd qVd 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_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 ]p}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   median)r   averager   rz   )r  g?        g&E    9r   r   r   zunrecognised-average)r    r!   r   r   r"   r   rN   rO   r$   r*   r*   r+   test_average  s    

zTestWelch.test_averageN)#r   r   r   r,   r5   r;   rA   r   r   rG   rK   rQ   r   r   r   r   r   r_   rd   rm   r}   r   r   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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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"   rJ   r   r   r%   r&   yr'   cr   Zc1r*   r*   r+   test_pad_shorter_x-  s    


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_y8  s    


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  s    
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  s    
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  s    z"TestCSD.test_integer_onesided_evenc                 C   sf   t jdtd}d|d< d|d< t||dd\}}t|t dd	  t g d
}t||ddd d S r   )r    r!   rD   r   r   r4   r   r$   r*   r*   r+   r   u  s    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     s    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!   rJ   r   r   r   r   r$   r*   r*   r+   rK     s    zTestCSD.test_complexc                 C   s,   t tttdtjtdtjddd d S r   )rN   rO   r   r    r!   rJ   r#   rP   r*   r*   r+   rQ     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   s   | S 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   s   t j| ddS r   r   r   r*   r*   r+   r     r   z/TestCSD.test_detrend_external.<locals>.<lambda>r   r1   r2   r*  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   s   t j| ddS 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   s   t j| dddS 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\   rN   rO   rh   r*   r*   r+   rm     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 ]6}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 B}|td t||dd\}}t||ddd\}}W d    n1 s`0    Y  t||dd	\}}t|| t|| t|| t|| d S r   )r    r!   r   r   r   r   r   r   r*   r*   r+   r     s    
2


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   )	rN   rO   r   r    r!   r#   r   r4   r[   rP   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   )rN   rO   r   r    r!   r#   rP   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   )rN   rO   r   r    r#   r!   rP   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   %  s    
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  s    
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   r+  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&   r%  ro   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   r  r7   r  )r    r  r  copyr   r   )r%   r&   r%  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   rK   rQ   r   r   r   r   r   r_   rd   rm   r}   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  r-  r"   r#   r   r   r%   r&   r%  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 r0  )r    r  r  r"   r#   r   r   r1  r*   r*   r+   test_phase_shifted_input  s    

z&TestCoherence.test_phase_shifted_inputN)r   r   r   r3  r4  r*   r*   r*   r+   r/  w  s   r/  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestSpectrogramc                 C   sh   t jd}d}d}d}d}t|||||\}}}t|||||\}	}
t||	 tt j|dd|
 d S )Nr  r8   r  g      ?r   rU   r   ra   )r    r  r  r   r   r   r,  )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   r6  r   rU   r   )r   )r   I   r   r   i   )
r    r  r  r   r
   rg   r   r\   rN   rO   )r%   r&   r  r   r   r   r'   r   r7  ri   rj   ZPerl   r*   r*   r+   rm     s"    z$TestSpectrogram.test_window_externalc                 C   s   t jd}d}t||dd\}}}t 2}|td t||ddd\}}}W d    n1 sb0    Y  t||dd	\}	}}
t||dd	\}}}t||	 t||
 t|| t|| d S )
Nr  r8   r6  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:  rm   r   r*   r*   r*   r+   r5    s   r5  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'   r7  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   r=  r>  r?  r@  rA  rB  rL   r   rU   Zsignificant)r    r	  r  r  rC  r"   r  r   r
  r\   r   max)r%   rD  rE  rF  rG  rH  r(   rI  rJ  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   r=  r>  r?  g333333?r@  rA  rB  T)Z	precenterF)
r    r	  r  r  rC  r"   r  r   r,  r   )r%   rD  rE  rF  rG  rH  r(   offsetrI  rJ  r&   r'   rO  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   r=  r>  r?  r@  rA  rB  T)	normalizerU   rM  )r    r	  r  r  rC  r"   r  r   r   dotr   rN  )r%   rD  rE  rF  rG  rH  r(   rI  rJ  r&   r'   rO  rR  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"   rN   rO   r   r%   rJ  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!   rN   ZeroDivisionErrorr   rW  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   rV   r>  F)ZendpointrL   2   i  r  rU   )r    r"   r  r   r	  rW  r*   r*   r+   test_lombscargle_atan_vs_atan2M  s    z.TestLombscargle.test_lombscargle_atan_vs_atan2N)
r   r   r   rL  rP  rS  rV  rX  rZ  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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   s:  dd }|d t ddd W d    n1 s20    Y  |d t ddd	 W d    n1 sf0    Y  |d
" t tddd W d    n1 s0    Y  |d" t td	dd W d    n1 s0    Y  |d tddd W d    n1 s0    Y  |d tddd	 W d    n1 sF0    Y  |d
" ttddd W d    n1 s0    Y  |d" ttd	dd W d    n1 s0    Y  |d tddd W d    n1 s0    Y  td}t|d }|d
" t|tdd W d    n1 sF0    Y  |d$ t|tddd W d    n1 s0    Y  |d t|dd W d    n1 s0    Y  |d t|ddd W d    n1 s0    Y  |d t|ddd W d    n1 s*0    Y  |d t| W d    n1 s\0    Y  |d
" t|tdd W d    n1 s0    Y  |d$ t|tddd W d    n1 s0    Y  |d t|dd W d    n1 s0    Y  |d t|ddd W d    n1 sD0    Y  |d t|ddd W d    n1 s|0    Y  tjt	dd   t|dddd! W d    n1 s0    Y  |d" t|ddd# W d    n1 s0    Y  |d$ t
||d%d& W d    n1 s,0    Y  |d'. t
|d d( |d(d  d)d& W d    n1 st0    Y  |d* t
||d%d+ W d    n1 s0    Y  d,}|d-|d. t||d/ W d    n1 s0    Y  |d-|d. t||d/ W d    n1 s,0    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	   rO   r   r*   r*   r+   chk_VEY  s    z.TestSTFT.test_input_validation.<locals>.chk_VEz"nperseg must be a positive integerre   ir   z#noverlap must be less than nperseg.rV   rS   zwindow must be 1-D)rU   rU   z"window must have length of npersegz"noverlap must be less than npersegz&noverlap must be a nonnegative integerr   ir  rU   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'\}rM   )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%   r^  r&   zr>   r*   r*   r+   test_input_validationW  st    
*
*
0
0
,
,
2
2
,

2
4
,
.
.
(
2
4
,
.
.0
.
.
>
.,zTestSTFT.test_input_validationc                 C   s2   g d}|D ] }dj | }tdt| |d qd S )N)boxcarrV   r   rh  rV   r   r  3      re   r      re   r      blackmanr     r  r   r   r   re   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 qdd S )
N)rg  ri  )rh  rV   r   rj  )r  rk  rV   rm  ro  )re   r   %   rq  )rr  r  {   rt  )ru  r   &   rv  )re   r   '   rx  Tr  r   r   rU   )re   r   r   F)ry  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   re   r   r   F)paddedr7   rc  r<   )r7   r>   r   rU   r   ra   )	r    r  r  r  r   r   r   r,  abs)r%   r&   r  r   r   r   r'   r   Zr8  r9  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   r`  )	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 ]\}}}}t |}dt j|j }t||||d d|d\}	}	}
t|
||||d\}}| d| }t|||d t|||d qd S )	Nr  rh  r=  rV   r   rh  r=  rV   r   )r  e   rk  rl  re   r  r   rn  )ru    r   r   )re   r  r   rw  rV   F)r   r   r   r   r  r>   )r   r   r   r>   r   r  	r    r  r  r4   r  sizer   r   r   )r%   r>   rz  r   Nr   r   rJ  r&   r   zztrxrr|  r*   r*   r+   test_roundtrip_real  s    	

zTestSTFT.test_roundtrip_realc              
   C   sB  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 s0    Y  t ||d t| sJ |t |	|d t|	 rDJ |qDd S )Nr  r   r   rU   r   )re   r   r   r   r   rV   Tr!   r   r   r   r   r  rc  ZNOLAr   r   r   r   rc  )r    r  r  r#   r  r   r4   r  r  r   r   rd  r   r   Zallclose)r%   r  rz  r   r  r   r   r|  rJ  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 ]\}}}}| d| d| }t|||sLJ |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  ))rh  r=  rV   rx   )r  r  rk  r~  )re   r  r      )ru  r  r      )re   r  r   rz   r   rV   Tr!   r  r  r  )r    r  r  r   r   r4   r  r  r   r   r   r  )r%   rz  r   r  r   r   r|  rJ  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 ]\}}}}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   Fr   r   r   r   r  r_  r   r  g-C6?gh㈵>)r  r   r3   )r    r  r  r4   r  r  Zastypefloat32r   r   r   r   rC   r%   rz  r   r  r   r   rJ  r&   r   r  r  r  r|  r*   r*   r+   test_roundtrip_float32X  s     

zTestSTFT.test_roundtrip_float32c                 C   sX  t jd g d}|D ]\}}}}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 s0    Y  t|
|||d|d\}}| d| d| }t|||d	 t|||d	 d S )Nr  r  rV   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>   rz  r   r  r   r   rJ  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 ]\}}}}t |}dt j|j }t||||d dd d\}}}	t|	||dd\}}
d	D ]d}t||||d d|d\}}}t|||dd\}}| d
| d
| }t||
|d t|||d qtqd S )Nr  r  r  rV   Tr  F)r   r   rc  )evenZoddZconstantr!   r   r  r  )r%   rz  r   r  r   r   rJ  r&   r   r  r  rc  Zzz_extZxr_extr|  r*   r*   r+   !test_roundtrip_boundary_extension  s.    

z*TestSTFT.test_roundtrip_boundary_extensionc              	   C   s   t jd ddg}|D ]\}}}}t |}dt j|j }t||||d dd\}}}	t|	||d\}
}| d| }t||
d |j |d	 t||d |j |d	 qd S )
Nr  )rh  r  rV   r   )re   r>  r   rn  rV   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 ]\}}}}}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   rn  rb  )re   r  r   rn  i  )rh  r=  rV   r   !   )ru  r  r   r   r  rV   y              ?rL   T)r   r   ro   r   r   r  F)r   r   ro   r   r   r  r7   )r   r   ro   r   )r   r   ro   r   r  r   r  )r    r  r  r4   r  r  expr	  r   r   r   )r%   rz  r   r  r   r   ro   rJ  r&   Zxcr   re  Zzcr  r  Zxcrr|  r*   r*   r+   test_roundtrip_padded_FFT  s.    






z"TestSTFT.test_roundtrip_padded_FFTc                 C   s   t jd t jd}t|\}}}tdD ]t}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  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z_flatr  r  r&   Zz_plusZz_minusZx_transpose_mZx_transpose_pr*   r*   r+   r    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   rU   r  r<   )rc  r>   Tr   ?   Nr   r   r   A   r   B   r2   Fr  )r7   rc  r>   ra   )r  rc  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   rf  r}  r  r:  r  r   r   Zparametrizer  r  r  r  r  r  r  r  r  r  r*   r*   r*   r+   r]  V  s"   R

+!#r]  )&r   numpyr    Znumpy.testingr   r   r   r   r   r   r   r   r	   rN   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"  r/  r5  r<  r]  r*   r*   r*   r+   <module>   s*   $0 ^  @  M4 