a
    BCCf                     @   sL  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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Z0G dd dZ1dS )    N)assert_assert_approx_equal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/   \/var/www/html/django/DPS/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/   r0   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/   r0   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/   r0   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/   r0   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 )Nr2   rG   r   r   r3   r4   r#   r5   r6   r7   )r%   r&   rI   r   r   r9   r(   r)   r/   r/   r0   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   rG   r   r   Fr;   r=   r>   )r%   r&   rI   r   r   r   r?   r)   r/   r/   r0   test_integer_twosidedT   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   Fr;   r=         ?)r%   r&   
complex128r   r   r   r?   r)   r/   r/   r0   test_complex]   s    zTestPeriodogram.test_complexc                 C   s   t tttdtjdd d S )N   foorB   )assert_raises
ValueErrorr   r%   r&   rO   r*   r/   r/   r0   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   rG      r   
   r=   r   rZ   r      r   <   )	r%   r&   float64reshaper   r   shaper   newaxisr*   r+   r,   r-   Zf0p0r/   r/   r0   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   rG   r[   rZ   r   r=   r   axisr]   rZ   r   r   r^   )r%   r&   r_   r`   r   r   ra   r   rc   r/   r/   r0   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fepewin_errr/   r/   r0   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/   r0   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   rz      r   r      rZ   )r   r   ra   r%   emptyr*   r,   r-   ra   r/   r/   r0   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r|   r~   r   rg   )r   r%   r   r   ra   r*   ra   r,   r-   r/   r/   r0   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   rs   r   r    r!   r"   r#   r$   r)   r/   r/   r0   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   rs   r   r    r!   r"   r#   r$   r)   r/   r/   r0   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   rH   r)   r/   r/   r0   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 )Nr2   r,   r   r   r3   r4   r#   r5   Hz>r7   )r%   r&   r   r   r9   r(   r   rH   r)   r/   r/   r0   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   rH   r)   r/   r/   r0   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   FrM   r   Fr;   r=   rN   r,   r   r)   r/   r/   r0   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   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/   r0   test_shorter_window_error   s    
z)TestPeriodogram.test_shorter_window_errorN)__name__
__module____qualname__r1   r:   r@   rF   rJ   rK   rL   rP   rV   pytestmarkZskipifsysmaxsizere   rj   rr   rx   r   r   r   r   r   r   r   r   r   r/   r/   r/   r0   r      s<   
	
		



		

r   c                   @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAS )B	TestWelchc                 C   s`   t d}d|d< d|d< t|dd\}}t|t ddd t g d}t||d	d	d
 d S Nr   r   r   r#   npersegr   r   iKGUU?gS8?#q?r   #q?r   r8   rtolr%   r&   r   r   r'   arrayr)   r/   r/   r0   r1      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   r9   r   r)   r/   r/   r0   r:      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   r<   r=   r   S8?r   r   r   r   r   r   r   r   )r%   r&   r   r   r   r   r)   r/   r/   r0   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#   rA   )r   rC   r   r   )g      ?g-UU?.iqUU?r   g-UU?r   r   r   r)   r/   r/   r0   rF     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   rG   r   r   r#   r   r   r   r   r   r   )r%   r&   rI   r   r   r'   r   r)   r/   r/   r0   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   rG   r   r   r#   r    r   r   r   r   r   r   )r%   r&   rI   r   r   r9   r   r)   r/   r/   r0   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   rG   r   r   r#   Fr   r=   r   r   r   )r%   r&   rI   r   r   r   r   r)   r/   r/   r0   rL   2  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   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/   r0   rP   <  s    zTestWelch.test_complexc                 C   s    t tttdtjddd d S NrQ   rR   )rC   r   )rS   rT   r   r%   r&   rO   rU   r/   r/   r0   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[   rG   {Gz?Zlinearr   detrendr6   r7   r%   r9   r_   r   r   
