Module Expr Str Import alias identifier Import alias identifier Import alias identifier Import alias identifier Expr Str Assign NameStore Num Assign NameStore Call NameLoad Call AttributeLoad NameLoad attr BinOpAdd Call AttributeLoad NameLoad attr NameLoad Num Assign NameStore Num Assign NameStore Call AttributeLoad NameLoad attr Call AttributeLoad AttributeLoad NameLoad attr attr Num BinOpPow Num BinOpSub NameLoad Num keyword TupleLoad Num While Call AttributeLoad NameLoad attr CompareLt Call AttributeLoad NameLoad attr NameLoad BinOpMult Num NameLoad body Assign NameStore Call AttributeLoad NameLoad attr Call AttributeLoad AttributeLoad NameLoad attr attr BinOpPow Num BinOpSub NameLoad Num keyword TupleLoad Num Assign NameStore Call AttributeLoad NameLoad attr Str Expr Str AugAssignAdd NameStore Call AttributeLoad AttributeLoad NameLoad attr attr Num Expr Str AugAssignDiv NameStore BinOpPow Num BinOpSub NameLoad Num Assign NameStore BinOpMult BinOpMult Call AttributeLoad AttributeLoad NameLoad attr attr Num Num AttributeLoad NameLoad attr Assign NameStore BinOpAdd BinOpDiv Call AttributeLoad NameLoad attr Num Num Call AttributeLoad NameLoad attr Call AttributeLoad AttributeLoad NameLoad attr attr Num Expr Str Assign NameStore Num Assign NameStore Call AttributeLoad NameLoad attr NameLoad Assign NameStore BinOpMult SubscriptLoad NameLoad ExtSlice Slice Index NameLoad Call AttributeLoad NameLoad attr BinOpAdd BinOpMult BinOpMult BinOpMult Num AttributeLoad NameLoad attr NameLoad SubscriptLoad NameLoad ExtSlice Slice Index NameLoad SubscriptLoad NameLoad ExtSlice Slice Index NameLoad Assign NameStore Call AttributeLoad NameLoad attr NameLoad keyword Num Assign NameStore BinOpMult Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad NameLoad Assign NameStore BinOpAdd NameLoad NameLoad Expr Str Expr Call AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr Num Expr Call AttributeLoad NameLoad attr AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr Call AttributeLoad BinOpMult Call AttributeLoad NameLoad attr ListLoad Num Num NameLoad attr Str Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr Num Expr Call AttributeLoad NameLoad attr NameLoad keyword Str keyword Num Expr Call AttributeLoad NameLoad attr NameLoad keyword TupleLoad Num Num Num keyword Num keyword Str Expr Call AttributeLoad NameLoad attr Call AttributeLoad BinOpMult Call AttributeLoad NameLoad attr ListLoad Num Num NameLoad attr Str Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad Call AttributeLoad NameLoad attr attr Expr Str Expr Str Assign TupleStore NameStore NameStore Call AttributeLoad NameLoad attr NameLoad TupleLoad NameLoad BinOpMult Num NameLoad keyword NameLoad keyword BinOpPow Num NameLoad Assign NameStore BinOpMult Num AttributeLoad BinOpMult SubscriptLoad NameLoad ExtSlice Slice Index NameLoad Call AttributeLoad NameLoad attr BinOpMult BinOpMult BinOpMult Num AttributeLoad NameLoad attr NameLoad SubscriptLoad NameLoad ExtSlice Slice Index NameLoad attr Expr Call AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr Num Expr Call AttributeLoad NameLoad attr AttributeLoad NameLoad attr Str keyword Num Expr Call AttributeLoad NameLoad attr AttributeLoad NameLoad attr Str keyword Num Expr Call AttributeLoad NameLoad attr BinOpMod Str SubscriptLoad AttributeLoad NameLoad attr Index Num Expr Call AttributeLoad NameLoad attr Call AttributeLoad BinOpMult Call AttributeLoad NameLoad attr ListLoad Num Num NameLoad attr Str Expr Call AttributeLoad NameLoad attr Num Assign NameStore BinOpSub NameLoad Call AttributeLoad NameLoad attr NameLoad keyword Num Expr Call AttributeLoad NameLoad attr BinOpPow NameLoad Num Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr Expr Str Assign NameStore Call AttributeLoad NameLoad attr NameLoad AugAssignAdd SubscriptStore NameLoad Index CompareLt NameLoad Num BinOpMult Num AttributeLoad NameLoad attr Assign NameStore BinOpPow Call AttributeLoad AttributeLoad NameLoad attr attr BinOpSub NameLoad NameLoad Num Assign NameStore BinOpDiv BinOpPow Call AttributeLoad AttributeLoad NameLoad attr attr BinOpSub NameLoad BinOpMult Num Call AttributeLoad NameLoad attr NameLoad Num BinOpPow Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad Num Assign NameStore BinOpPow Call AttributeLoad AttributeLoad NameLoad attr attr BinOpSub NameLoad NameLoad Num Print TupleLoad Str NameLoad Str NameLoad BinOpMod Str NameLoad Print TupleLoad Str NameLoad Str BinOpMult Num Call AttributeLoad NameLoad attr NameLoad BinOpMod Str NameLoad Print TupleLoad Str NameLoad Str NameLoad BinOpMod Str NameLoad Print BinOpMod Str TupleLoad BinOpDiv Call AttributeLoad NameLoad attr BinOpPow NameLoad Num BinOpPow NameLoad Num
N/A N/A 

