def mc_ar1_spectrum(self, data=None, N=1000, filter_type=None, filter_cutoff=None, spectrum='mtm'): """ calculates the Monte-Carlo spectrum with 1, 2.5, 5, 95, 97.5, 99 percentiles input: x .. time series spectrum .. spectral density estimation function N .. number of MC simulations filter_type .. filter_cutoff .. spectrum .. 'mtm': multi-taper method, 'per': periodogram, 'Welch' output: mc_spectrum .. """ if data is None: data = np.array(self.ts) assert type(data) == np.ndarray AM = ARMA(endog=data, order=(1, 0)).fit() phi, std = AM.arparams[0], np.sqrt(AM.sigma2) mc = self.mc_ar1_ARMA(phi=phi, std=std, n=len(data), N=N) if filter_type is not None: assert filter_type in ['lowpass', 'chebychev'] assert type(filter_cutoff) == int assert filter_cutoff > 1 n = int( filter_cutoff / 2 ) + 1 # datapoints to remove from either end due to filter edge effects mc = mc[:, n:-n] if filter_type == 'lowpass': mc = lowpass(mc.T, filter_cutoff).T elif filter_type == 'chebychev': mc = chebychev(mc.T, filter_cutoff).T if spectrum == 'mtm': freq, _ = self.mtspectrum() elif spectrum == 'per': freq, _ = self.periodogram() elif spectrum == 'Welch': freq, _ = self.Welch() mc_spectra = np.zeros((N, len(freq))) # mc_spectra = np.zeros((N, int(len(mc[0,:])/2)+1))#int(self.len/2)+1)) for i in range(N): if spectrum == 'mtm': freq, mc_spectra[i, :] = self.mtspectrum(data=mc[i, :]) elif spectrum == 'per': freq, mc_spectra[i, :] = self.periodogram(data=mc[i, :]) elif spectrum == 'Welch': freq, mc_spectra[i, :] = self.Welch(data=mc[i, :]) mc_spectrum = {} mc_spectrum['median'] = np.median(mc_spectra, axis=0) mc_spectrum['freq'] = freq for p in [1, 2.5, 5, 95, 97.5, 99]: mc_spectrum[str(p)] = np.percentile(mc_spectra, p, axis=0) return mc_spectrum
def filter_timeseries(self, data, filter_type, filter_cutoff): assert filter_type in ['lowpass', 'chebychev'] assert type(filter_cutoff) == int assert filter_cutoff > 1 n = int( filter_cutoff / 2 ) + 1 # datapoints to remove from either end due to filter edge effects if filter_type == 'lowpass': data = lowpass(data, filter_cutoff)[n:-n] elif filter_type == 'chebychev': data = chebychev(data, filter_cutoff)[n:-n] return data