<s> import os <EOL> import numpy as np <EOL> import numpy . testing as npt <EOL> import numpy . testing . decorators as dec <EOL> from scipy . signal import signaltools <EOL> from scipy import fftpack <EOL> import nitime <EOL> from nitime import algorithms as tsa <EOL> from nitime import utils as ut <EOL> test_dir_path = os . path . join ( nitime . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> def test_scipy_resample ( ) : <EOL> """<STR_LIT>""" <EOL> freq_list = np . random . randint ( <NUM_LIT:0> , high = <NUM_LIT:15> , size = <NUM_LIT:5> ) <EOL> a = [ np . sin ( <NUM_LIT:2> * np . pi * f * np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:64> , endpoint = False ) ) <EOL> for f in freq_list ] <EOL> tst = np . array ( a ) . sum ( axis = <NUM_LIT:0> ) <EOL> t_up = signaltools . resample ( tst , <NUM_LIT> ) <EOL> np . testing . assert_array_almost_equal ( t_up [ : : <NUM_LIT:2> ] , tst ) <EOL> t_dn = signaltools . resample ( tst , <NUM_LIT:32> ) <EOL> np . testing . assert_array_almost_equal ( t_dn , tst [ : : <NUM_LIT:2> ] ) <EOL> dn_samp_ana = np . array ( [ np . sin ( <NUM_LIT:2> * np . pi * f * np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> , endpoint = False ) ) <EOL> for f in freq_list ] ) . sum ( axis = <NUM_LIT:0> ) <EOL> t_dn2 = signaltools . resample ( tst , <NUM_LIT> ) <EOL> npt . assert_array_almost_equal ( t_dn2 , dn_samp_ana ) <EOL> def test_dpss_windows ( ) : <EOL> "<STR_LIT>" <EOL> _ , l = tsa . dpss_windows ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:4> ) <EOL> unos = np . ones ( <NUM_LIT:4> ) <EOL> npt . assert_array_almost_equal ( l , unos ) <EOL> _ , l = tsa . dpss_windows ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:4> ) <EOL> npt . assert_array_almost_equal ( l , unos ) <EOL> _ , l = tsa . dpss_windows ( <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT:4> ) <EOL> npt . assert_array_almost_equal ( l , unos ) <EOL> _ , l = tsa . dpss_windows ( <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT> ) <EOL> npt . assert_array_almost_equal ( l , unos ) <EOL> def test_dpss_matlab ( ) : <EOL> """<STR_LIT>""" <EOL> a , _ = tsa . dpss_windows ( <NUM_LIT:100> , <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> b = np . loadtxt ( os . path . join ( test_dir_path , '<STR_LIT>' ) ) <EOL> npt . assert_almost_equal ( a , b . T ) <EOL> def test_periodogram ( ) : <EOL> arsig , _ , _ = ut . ar_generator ( N = <NUM_LIT> ) <EOL> avg_pwr = ( arsig * arsig . conjugate ( ) ) . mean ( ) <EOL> f , psd = tsa . periodogram ( arsig , N = <NUM_LIT> ) <EOL> df = <NUM_LIT> * np . pi / <NUM_LIT> <EOL> avg_pwr_est = np . trapz ( psd , dx = df ) <EOL> npt . assert_almost_equal ( avg_pwr , avg_pwr_est , decimal = <NUM_LIT:1> ) <EOL> def permutation_system ( N ) : <EOL> p = np . zeros ( ( N , N ) ) <EOL> targets = list ( range ( N ) ) <EOL> for i in range ( N ) : <EOL> popper = np . random . randint ( <NUM_LIT:0> , high = len ( targets ) ) <EOL> j = targets . pop ( popper ) <EOL> p [ i , j ] = <NUM_LIT:1> <EOL> return p <EOL> def test_boxcar_filter ( ) : <EOL> a = np . random . rand ( <NUM_LIT:100> ) <EOL> b = tsa . boxcar_filter ( a ) <EOL> npt . assert_equal ( a , b ) <EOL> a = np . random . rand ( <NUM_LIT> ) <EOL> b = tsa . boxcar_filter ( a ) <EOL> npt . assert_equal ( a , b ) <EOL> b = tsa . boxcar_filter ( a , ub = <NUM_LIT> ) <EOL> npt . assert_equal ( a . shape , b . shape ) <EOL> b = tsa . boxcar_filter ( a , lb = <NUM_LIT> ) <EOL> npt . assert_equal ( a . shape , b . shape ) <EOL> def test_get_spectra ( ) : <EOL> """<STR_LIT>""" <EOL> t = np . linspace ( <NUM_LIT:0> , <NUM_LIT:16> * np . pi , <NUM_LIT:2> ** <NUM_LIT:10> ) <EOL> x = ( np . sin ( t ) + np . sin ( <NUM_LIT:2> * t ) + np . sin ( <NUM_LIT:3> * t ) + <EOL> <NUM_LIT:0.1> * np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) ) <EOL> NFFT = <NUM_LIT:64> <EOL> N = x . shape [ - <NUM_LIT:1> ] <EOL> f_welch = tsa . get_spectra ( x , method = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : NFFT } ) <EOL> f_periodogram = tsa . get_spectra ( x , method = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> f_multi_taper = tsa . get_spectra ( x , method = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> npt . assert_equal ( f_welch [ <NUM_LIT:0> ] . shape , ( NFFT // <NUM_LIT:2> + <NUM_LIT:1> , ) ) <EOL> npt . assert_equal ( f_periodogram [ <NUM_LIT:0> ] . shape , ( N // <NUM_LIT:2> + <NUM_LIT:1> , ) ) <EOL> npt . assert_equal ( f_multi_taper [ <NUM_LIT:0> ] . shape , ( N // <NUM_LIT:2> + <NUM_LIT:1> , ) ) <EOL> x = np . reshape ( x , ( <NUM_LIT:2> , x . shape [ - <NUM_LIT:1> ] // <NUM_LIT:2> ) ) <EOL> N = x . shape [ - <NUM_LIT:1> ] <EOL> NFFT = <NUM_LIT:64> <EOL> f_welch = tsa . get_spectra ( x , method = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : NFFT } ) <EOL> f_periodogram = tsa . get_spectra ( x , method = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> f_multi_taper = tsa . get_spectra ( x , method = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> npt . assert_equal ( f_welch [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] , NFFT / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> npt . assert_equal ( f_periodogram [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] , N / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> npt . assert_equal ( f_multi_taper [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] , N / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> def test_psd_matlab ( ) : <EOL> """<STR_LIT>""" <EOL> from matplotlib import mlab <EOL> test_dir_path = os . path . join ( nitime . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> ts = np . loadtxt ( os . path . join ( test_dir_path , '<STR_LIT>' ) ) <EOL> ts0 = ts [ <NUM_LIT:1> ] + ts [ <NUM_LIT:0> ] * np . complex ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> NFFT = <NUM_LIT> <EOL> Fs = <NUM_LIT:1.0> <EOL> noverlap = NFFT / <NUM_LIT:2> <EOL> fxx , f = mlab . psd ( ts0 , NFFT = NFFT , Fs = Fs , noverlap = noverlap , <EOL> scale_by_freq = True ) <EOL> fxx_mlab = fftpack . fftshift ( fxx ) . squeeze ( ) <EOL> fxx_matlab = np . loadtxt ( os . path . join ( test_dir_path , '<STR_LIT>' ) ) <EOL> npt . assert_almost_equal ( fxx_mlab , fxx_matlab , decimal = <NUM_LIT:5> ) <EOL> @ dec . slow <EOL> def test_long_dpss_win ( ) : <EOL> """<STR_LIT>""" <EOL> a1 , e = tsa . dpss_windows ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT:8> , interp_from = <NUM_LIT> ) <EOL> a2 , e = tsa . dpss_windows ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> npt . assert_almost_equal ( a1 , a2 , decimal = <NUM_LIT:5> ) <EOL> test_dir_path = os . path . join ( nitime . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> matlab_long_dpss = np . load ( os . path . join ( test_dir_path , '<STR_LIT>' ) ) <EOL> npt . assert_almost_equal ( a1 [ <NUM_LIT:0> ] , matlab_long_dpss , decimal = <NUM_LIT:5> ) <EOL> npt . assert_almost_equal ( a1 [ <NUM_LIT:0> ] , matlab_long_dpss , decimal = <NUM_LIT:5> ) </s>
