def main(): col = columns.Columns() print('Reading data...') data = np.loadtxt(sys.argv[1], dtype=float, delimiter=',', skiprows=9) sc = data[:,col.selectColumn('E: Skin Cond')] # skin conductivity scf = filters.lowpass(2,0.01,sc) scd = filters.detrend(scf) # Make plots fig = plt.figure(1) ax1 = fig.add_subplot(211) plt.plot(range(0,sc.size),sc, 'b-') plt.plot(range(0,sc.size),scf, 'r-',linewidth=2) plt.plot(range(0,sc.size),scd, 'g-',linewidth=2) ax1.axes.get_xaxis().set_visible(False) ax1 = fig.add_subplot(212) plt.plot(range(0,sc.size),sc-scf, 'b-') sp = np.fft.fft(sc) freq = np.fft.fftfreq(sc.shape[-1]) plt.figure(2) plt.plot(freq, sp.real, freq, sp.imag) plt.show()
def main(): col = columns.Columns() print('Reading data...') data = np.loadtxt(sys.argv[1], dtype=float, delimiter=',', skiprows=9) sc = data[:, col.selectColumn('E: Skin Cond')] # skin conductivity scf = filters.lowpass(2, 0.01, sc) scd = filters.detrend(scf) # Make plots fig = plt.figure(1) ax1 = fig.add_subplot(211) plt.plot(range(0, sc.size), sc, 'b-') plt.plot(range(0, sc.size), scf, 'r-', linewidth=2) plt.plot(range(0, sc.size), scd, 'g-', linewidth=2) ax1.axes.get_xaxis().set_visible(False) ax1 = fig.add_subplot(212) plt.plot(range(0, sc.size), sc - scf, 'b-') sp = np.fft.fft(sc) freq = np.fft.fftfreq(sc.shape[-1]) plt.figure(2) plt.plot(freq, sp.real, freq, sp.imag) plt.show()
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
def rotateAcceleration(accel, angles): #angles: 0:roll, 1:pitch, 2:yaw #accel: 0:x, 1:y, 2:z #gravity = rotateGravity(angles) #accel[0] -= gravity[0] #accel[1] -= gravity[1] #accel[2] -= gravity[2] rotAccel = [[0.0 for i in range(len(accel[0]))] for i in range(3)] roll = [math.radians(i) for i in angles[0]] pitch = [math.radians(i) for i in angles[1]] yaw = [math.radians(i) for i in angles[2]] alpha = 0.05 accel[0] = filters.lowpass(filters.highpass(accel[0], alpha), 0.2) accel[1] = filters.lowpass(filters.highpass(accel[1], alpha), 0.2) accel[2] = filters.lowpass(filters.highpass(accel[2], alpha), 0.2) for k in range(len(accel[0])): R = RtoGround(roll[k], pitch[k], yaw[k]) for i in range(3): for j in range(3): rotAccel[i][k] += R[i][j] * accel[j][k] return rotAccel
# df_PDOsplit = pd.DataFrame(standardize.transform(df_PDOsplit['PDO'].values.reshape(-1,1)), # index=df_PDOsplit.index, columns=['PDO']) df_PDOsplit = df_PDOsplit[['PDO']].apply(standardize_on_train, args=[df_PDO.loc[0]['TrainIsTrue']], result_type='broadcast') # Butter Lowpass yr = 2 dates = df_PDOsplit.index freqraw = (dates[1] - dates[0]).days window = int(yr*functions_pp.get_oneyr(dates).size) # 2 year fig, ax = plt.subplots(1,1) ax.plot_date(dates, df_PDOsplit.values, label=f'raw ({freqraw} daymeans)', alpha=.2, linestyle='solid', marker=None) ax.plot_date(dates, filters.lowpass(df_PDOsplit, period=window), label='Butterworth', linestyle='solid', linewidth=1, marker=None) df_PDOrm = df_PDOsplit.rolling(window=window, center=True, min_periods=1).mean() # ax.plot_date(dates, filters.lowpass(df_PDOrm, period=window), label='Butterworth on rolling mean', # linestyle='solid', linewidth=1, marker=None) ax.plot_date(dates, df_PDOrm, label='rolling mean', color='green', linestyle='solid', linewidth=1, marker=None) ax.legend() #%% import wrapper_PCMCI as wPCMCI
args=[df_PDO.loc[0]['TrainIsTrue']], result_type='broadcast') # Butter Lowpass dates = df_PDOsplit.index freqraw = (dates[1] - dates[0]).days ls = ['solid', 'dotted', 'dashdot', 'dashed'] fig, ax = plt.subplots(1,1, figsize=(10,5)) list_dfPDO = [df_PDOsplit] lowpass_yrs = [.25, .5, 1.0, 2.0] for i, yr in enumerate(lowpass_yrs): window = int(yr*functions_pp.get_oneyr(dates).size) # 2 year if i ==0: ax.plot_date(dates, df_PDOsplit.values, label=f'Raw ({freqraw} day means)', alpha=.3, linestyle='solid', marker=None) df_PDObw = pd.Series(filters.lowpass(df_PDOsplit, period=window).squeeze(), index=dates, name=f'PDO{yr}bw') ax.plot_date(dates, df_PDObw, label=f'Butterworth {yr}-year low-pass', color='red',linestyle=ls[i], linewidth=1, marker=None) df_PDOrm = df_PDOsplit.rolling(window=window, closed='right', min_periods=window).mean() df_PDOrm = df_PDOrm.rename({'PDO':f'PDO{yr}rm'}, axis=1) ax.plot_date(dates, df_PDOrm, label=f'Rolling mean {yr}-year low-pass (closed right)', color='green',linestyle=ls[i], linewidth=1, marker=None) list_dfPDO.append(df_PDObw) ; list_dfPDO.append(df_PDOrm) ax.legend() filepath = os.path.join(path_out_main, 'Low-pass_filter.pdf') plt.savefig(filepath, bbox_inches='tight') df_PDOs = pd.concat(list_dfPDO,axis=1)
# Prove we don't need synchronized carrier oscilators initial_carrier_phase = random.random() * 2 * math.pi last_angle = 0.0 istream = [] qstream = [] for n in range(0, input_src.getnframes()): inputsgn = struct.unpack('h', input_src.readframes(1))[0] / 32768.0 # I/Q demodulation, not unlike QAM carrier = 2 * math.pi * FM_CARRIER * (n / 44100.0) + initial_carrier_phase istream.append(inputsgn * math.cos(carrier)) qstream.append(inputsgn * -math.sin(carrier)) istream = filters.lowpass(istream, 1500) qstream = filters.lowpass(qstream, 1500) last_output = 0 for n in range(0, len(istream)): i = istream[n] q = qstream[n] # Determine phase (angle) of I/Q pair angle = math.atan2(q, i) # Change of angle = baseband signal # Were you rushing or were you dragging?! angle_change = last_angle - angle