def test_fit_over_f_plus_const(self): dt = 0.13 n_time = 10000 amp = 0.67 # K**2/Hz index = -1.3 f_0 = 1.0 thermal = 2.7 # K**2/Hz BW = 1./dt/2 window = sig.get_window('hanning', n_time) n_spec = 10 p = 0 for ii in range(n_spec): time_stream = noise_power.generate_overf_noise(amp, index, f_0, dt, n_time) time_stream += rand.normal(size=n_time) * sp.sqrt(thermal * BW * 2) time_stream -= sp.mean(time_stream) time_stream *= window p += noise_power.calculate_power(time_stream) p /= n_spec p = noise_power.make_power_physical_units(p, dt) w = noise_power.calculate_power(window) w_norm = sp.mean(w).real #w /= w_norm p = noise_power.prune_power(p).real #p /= w_norm f = noise_power.ps_freq_axis(dt, n_time) p = p[1:] f = f[1:] amp_m, index_m, f0_m, thermal_m = mn.fit_overf_const(p, w, f) self.assertTrue(sp.allclose(amp_m, amp, atol=0.2)) self.assertTrue(sp.allclose(index_m, index, atol=0.1)) self.assertTrue(sp.allclose(thermal_m, thermal, atol=0.1))
def test_convolve_normalization(self) : window = sp.ones_like(self.wave1) power = npow.calculate_power(self.wave1) window_power = npow.calculate_power(window) # Convolving with the window function should do nothing for all ones. convolved_power = npow.convolve_power(power, window_power) self.assertTrue(sp.allclose(power, convolved_power))
def test_convolve_normalization(self): window = sp.ones_like(self.wave1) power = npow.calculate_power(self.wave1) window_power = npow.calculate_power(window) # Convolving with the window function should do nothing for all ones. convolved_power = npow.convolve_power(power, window_power) self.assertTrue(sp.allclose(power, convolved_power))
def test_fit_over_f_plus_const(self): dt = 0.13 n_time = 10000 amp = 0.67 # K**2/Hz index = -1.3 f_0 = 1.0 thermal = 2.7 # K**2/Hz BW = 1. / dt / 2 window = sig.get_window('hanning', n_time) n_spec = 10 p = 0 for ii in range(n_spec): time_stream = noise_power.generate_overf_noise( amp, index, f_0, dt, n_time) time_stream += rand.normal(size=n_time) * sp.sqrt(thermal * BW * 2) time_stream -= sp.mean(time_stream) time_stream *= window p += noise_power.calculate_power(time_stream) p /= n_spec p = noise_power.make_power_physical_units(p, dt) w = noise_power.calculate_power(window) w_norm = sp.mean(w).real #w /= w_norm p = noise_power.prune_power(p).real #p /= w_norm f = noise_power.ps_freq_axis(dt, n_time) p = p[1:] f = f[1:] amp_m, index_m, f0_m, thermal_m = mn.fit_overf_const(p, w, f) self.assertTrue(sp.allclose(amp_m, amp, atol=0.2)) self.assertTrue(sp.allclose(index_m, index, atol=0.1)) self.assertTrue(sp.allclose(thermal_m, thermal, atol=0.1))
def test_convolve(self) : window = sp.ones_like(self.wave1) window += sp.sin(sp.arange(self.n)/50.0) self.wave1 *= window power = npow.calculate_power(self.wave1) window_power = npow.calculate_power(window) deconvolved_power = npow.deconvolve_power(power, window_power) reconvolved_power = npow.convolve_power(deconvolved_power, window_power) self.assertTrue(sp.allclose(power, reconvolved_power))
def test_convolve(self): window = sp.ones_like(self.wave1) window += sp.sin(sp.arange(self.n) / 50.0) self.wave1 *= window power = npow.calculate_power(self.wave1) window_power = npow.calculate_power(window) deconvolved_power = npow.deconvolve_power(power, window_power) reconvolved_power = npow.convolve_power(deconvolved_power, window_power) self.assertTrue(sp.allclose(power, reconvolved_power))
def test_no_window(self) : window = sp.ones_like(self.wave1) power = npow.windowed_power(self.wave1, window) power = npow.prune_power(power) self.assertAlmostEqual(power[self.mode1]/self.amp1**2/self.n*4, 1) self.assertTrue(sp.allclose(power[:self.mode1], 0, atol=self.amp1**2*self.n/1e15)) self.assertTrue(sp.allclose(power[self.mode1+1:], 0, atol=self.amp1**2*self.n/1e15)) # With no window, we have a quick way to the answer quick_power = npow.calculate_power(self.wave1) self.assertTrue(sp.allclose(power, quick_power[:self.n//2]))
def test_no_window(self): window = sp.ones_like(self.wave1) power = npow.windowed_power(self.wave1, window) power = npow.prune_power(power) self.assertAlmostEqual(power[self.mode1] / self.amp1**2 / self.n * 4, 1) self.assertTrue( sp.allclose(power[:self.mode1], 0, atol=self.amp1**2 * self.n / 1e15)) self.assertTrue( sp.allclose(power[self.mode1 + 1:], 0, atol=self.amp1**2 * self.n / 1e15)) # With no window, we have a quick way to the answer quick_power = npow.calculate_power(self.wave1) self.assertTrue(sp.allclose(power, quick_power[:self.n // 2]))
def test_statistical_physical_units(self) : n_trials = 1000 n_points = 200 dt = 0.001 window = sp.ones(n_points, dtype=float) power = sp.zeros(n_points//2) for ii in range(n_trials) : wave = self.amp1*random.randn(n_points) power += npow.prune_power(npow.calculate_power(wave)).real power /= n_trials power = npow.make_power_physical_units(power, dt) freqs = npow.ps_freq_axis(dt, n_points) df = abs(sp.mean(sp.diff(freqs))) # The integral of the power spectrum should be the variance. Factor of # 2 get the negitive frequencies. integrated_power = sp.sum(power) * df * 2 self.assertTrue(sp.allclose(integrated_power/self.amp1**2, 1.0, atol=4.0*(2.0/sp.sqrt(n_trials*n_points))))
def test_statistical_physical_units(self): n_trials = 1000 n_points = 200 dt = 0.001 window = sp.ones(n_points, dtype=float) power = sp.zeros(n_points // 2) for ii in range(n_trials): wave = self.amp1 * random.randn(n_points) power += npow.prune_power(npow.calculate_power(wave)).real power /= n_trials power = npow.make_power_physical_units(power, dt) freqs = npow.ps_freq_axis(dt, n_points) df = abs(sp.mean(sp.diff(freqs))) # The integral of the power spectrum should be the variance. Factor of # 2 get the negitive frequencies. integrated_power = sp.sum(power) * df * 2 self.assertTrue( sp.allclose(integrated_power / self.amp1**2, 1.0, atol=4.0 * (2.0 / sp.sqrt(n_trials * n_points))))