zeros_liker*   r+   r,   r-   r/   r/   r0   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[   rG   r   Fr   c                 S   s   | S Nr/   r+   r/   r/   r0   <lambda>R      z.TestWelch.test_no_detrending.<locals>.<lambda>r6   r7   )r%   r9   r_   r   r   r*   r+   f1p1f2p2r/   r/   r0   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[   rG   r   c                 S   s   t j| ddS Nl)typer
   r   segr/   r/   r0   r   Y  r   z1TestWelch.test_detrend_external.<locals>.<lambda>r   r6   r7   r   r   r/   r/   r0   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(   rG   r   rZ   rZ   r[   r[   c                 S   s   t j| ddS r   r   r   r/   r/   r0   r   `  r   z7TestWelch.test_detrend_external_nd_m1.<locals>.<lambda>r   r6   r7   )r%   r9   r_   r`   r   r   r   r   r/   r/   r0   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   rG   r   rY   rZ   r   r[   c                 S   s   t j| dddS Nr   r   )rh   r   r   r   r/   r/   r0   r   h  r   z6TestWelch.test_detrend_external_nd_0.<locals>.<lambda>r   rh   r   r6   r7   )r%   r9   r_   r`   moveaxisr   r   r   r   r/   r/   r0   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   rG   r   rY   r[   r   r\   r   r   vIh%<=r   )	r%   r9   r_   r`   r   r   ra   r   rb   rc   r/   r/   r0   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   rG   r   rf   r[   r   )r   rh   ri   r   r   r   r   )r%   r9   r_   r`   r   r   ra   r   rc   r/   r/   r0   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/   r0   rr   }  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 ry   )r   r   ra   r%   r   r   r/   r/   r0   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   ra   r   r/   r/   r0   r     s    z%TestWelch.test_empty_input_other_axisc           
      C   s   t d}d|d< t B}d}|t| 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 = 8r   r      r   r   r   )r%   r&   r   filterUserWarningr   r   
r*   r+   supmsgr,   r-   r   r   r   r   r/   r/   r0   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 NrQ   r   )r   r   r   r   r   r]   )rZ   r}   )rS   rT   r   r%   r&   r   r9   r`   rU   r/   r/   r0   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=r7   )r%   r&   r   r   r   r)   r/   r/   r0   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/   r0   test_bad_noverlap  s    zTestWelch.test_bad_noverlapc                 C   s   t tttdddd d S N   r}   rQ   )rt   r   )rS   rT   r   r%   r(   rU   r/   r/   r0   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   rH   r)   r/   r/   r0   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   r9   r   r   rH   r)   r/   r/   r0   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   r=   r   r   r   r%   r&   r   r   r   r   r   rH   r)   r/   r/   r0   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/   r0   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   rt   r]      )r%   r&   r   r   r   )r*   r+   rt   r,   fodd_fevenr/   r/   r0   test_padded_freqs  s    



zTestWelch.test_padded_freqsc                 C   s   d}d}t |d }d}t || | }t|| }|tdtj | |  }dD ]n}t||||dd\}	}
t||||d	d\}}t|
| |d d
  ttt|||td d dd qVd S )NrX   g     @r[   ,  rZ   )r   bartlett)tukey皙?ZflattoprA   )fsr   r   rC   rD   r"   gMbP?r   )	rI   r%   r9   sinpir   r   sqrtr   )r*   Ar  r   Zfsigiittr+   r   r  Zp_specfreqZp_densr/   r/   r0   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     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/   r0   test_axis_rolling  s    

zTestWelch.test_axis_rollingc                 C   st   t d}d|d< d|d< t|ddd\}}t|t ddd t g d	}t||d
d
d ttt|ddd d S )Nr   r   r   r#   median)r   averager   r   )r  g?        g&E    9r'  r   r   zunrecognised-average)r%   r&   r   r   r'   r   rS   rT   r)   r/   r/   r0   test_average'  s    

