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






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

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

"z"TestSTFT.test_average_all_segmentsc              	   C   s   t jd}|d}d}d}d}d}t|||||\}}}	t|d||||dd	\}
}}t|	||||\}}t|j||||dd
d\}}t||
 t|| t|| t|	|d d ddd d f  t||d d ddf  d S )Nr'  r(  r<   r   r   r"   )r    r   r   r   rg   r    r  )	r$   r+  rc  r-  r  r`   r  Tr   )r)   rk  r*   r  r   r   r   r   t1ZZ1r   t2ZZ2Zt3x1Zt4Zx2r.   r.   r/   test_permute_axesN  s&   


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


z"TestSTFT.test_roundtrip_padded_FFTc                 C   s   t jd}|d}t|\}}}tdD ]:}dgd }d||< ||}t||d\}}}t|||j d\}}}	t||	 |d t||		 ||j d qt
|jddd	\}}
t
|jd
dd	\}}t||
dd t||dd d S )Nr'  r(  r}   r   r    rg   r)  r  r   zistft transpose minuszistft transpose plus)r$   r+  rc  r-  r  r.  r`   r/  r   r0  r  r  r   )r)   rk  r1  r  Zz_flatr2  r3  r*   Zz_plusZz_minusZx_transpose_mZx_transpose_pr.   r.   r/   r4  W  s   


zTestSTFT.test_axis_rollingc                 C   s6  t jdtd}d|d< t j|}t|d t| }t|dddd }t|d	ddd
 }t	|| t	t
|dddf d t	t
|dddf d
 t	t
|dddf d d|ddddf< t	|ddddf dt |jjd t|ddddd }t j|jd |jd  dd|jd  }t	|| t|dd	ddd
 }t	|| t|d	dddd }t |dddddf }	t	|ddddf | t	|ddddf |	 t j|jd |jd  ddt j|	jd |	jd  dd }
|
|jd |	jd   }t	|| t|d	d	ddd
 }t	|| dS )z&Verify behavior of scaling parameter. i  rF   r(  r   rZ   r  r@   )r  rB   Tr   ?   Nr    r   r   A   r   B   r6   Fr  )r;   r  rB   rg   )r  r  rB   r     )r$   r%   complexr   r   rz  r  r  r  r   r  finforG   
resolutionrealimagra   Zconj)r)   Xr*   Zpower_xZsr  ZZpZpsd_ZpZZp0ZZp1s2Zpsd_Zp01r.   r.   r/   test_roundtrip_scalingq  s8   
((


zTestSTFT.test_roundtrip_scalingN)r   r   r   r   r   Zthread_unsafer  r  r  rX  r  Zparametrizer  r  r  r  r  r  r  r  r4  r  r.   r.   r.   r/   r    s*    
R

+!#r  c                   @   s   e Zd ZU dZdZeed< dZeed< dZ	eed< dZ
eed< ejed	< ejed
< eed< eed< dd ZedefddZedefddZdd Zdd ZdS )"TestSampledSpectralRepresentationsa  Check energy/power relations from `Spectral Analysis` section in the user guide.

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

        Furthermore, the scalings of `periodogram` and `welch` are verified.
        F)ZsymrZ   r   )r  r   r   r;   rB   r@   rC   r6   r   r   Nr.   )r   r  r  rz  r$   r!  r  r  r   r   r   r  r  r  r  r  r  r  dictr   r  rG   r  r{  r  r   )r)   rg  Zc_ampZc_rmsZXwZX_ESDZX_PSDkwr  ZP_magZP_psdZ	float_resr.   r.   r/   test_windowed_DFT  s,   ,&"


z4TestSampledSpectralRepresentations.test_windowed_DFTN)r   r   r   __doc__r  rH   __annotations__r  ry  r  r  r$   Zndarrayr  propertyr  r  r	  r  r.   r.   r.   r/   r    s"   
 

r  )1r   numpyr$   Znumpy.testingr   r   r   r   r   r   r   r   rS   Zscipyr	   Z	scipy.fftr
   r   r   r   Zscipy.integrater   Zscipy.signalr   r   r   r   r   r   r   Zscipy.signal.windowsr   Zscipy.signal._spectral_pyr   Z,scipy.signal.tests._scipy_spectral_test_shimr   r  r   r  r   r<  r   r   r:  rM  rS  rZ  r  r  r.   r.   r.   r/   <module>   sB     $ ^  A  N5   m   