def crossSpecgram(x, y, NFFT=256, Fs=2, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None): """ Use matplotlib.mlab function, _spectral_helper, to calculate cross power between x and y in short time bins. USAGE: Pxy,freqs,t=crossSpecgram(x,y,NFFT=256, Fs=2, detrend=mlab.detrend_none, window=window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None) NFFT=# data points in each time bin Fs = sample frequency in units of 1/[x] etc. - as in other mlab functions. Pxy=cross power Ted Golfinopoulos, 15 Aug 2012 """ assert (len(x) == len(y)) Pxy, freqs, t = mlab._spectral_helper(x, y, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq) return Pxy, freqs, t
def test_spectral_helper_raises(self): # We don't use parametrize here to handle ``y = self.y``. for kwargs in [ # Various error conditions: {"y": self.y+1, "mode": "complex"}, # Modes requiring ``x is y``. {"y": self.y+1, "mode": "magnitude"}, {"y": self.y+1, "mode": "angle"}, {"y": self.y+1, "mode": "phase"}, {"mode": "spam"}, # Bad mode. {"y": self.y, "sides": "eggs"}, # Bad sides. {"y": self.y, "NFFT": 10, "noverlap": 20}, # noverlap > NFFT. {"NFFT": 10, "noverlap": 10}, # noverlap == NFFT. {"y": self.y, "NFFT": 10, "window": np.ones(9)}, # len(win) != NFFT. ]: with pytest.raises(ValueError): mlab._spectral_helper(x=self.y, **kwargs)
def test_spectral_helper_psd(self, mode, case): freqs = getattr(self, f"freqs_{case}") spec, fsp, t = mlab._spectral_helper( x=self.y, y=self.y, NFFT=getattr(self, f"NFFT_{case}"), Fs=self.Fs, noverlap=getattr(self, f"nover_{case}"), pad_to=getattr(self, f"pad_to_{case}"), sides=self.sides, mode=mode) assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, getattr(self, f"t_{case}"), atol=1e-06) assert spec.shape[0] == freqs.shape[0] assert spec.shape[1] == getattr(self, f"t_{case}").shape[0]