zTestWelch.test_averageN)#r   r   r   r1   r:   r@   rF   r   r   rL   rP   rV   r   r   r   r   r   re   rj   rr   r   r   r   r   r   r   r   r   r   r   r   r  r  r$  r(  r/   r/   r/   r0   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   rG   r   r%   r&   r'   rO   csdr   r*   r+   yr,   cr   c1r/   r/   r0   test_pad_shorter_x5  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   rG   r   r*  r,  r/   r/   r0   test_pad_shorter_y@  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/   r0   r1   K  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   r9   r   r)   r/   r/   r0   r:   U  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/   r0   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   r2  r)   r/   r/   r0   rF   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&   rI   r+  r   r'   r   r)   r/   r/   r0   r   s  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&   rI   r+  r   r9   r   r)   r/   r/   r0   r   }  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&   rI   r+  r   r   r   r)   r/   r/   r0   rL     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&   rO   r+  r   r   r   r)   r/   r/   r0   rP     s    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/   r0   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%   r9   r_   r+  r   r   r   r/   r/   r0   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[   rG   r   Fr   c                 S   s   | S r   r/   r   r/   r/   r0   r     r   z,TestCSD.test_no_detrending.<locals>.<lambda>r6   r7   )r%   r9   r_   r+  r   r   r/   r/   r0   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[   rG   r   c                 S   s   t j| ddS r   r   r   r/   r/   r0   r     r   z/TestCSD.test_detrend_external.<locals>.<lambda>r   r6   r7   r3  r   r/   r/   r0   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   rG   r   r   r[   c                 S   s   t j| ddS r   r   r   r/   r/   r0   r     r   z5TestCSD.test_detrend_external_nd_m1.<locals>.<lambda>r   r6   r7   )r%   r9   r_   r`   r+  r   r   r   r/   r/   r0   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   rG   r   rY   rZ   r   r[   c                 S   s   t j| dddS r   r   r   r/   r/   r0   r     r   z4TestCSD.test_detrend_external_nd_0.<locals>.<lambda>r   r6   r7   )r%   r9   r_   r`   r   r+  r   r   r   r/   r/   r0   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%   r9   r_   r`   r+  r   ra   r   rb   rc   r/   r/   r0   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%   r9   r_   r`   r+  r   ra   r   rc   r/   r/   r0   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/   r0   rr     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 )Nr[   rz   r{   )r   r   )r+  r%   r&   r   ra   r   r(   r   r/   r/   r0   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/   r0   r     s    z#TestCSD.test_empty_input_other_axisc           
      C   s   t d}d|d< t F}d}|t| t||dd\}}t||ddd\}}W d    n1 sd0    Y  t||dd	\}}	t|| t||	 t|| t||	 d S r   )r%   r&   r   r   r   r+  r   r   r/   r/   r0   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   )	rS   rT   r+  r%   r&   r(   r   r9   r`   rU   r/   r/   r0   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/   r0   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/   r0   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/   r0   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   rH   r)   r/   r/   r0   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   r9   r   r   rH   r)   r/   r/   r0   r   9  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   rH   r)   r/   r/   r0   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   r4  r)   r/   r/   r0   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-  rt   r,   r  r  r  r/   r/   r0   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   r&  r<   r%  )r%   r  r  copyr+  r   )r*   r+   r-  r  Zp_sameZp_copiedr/   r/   r0   test_copied_datao  s     








zTestCSD.test_copied_dataN)#r   r   r   r0  r1  r1   r:   r@   rF   r   r   rL   rP   rV   r   r   r   r   r   re   rj   rr   r   r   r   r   r   r   r   r   r   r   r   r  r7  r/   r/   r/   r0   r)  4  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 NrX   r   r   r]   r[   r   )r%   r  r  r6  r'   r(   r   r   r*   r+   r-  r,   Cr   ZC1r/   r/   r0   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 r9  )r%   r  r  r'   r(   r   r   r:  r/   r/   r0   test_phase_shifted_input  s    

z&TestCoherence.test_phase_shifted_inputN)r   r   r   r<  r=  r/   r/   r/   r0   r8    s   r8  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   r5  )r*   r+   r  r   r   r   r,   r  PfwPwr/   r/   r0   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=   r?  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  r@  rn   ro   ZPerq   r/   r/   r0   rr     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  r=   r?  r   zInperseg = 1025 is greater than input length  = 1024, using nperseg = 1024i  r   r   r   )r%   r  r  r   r   r   r   r   )r*   r+   r  r,   r  r-   r   r   r   r   r   Zf3Zp3r/   r/   r0   r     s     *


zTestSpectrogram.test_short_dataN)r   r   r   rC  rr   r   r/   r/   r/   r0   r>    s   r>  c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )TestLombscarglec                 C   s   d}d}dt j }d}d}d}t jd t j|}t dt j d	t j |||k }|t || |  }	t dd	|}
t||	|
}|
d
 |
d  }t||
t 	|  |d k  dS )zgTest if frequency location of peak corresponds to frequency of
        generated input signal.
        r"   r=   r   d     ffffff?# {Gz?      $@r   r   N)
r%   r  r  r  randr'   r  r   r   Zargmax)r*   amplwphininnoutr-   rtr+   r,   r@  deltar/   r/   r0   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"   r=   r   rF  rG  rH  rI  rJ  rK  rQ   r   rZ   Zsignificant)r%   r  r  r  rL  r'   r  r   r  ra   r   max)r*   rM  rN  rO  rP  rQ  r-   rR  rS  r+   r,   pgramr/   r/   r0   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"   r=   r   rF  rG  rH  g333333?rI  rJ  rK  T)Z	precenterF)
r%   r  r  r  rL  r'   r  r   r5  r   )r*   rM  rN  rO  rP  rQ  r-   offsetrR  rS  r+   r,   rX  pgram2r/   r/   r0   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"   r=   r   rF  rG  rH  rI  rJ  rK  T)	normalizerZ   rV  )r%   r  r  r  rL  r'   r  r   r   dotr   rW  )r*   rM  rN  rO  rP  rQ  r-   rR  rS  r+   r,   rX  r[  r/   r/   r0   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   rZ   r}   )r%   r'   rS   rT   r   r*   rS  r+   r,   r/   r/   r0   test_wrong_shapeK  s    z TestLombscargle.test_wrong_shapec                 C   s2   t d}t d}t d}ttt||| d S )Nr   )r%   r&   rS   ZeroDivisionErrorr   r`  r/   r/   r0   test_zero_divisionQ  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   r[   rG  F)ZendpointrQ   2   i  r  rZ   )r%   r'   r  r   r  r`  r/   r/   r0   test_lombscargle_atan_vs_atan2W  s    z.TestLombscargle.test_lombscargle_atan_vs_atan2N)
r   r   r   rU  rY  r\  r_  ra  rc  re  r/   r/   r/   r0   rE    s    !rE  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	   rT   r   r/   r/   r0   chk_VEc  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'\]!rB   )r   r%   r(   r   r&   rl  istftr   warnsr   r   )r*   rg  r+   zrC   r/   r/   r0   test_input_validationa  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)boxcarr[   r   rs  r[   r    r
  3      r   r      r   r      blackmanr	     r  r   r   r   r   r      
{}, {}, {}Tr  )formatr   r   )r*   settingssettingr   r/   r/   r0   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)rr  rt  )rs  r[   r   ru  )r
  rv  r[   rx  rz  )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(   len)r*   Zsettings_passr  r   w_failZsettings_failr/   r/   r0   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  r=   r   r   r#   F)paddedr<   rm  rA   )r<   rC   r   rZ   r!   rg   )	r%   r  r  r  rl  r   r   r5  abs)r*   r+   r  r   r   r   r,   r  ZrA  rB  r/   r/   r0   rC    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  r=   r   r   r#   )r!   r   r   r   rg   r!   ri  )	r%   r  r  r  rl  r`   rn  Tr   )r*   r+   r  r   r   r   r   t1ZZ1r   t2ZZ2t3x1Zt4Zx2r/   r/   r0   test_permute_axes  s&    



