def test_cached_coherence(): """Testing the cached coherence functions """ NFFT = 64 # This is the default behavior n_freqs = NFFT // 2 + 1 ij = [(0, 1), (1, 0)] ts = np.loadtxt(os.path.join(test_dir_path, 'tseries12.txt')) freqs, cache = tsa.cache_fft(ts, ij) # Are the frequencies the right ones? npt.assert_equal(freqs, utils.get_freqs(2 * np.pi, NFFT)) # Check that the fft of the first window is what we expect: hann = mlab.window_hanning(np.ones(NFFT)) w_ts = ts[0][:NFFT] * hann w_ft = fftpack.fft(w_ts)[0:n_freqs] # This is the result of the function: first_window_fft = cache['FFT_slices'][0][0] npt.assert_equal(w_ft, first_window_fft) coh_cached = tsa.cache_to_coherency(cache, ij)[0, 1] f, c = tsa.coherency(ts) coh_direct = c[0, 1] npt.assert_almost_equal(coh_direct, coh_cached) # Only welch PSD works and an error is thrown otherwise. This tests that # the error is thrown: with pytest.raises(ValueError) as e_info: tsa.cache_fft(ts, ij, method=methods[2]) # Take the method in which the window is defined on input: freqs, cache1 = tsa.cache_fft(ts, ij, method=methods[3]) # And compare it to the method in which it isn't: freqs, cache2 = tsa.cache_fft(ts, ij, method=methods[4]) npt.assert_equal(cache1, cache2) # Do the same, while setting scale_by_freq to False: freqs, cache1 = tsa.cache_fft(ts, ij, method=methods[3], scale_by_freq=False) freqs, cache2 = tsa.cache_fft(ts, ij, method=methods[4], scale_by_freq=False) npt.assert_equal(cache1, cache2) # Test cache_to_psd: psd1 = tsa.cache_to_psd(cache, ij)[0] # Against the standard get_spectra: f, c = tsa.get_spectra(ts) psd2 = c[0][0] npt.assert_almost_equal(psd1, psd2) # Test that prefer_speed_over_memory doesn't change anything: freqs, cache1 = tsa.cache_fft(ts, ij) freqs, cache2 = tsa.cache_fft(ts, ij, prefer_speed_over_memory=True) psd1 = tsa.cache_to_psd(cache1, ij)[0] psd2 = tsa.cache_to_psd(cache2, ij)[0] npt.assert_almost_equal(psd1, psd2)
def spectrum(self): """get the spectrum for the collection of time-series in this analyzer """ self.method['Fs'] = self.method.get('Fs',self.input.sampling_rate) spectrum = tsa.cache_to_psd(self.cache,self.ij) return spectrum
def spectrum(self): """get the spectrum for the collection of time-series in this analyzer """ spectrum = tsa.cache_to_psd(self.cache, self.ij) return spectrum