.. _multi-taper-harmonic-test:


=========================================
Multitaper F-test for harmonic components
=========================================

The Slepian sequences of the multitaper spectral estimation method can
also be used to perform a hypothesis test regarding the presence of a
pure sinusoid at any analyzed frequency. The F-test is used to assess
whether the power at a given frequency can be attributed to a single
line component. In this case, the power would be given by the
summed spectral convolutions of the Slepian frequency functions with
the line power spectrum, which is a dirac delta. The complex Fourier
coefficient of the putative sinusoid is estimated through a linear
regression of the Slepian DC components, and the strength of the
regression coefficient is tested against the residual spectral power
for the F-test.

The following demonstrates the use of the harmonic test.

 N/A numpy np N/A nitime.algorithms nt_alg N/A nitime.utils nt_ut N/A matplotlib.pyplot pp N/A 
We will set up a test signal with 3 harmonic components within
Gaussian noise. The line components must be sufficiently resolved
given the multi-taper bandwidth of 2NW.
 N/A N 10000 N/A fft_pow N/A int N/A N/A np ceil N/A N/A N/A np log2 N 2 N/A NW 4 N/A lines N/A N/A np sort N/A N/A N/A np random randint 100 N/A 2 N/A fft_pow 6 size N/A 3 N/A N/A N/A np any N/A N/A N/A np diff lines N/A 2 NW N/A N/A lines N/A N/A np sort N/A N/A N/A np random randint N/A 2 N/A fft_pow 6 size N/A 3 N/A lines N/A N/A lines astype d N/A 
The harmonic test should find *exact* frequencies if they were to fall
on the FFT grid. (Try commenting the following to see.) In the
scenario of real sampled data, increasing the number of FFT points can
help to locate the line components.
 N/A lines N/A N/A N/A np random randn 3 N/A 
Now proceed to specify the frequencies, phases, and amplitudes.
 N/A lines N/A 2.0 N/A fft_pow 2 N/A phs N/A N/A N/A N/A N/A np random rand 3 2 N/A np pi N/A amps N/A N/A N/A N/A np sqrt 2 2 N/A N/A np abs N/A N/A N/A np random randn 3 N/A 