zTestSTFT.test_permute_axesrC   rA   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  rs  rF  r[   r   rs  rF  r[   r    )r
  e   rv  rw  r   r  r   ry  )r    r   r   )r   r  r   r  r[   F)r   r   r   r   r  rC   )r   r   r   rC   r   r  	r%   r  r  r9   r  sizerl  rn  r   )r*   rC   r  r   Nr   r   rS  r+   r  zztrxrr   r/   r/   r0   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   rZ   r   )r   r   r   r   r   r[   Tr&   r   r   r   r   r  rm  ZNOLAr   r   r   r   rm  )r%   r  r  r(   r  r   r9   r  r  rl  r   ro  r   rn  Zallclose)r*   r  r  r   r  r   r   r   rS  r+   r  r  r  r  r/   r/   r0   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  ))rs  rF  r[   r}   )r
  r  rv  r  )r   r  r      )r  r  r      )r   r  r   r   r   r[   Tr&   r  r  r  )r%   r  r  r   r   r9   r  r  rl  rn  r   r  )r*   r  r   r  r   r   r   rS  r+   r  r  r  r  r/   r/   r0   test_roundtrip_nola_not_colaD  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  r[   Fr   r   r   r   r  rh  r   r  g-C6?gh㈵>)r  r   r8   )r%   r  r  r9   r  r  ZastypeZfloat32rl  rn  r   r   rH   r*   r  r   r  r   r   rS  r+   r  r  r  r  r   r/   r/   r0   test_roundtrip_float32b  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  r[   y              $@F)r   r   r   r   r  r<   rC   )r   r   r   input_onesidedrC   r   r  z9Input data is complex, switching to return_onesided=FalseT)r%   r  r  r9   r  r  rl  rn  r   r   r   r   )r*   rC   r  r   r  r   r   rS  r+   r  r  r  r  r   r   r/   r/   r0   test_roundtrip_complexw  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  r[   Tr  F)r   r   rm  )evenZoddZconstantr&   r   r  r  )r*   r  r   r  r   r   rS  r+   r  r  r  rm  Zzz_extZxr_extr   r/   r/   r0   !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  )rs  r  r[   r   )r   rG  r   ry  r[   Tr  )r   r   r   r  r  r  r/   r/   r0   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  ))r   r  r   ry  rk  )r   r  r   ry  i  )rs  rF  r[   r   !   )r  r  r   r   r  r[   y              ?rQ   T)r   r   rt   r   r   r  F)r   r   rt   r   r   r  r<   )r   r   rt   r   )r   r   rt   r   r  r   r  )r%   r  r  r9   r  r  expr  rl  rn  r   )r*   r  r   r  r   r   rt   rS  r+   Zxcr  rp  Zzcr  r  Zxcrr   r/   r/   r0   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  r}   r   r!   rg   r  ri  r   zistft transpose minuszistft transpose plus)r%   r  r  r  rl  r  r`   r  r   r   rn  r  r   )r*   r!  r  Zz_flatr"  r#  r+   Zz_plusZz_minusZx_transpose_mZx_transpose_pr/   r/   r0   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  rG   r  r   rZ   r  rA   )rm  rC   Tr   ?   Nr!   r   r   A   r   B   r7   Fr  )r<   rm  rC   rg   )r  rm  rC   r     )r%   r&   complexr   r   sumr  rl  rn  r   r  finforH   
resolutionrealimagra   Zconj)r*   Xr+   Zpower_xZsr  ZZpZpsd_ZpZZp0ZZp1s2Zpsd_Zp01r/   r/   r0   test_roundtrip_scaling  s8    
((


zTestSTFT.test_roundtrip_scalingN)r   r   r   rq  r  r  rC  r  r   r   Zparametrizer  r  r  r  r  r  r  r  r$  r  r/   r/   r/   r0   rf  `  s"   R

