lasp/test/test_ps.py

39 lines
794 B
Python
Raw Normal View History

2018-01-29 15:14:50 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Testing code for power spectra
2018-01-29 15:14:50 +00:00
"""
import numpy as np
from lasp import PowerSpectra, Window
# import matplotlib.pyplot as plt
# plt.close('all')
def test_ps():
"""
Check Parsevall for single-sided power spectra
"""
nfft = 2048
t = np.linspace(0, 1.0, nfft, endpoint=False)
ps = PowerSpectra(nfft, Window.WindowType.Rectangular)
sig = np.random.randn(nfft)
freq = 4
omg = 2*np.pi*freq
# sig = 8*np.cos(omg*t)
cps = ps.compute(sig[:,None])
pow1 = np.sum(sig**2)/sig.size
pow2 = np.sum((cps[:,0,0]).real)
# print(pow1)
# print(pow2)
# plt.plot(cps[:,0,0])
assert np.isclose(pow2 - pow1,0, atol=1e-1)
if __name__ == '__main__':
test_ps()