#!/usr/bin/env python from __future__ import print_function, division import sys import numpy as np from scipy import signal try: from future_builtins import zip except ImportError: # either before Python2.6 or one of the Python 3.* try: from itertools import izip as zip except ImportError: # not an early Python, so must be Python 3.* pass sampling_freq = 20 # if not read from input times=[] # time stamps from first column values=[] # data values from second column for line in sys.stdin: if line.startswith('#'): print (line.rstrip()) if line.startswith('# Recording every'): # extract from input: # Recording every 0.05 sec (20.0 Hz) fields = line.split('(') sampling_freq=float(fields[1].split()[0]) continue fields = [x for x in map(float, line.split())] if len(fields)>=2: times.append(fields[0]) values.append(fields[1]) values=np.array(values) # needed because sosfiltfilt omits the cast # define the corner frequencies low_end_cutoff = 0.3 # Hz lo_end_over_Nyquist = low_end_cutoff/(0.5*sampling_freq) high_end_cutoff = 6.0 # Hz hi_end_over_Nyquist = high_end_cutoff/(0.5*sampling_freq) # band-pass filter passes frequencies between low_end_cutoff and high_end_cutoff sos_band = signal.iirfilter(4, Wn=[lo_end_over_Nyquist,hi_end_over_Nyquist], btype="bandpass", ftype='bessel', output='sos') bandpass = signal.sosfiltfilt(sos_band, values) # low-pass filter passes frequencies below low_end_cutoff sos_low = signal.iirfilter(4, Wn=[lo_end_over_Nyquist], btype="lowpass", ftype='bessel', output='sos') lowpass = signal.sosfiltfilt(sos_low, values) print("# Bessel bandpass to {:.3g}Hz to {:.3g}Hz".format(low_end_cutoff, high_end_cutoff)) print("# Bessel lowpass {:.3g}Hz".format(low_end_cutoff)) for t,v,b,l in zip(times,values,bandpass,lowpass): print("{:.7f}\t{:.6f}\t{:.6f}\t{:.6f}".format(t,v,b,l))