+!#rf  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edddZeed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/   r0   setup_methodj  s    

z/TestSampledSpectralRepresentations.setup_method)returnc                 C   s   | j | j S )zDuration of signal. r  r  rU   r/   r/   r0   r  w  s    z&TestSampledSpectralRepresentations.tauc                 C   s   d| j | j  S )z
Bin width r   r  rU   r/   r/   r0   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  r  r  r  r  r  r  r  )r*   Zsum_X_ref_squaredr/   r/   r0   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f 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)symrZ   r   )r  r   r   r<   rC   rA   rD   r7   r   r   N)r   r  r  r  r%   r  r  r  r   r  r   r  r  r  r  r  r  r  dictr   r  rH   r  rW  r  r   )r*   rN  Zc_ampZc_rmsZXwZX_ESDZX_PSDkwr  ZP_magZP_psdZ	float_resr/   r/   r0   test_windowed_DFT  s,    ,&"

z4TestSampledSpectralRepresentations.test_windowed_DFTN)r   r   r   __doc__r  rI   __annotations__r  floatr  r  r%   Zndarrayr  propertyr  r  r  r  r/   r/   r/   r0   r  Y  s    


r  )2r   numpyr%   Znumpy.testingr   r   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   rl  r   rn  r   r+  r   r   r)  r8  r>  rE  rf  r  r/   r/   r/   r0   <module>   s<   $$ ^  A  N5    |