def test_periodogram_spectral_normalization(): """ Check that the spectral estimators are normalized in the correct Watts/Hz fashion """ x = np.random.randn(1024) f1, Xp1 = tsa.periodogram(x) f2, Xp2 = tsa.periodogram(x, Fs=100) f3, Xp3 = tsa.periodogram(x, N=2**12) p1 = np.sum(Xp1) * 2 * np.pi / 2**10 p2 = np.sum(Xp2) * 100 / 2**10 p3 = np.sum(Xp3) * 2 * np.pi / 2**12 npt.assert_( np.abs(p1 - p2) < 1e-14, 'Inconsistent frequency normalization in periodogram (1)' ) npt.assert_( np.abs(p3 - p2) < 1e-8, 'Inconsistent frequency normalization in periodogram (2)' ) td_var = np.var(x) # assure that the estimators are at least in the same # order of magnitude as the time-domain variance npt.assert_( np.abs(np.log10(p1/td_var)) < 1, 'Incorrect frequency normalization in periodogram' ) # check the freq vector while we're here npt.assert_( f2.max() == 50, 'Periodogram returns wrong frequency bins' )
def test_periodogram_spectral_normalization(): """ Check that the spectral estimators are normalized in the correct Watts/Hz fashion """ x = np.random.randn(1024) f1, Xp1 = tsa.periodogram(x) f2, Xp2 = tsa.periodogram(x, Fs=100) f3, Xp3 = tsa.periodogram(x, N=2**12) p1 = np.sum(Xp1) * 2 * np.pi / 2**10 p2 = np.sum(Xp2) * 100 / 2**10 p3 = np.sum(Xp3) * 2 * np.pi / 2**12 nt.assert_true( np.abs(p1 - p2) < 1e-14, 'Inconsistent frequency normalization in periodogram (1)') nt.assert_true( np.abs(p3 - p2) < 1e-8, 'Inconsistent frequency normalization in periodogram (2)') td_var = np.var(x) # assure that the estimators are at least in the same # order of magnitude as the time-domain variance nt.assert_true( np.abs(np.log10(p1 / td_var)) < 1, 'Incorrect frequency normalization in periodogram') # check the freq vector while we're here nt.assert_true(f2.max() == 50, 'Periodogram returns wrong frequency bins')
def test_periodogram(): arsig, _, _ = ut.ar_generator(N=512) avg_pwr = (arsig * arsig.conjugate()).mean() f, psd = tsa.periodogram(arsig, N=2048) df = 2. * np.pi / 2048 avg_pwr_est = np.trapz(psd, dx=df) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=1)
def test_periodogram(): arsig,_,_ = ut.ar_generator(N=512) avg_pwr = (arsig*arsig.conjugate()).mean() f, psd = tsa.periodogram(arsig, N=2048) # for efficiency, let's leave out the 2PI in the numerator and denominator # for the following integral dw = 1./2048 avg_pwr_est = np.trapz(psd, dx=dw) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=1)
def test_periodogram(): arsig, _, _ = ut.ar_generator(N=512) avg_pwr = (arsig * arsig.conjugate()).mean() f, psd = tsa.periodogram(arsig, N=2048) # for efficiency, let's leave out the 2PI in the numerator and denominator # for the following integral dw = 1. / 2048 avg_pwr_est = np.trapz(psd, dx=dw) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=1)
def test_periodogram(): """Test some of the inputs to periodogram """ arsig, _, _ = utils.ar_generator(N=1024) Sk = fftpack.fft(arsig) f1, c1 = tsa.periodogram(arsig) f2, c2 = tsa.periodogram(arsig, Sk=Sk) npt.assert_equal(c1, c2) # Check that providing a complex signal does the right thing # (i.e. two-sided spectrum): N = 1024 r, _, _ = utils.ar_generator(N=N) c, _, _ = utils.ar_generator(N=N) arsig = r + c * scipy.sqrt(-1) f, c = tsa.periodogram(arsig) npt.assert_equal(f.shape[0], N) # Should be N, not the one-sided N/2 + 1
def test_periodogram(): """Test some of the inputs to periodogram """ arsig, _, _ = utils.ar_generator(N=1024) Sk = np.fft.fft(arsig) f1, c1 = tsa.periodogram(arsig) f2, c2 = tsa.periodogram(arsig, Sk=Sk) npt.assert_equal(c1, c2) # Check that providing a complex signal does the right thing # (i.e. two-sided spectrum): N = 1024 r, _, _ = utils.ar_generator(N=N) c, _, _ = utils.ar_generator(N=N) arsig = r + c * scipy.sqrt(-1) f, c = tsa.periodogram(arsig) npt.assert_equal(f.shape[0], N) # Should be N, not the one-sided N/2 + 1
def periodogram(self): """ This is the spectrum estimated as the FFT of the time-series Returns ------- (f,spectrum): f is an array with the frequencies and spectrum is the complex-valued FFT. """ return tsa.periodogram(self.input.data, Fs=self.input.sampling_rate)
def test_hermitian_periodogram_csd(): """ Make sure CSD matrices returned by various methods have Hermitian symmetry. """ sig = np.random.randn(4, 256) _, csd1 = tsa.periodogram_csd(sig) for i in range(4): for j in range(i + 1): xc1 = csd1[i, j] xc2 = csd1[j, i] npt.assert_equal(xc1, xc2.conj(), err_msg='Periodogram CSD not Hermitian') _, psd = tsa.periodogram(sig) for i in range(4): npt.assert_almost_equal( psd[i], csd1[i, i].real, err_msg='Periodgram CSD diagonal inconsistent with real PSD')
def test_hermitian_periodogram_csd(): """ Make sure CSD matrices returned by various methods have Hermitian symmetry. """ sig = np.random.randn(4,256) _, csd1 = tsa.periodogram_csd(sig) for i in range(4): for j in range(i+1): xc1 = csd1[i,j] xc2 = csd1[j,i] npt.assert_equal( xc1, xc2.conj(), err_msg='Periodogram CSD not Hermitian' ) _, psd = tsa.periodogram(sig) for i in range(4): npt.assert_almost_equal( psd[i], csd1[i,i].real, err_msg='Periodgram CSD diagonal inconsistent with real PSD' )
def get_psd_v2(data, fs): freq, psd = tsa.periodogram(data,fs,normalize=True) return freq, psd
""" psd *= 2 dB(psd, psd) """ We begin by using the naive periodogram function (:func:`tsa.periodogram` in order to calculate the PSD and compare that to the true PSD calculated above. """ freqs, d_psd = tsa.periodogram(ar_seq) dB(d_psd, d_psd) fig03 = plot_spectral_estimate(freqs, psd, (d_psd,), elabels=("Periodogram",)) """ .. image:: fig/multi_taper_spectral_estimation_03.png Next, we use Welch's periodogram, by applying :func:`tsa.get_spectra`. Note that we explicitely provide the function with a 'method' dict, which specifies the method used in order to calculate the PSD, but the default method is 'welch'. """
This is a one-sided spectrum, so we double the power: """ psd *= 2 dB(psd, psd) """ We begin by using the naive periodogram function (:func:`tsa.periodogram` in order to calculate the PSD and compare that to the true PSD calculated above. """ freqs, d_psd = tsa.periodogram(ar_seq) dB(d_psd, d_psd) fig03 = plot_spectral_estimate(freqs, psd, (d_psd, ), elabels=("Periodogram", )) """ .. image:: fig/multi_taper_spectral_estimation_03.png Next, we use Welch's periodogram, by applying :func:`tsa.get_spectra`. Note that we explicitly provide the function with a 'method' dict, which specifies the method used in order to calculate the PSD, but the default method is 'welch'. """
ar_seq = foo["arr_0"] nz = foo["arr_1"] alpha = foo["arr_2"] else: ar_seq, nz, alpha = utils.ar_generator(N=N, drop_transients=10) ar_seq -= ar_seq.mean() np.savez("example_arrs", ar_seq, nz, alpha) # --- True SDF fgrid, hz = alg.my_freqz(1.0, a=np.r_[1, -alpha], Nfreqs=N) sdf = (hz * hz.conj()).real # onesided spectrum, so double the power sdf[1:-1] *= 2 dB(sdf, sdf) # --- Direct Spectral Estimator freqs, d_sdf = alg.periodogram(ar_seq) dB(d_sdf, d_sdf) # --- Welch's Overlapping Periodogram Method via mlab mlab_sdf, mlab_freqs = pp.mlab.psd(ar_seq, NFFT=N) mlab_freqs *= np.pi / mlab_freqs.max() mlab_sdf = mlab_sdf.squeeze() dB(mlab_sdf, mlab_sdf) ### Taper Bandwidth Adjustments NW = 4 # --- Regular Multitaper Estimate f, sdf_mt, nu = alg.multi_taper_psd(ar_seq, width=NW, adaptive=False, jackknife=False) dB(sdf_mt, sdf_mt)