def test_AR_LD(): """ Test the Levinson Durbin estimate of the AR coefficients against the expercted PSD """ arsig,_,_ = utils.ar_generator(N=512) avg_pwr = (arsig*arsig.conjugate()).real.mean() order = 8 ak, sigma_v = tsa.AR_est_LD(arsig, order) w, psd = tsa.AR_psd(ak, sigma_v) # the psd is a one-sided power spectral density, which has been # multiplied by 2 to preserve the property that # 1/2pi int_{-pi}^{pi} Sxx(w) dw = Rxx(0) # evaluate this integral numerically from 0 to pi dw = np.pi/len(psd) avg_pwr_est = np.trapz(psd, dx=dw) / (2*np.pi) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=0) # Test for providing the autocovariance as an input: ak,sigma_v = tsa.AR_est_LD(arsig, order, utils.autocov(arsig)) w, psd = tsa.AR_psd(ak, sigma_v) avg_pwr_est = np.trapz(psd, dx=dw) / (2*np.pi) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=0)
def test_AR_LD(): """ Test the Levinson Durbin estimate of the AR coefficients against the expercted PSD """ arsig, _, _ = utils.ar_generator(N=512) avg_pwr = (arsig * arsig.conjugate()).real.mean() order = 8 ak, sigma_v = tsa.AR_est_LD(arsig, order) w, psd = tsa.AR_psd(ak, sigma_v) # the psd is a one-sided power spectral density, which has been # multiplied by 2 to preserve the property that # 1/2pi int_{-pi}^{pi} Sxx(w) dw = Rxx(0) # evaluate this integral numerically from 0 to pi dw = np.pi / len(psd) avg_pwr_est = np.trapz(psd, dx=dw) / (2 * np.pi) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=0) # Test for providing the autocovariance as an input: ak, sigma_v = tsa.AR_est_LD(arsig, order, utils.autocov(arsig)) w, psd = tsa.AR_psd(ak, sigma_v) avg_pwr_est = np.trapz(psd, dx=dw) / (2 * np.pi) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=0)
def _plot_acov_fired(self): t, y = self.curve_manager.interactive_curve.current_data( full_xdata=False) y *= 1e6 t_stamp = t.mean() t = (t - t[0]) * 1e3 dt = t[1] - t[0] N = int(self.n_lags / dt) Ct = autocov(y, all_lags=False)[:, :min(len(t), N)] if self.norm_kernel: Ct /= Ct[:, 0][:, None] chan_map = self.chan_map frames = chan_map.embed(Ct.T, axis=1) fig = Figure(figsize=(6, 10)) ax1 = subplot2grid(fig, (2, 1), (0, 0)) ax2 = subplot2grid(fig, (2, 1), (1, 0)) # top panel is acov map at lag t mx = np.abs(Ct).max() _, cb = chan_map.image(Ct[:, 0], cmap='bwr', clim=(-mx, mx), ax=ax1) ax1.set_title('Map of ~ t={0:.2f} sec'.format(t_stamp)) # bottom panel left: acov as function of lag t ax2.plot(t[:len(frames)], Ct.T, lw=.25, color='k') ax2.set_xlabel('Lags (ms)') if self.norm_kernel: ax1.set_title('Map of autocorr. ~ t={0:.2f} sec'.format(t_stamp)) cb.set_label('Autocorrelation') ax2.set_ylabel('Autocorrelation') else: ax1.set_title('Map of autocov. ~ t={0:.2f} sec'.format(t_stamp)) cb.set_label('Autocovariance') ax2.set_ylabel('Autocovariance') plotters.sns.despine(ax=ax2) splot = MultiframeSavesFigure(fig, frames, mode_name='lag (ms)', frame_index=t[:len(frames)]) splot.edit_traits() fig.tight_layout() fig.canvas.draw_idle() if not hasattr(self, '_kernel_plots'): self._kernel_plots = [splot] else: self._kernel_plots.append(splot)
def test_AR_YW(): arsig,_,_ = utils.ar_generator(N=512) avg_pwr = (arsig*arsig.conjugate()).mean() order = 8 ak,sigma_v = tsa.AR_est_YW(arsig, order) w, psd = tsa.AR_psd(ak, sigma_v) # the psd is a one-sided power spectral density, which has been # multiplied by 2 to preserve the property that # 1/2pi int_{-pi}^{pi} Sxx(w) dw = Rxx(0) # evaluate this integral numerically from 0 to pi dw = np.pi / len(psd) avg_pwr_est = np.trapz(psd, dx=dw) / (2*np.pi) # consistency on the order of 10**0 is pretty good for this test npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=0) # Test for providing the autocovariance as an input: ak,sigma_v = tsa.AR_est_YW(arsig, order, utils.autocov(arsig)) w, psd = tsa.AR_psd(ak, sigma_v) avg_pwr_est = np.trapz(psd, dx=dw) / (2*np.pi) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=0)
def test_AR_YW(): arsig, _, _ = utils.ar_generator(N=512) avg_pwr = (arsig * arsig.conjugate()).mean() order = 8 ak, sigma_v = tsa.AR_est_YW(arsig, order) w, psd = tsa.AR_psd(ak, sigma_v) # the psd is a one-sided power spectral density, which has been # multiplied by 2 to preserve the property that # 1/2pi int_{-pi}^{pi} Sxx(w) dw = Rxx(0) # evaluate this integral numerically from 0 to pi dw = np.pi / len(psd) avg_pwr_est = np.trapz(psd, dx=dw) / (2 * np.pi) # consistency on the order of 10**0 is pretty good for this test npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=0) # Test for providing the autocovariance as an input: ak, sigma_v = tsa.AR_est_YW(arsig, order, utils.autocov(arsig)) w, psd = tsa.AR_psd(ak, sigma_v) avg_pwr_est = np.trapz(psd, dx=dw) / (2 * np.pi) npt.assert_almost_equal(avg_pwr, avg_pwr_est, decimal=0)