Set the RMS noise power here. Strategies to detect harmonics in low
SNR include improving the reliability of the spectral estimate
(increasing NW) and/or increasing the number of FFT points. Note that
the former option will limit the ability to resolve lines at nearby
frequencies.
 N/A nz_sig 1 N/A tx N/A N/A np arange N N/A harmonics N/A N/A amps N/A N/A N/A None N/A N/A np cos N/A N/A N/A N/A 2 N/A np pi tx N/A lines N/A N/A N/A None N/A phs N/A N/A N/A None N/A harmonic N/A N/A np sum harmonics axis 0 N/A nz N/A N/A N/A N/A np random randn N nz_sig N/A sig N/A harmonic nz N/A 
Take a look at our mock signal.
 N/A N/A N/A pp figure N/A N/A N/A pp subplot 211 N/A N/A N/A pp plot N/A harmonics T N/A N/A N/A pp xlim N/A N/A N/A N/A N/A np array N/A 0.2 0.3 N astype i N/A N/A N/A pp title Sinusoid components N/A N/A N/A pp subplot 212 N/A N/A N/A pp plot harmonic color k linewidth 3 N/A N/A N/A pp plot sig color N/A 0.6 0.6 0.6 linewidth 2 linestyle -- N/A N/A N/A pp xlim N/A N/A N/A N/A N/A np array N/A 0.2 0.3 N astype i N/A N/A N/A pp title Signal in noise N/A N/A N/A N/A N/A pp gcf tight_layout N/A 

.. image:: fig/mtm_harmonic_test_01.png

 N/A 
Here we'll use the :func:`utils.detect_lines` function with the given
Slepian properties (NW), and we'll ensure that we limit spectral bias
by choosing Slepians with concentration factors greater than 0.9. The
arrays returned include the detected line frequencies (f) and their
complex coefficients (b). The frequencies are normalized from :math:`(0,rac{1}{2})`
 N/A N/A f b N/A N/A nt_ut detect_lines sig N/A NW N/A 2 NW low_bias True NFFT N/A 2 fft_pow N/A h_est N/A 2 N/A N/A N/A b N/A N/A N/A None N/A N/A np exp N/A N/A N/A 2j N/A np pi tx N/A f N/A N/A N/A None real N/A N/A N/A pp figure N/A N/A N/A pp subplot 211 N/A N/A N/A pp plot N/A harmonics T c linewidth 3 N/A N/A N/A pp plot N/A h_est T r-- linewidth 2 N/A N/A N/A pp title N/A %d lines detected N/A N/A h_est shape N/A 0 N/A N/A N/A pp xlim N/A N/A N/A N/A N/A np array N/A 0.2 0.3 N astype i N/A N/A N/A pp subplot 212 N/A err N/A harmonic N/A N/A np sum h_est axis 0 N/A N/A N/A pp plot N/A err 2 N/A N/A N/A pp title Error signal N/A N/A N/A pp show N/A 

.. image:: fig/mtm_harmonic_test_02.png

We can see the quality (or not) of our estimated lines. A breakdown of
the errors in the various estimated quantities follows in the demo.

 N/A phs_est N/A N/A np angle b N/A N/A phs_est N/A N/A phs_est 0 N/A 2 N/A np pi N/A phs_err N/A N/A N/A N/A np linalg norm N/A phs_est phs 2 N/A amp_err N/A N/A N/A N/A N/A np linalg norm N/A amps N/A 2 N/A N/A np abs b 2 N/A N/A N/A N/A np linalg norm amps 2 N/A freq_err N/A N/A N/A N/A np linalg norm N/A lines f 2 N/A N/A freqs: lines 	estimated: f N/A 	err: %1.3e freq_err N/A N/A amp: amps 	estimated: N/A 2 N/A N/A np abs b N/A 	err: %1.3e amp_err N/A N/A phase: phs 	estimated: phs_est N/A 	err: %1.3e phs_err N/A N/A MS error over noise: %1.3e N/A N/A N/A N/A np mean N/A err 2 N/A nz_sig 2