def bandstop(s, f1, f2, order=2, fs=1000.0, use_filtfilt=True): ''' @brief: for a given signal s rejects (attenuates) the frequencies within a certain range (between f1 and f2) and passes the frequencies outside that range by applying a Butterworth digital filter @params: s: array-like signal f1: int the lower cutoff frequency f2: int the upper cutoff frequency order: int Butterworth filter order fs: float sampling frequency @return: signal: array-like filtered signal ''' b, a = signal.butter(order, [f1 / fs, f2 / fs], btype='bandstop') if use_filtfilt: return filtfilt(b, a, s) return signal.lfilter(b, a, s)
def filter(mgr, wp, ws, gpass, gstop, analog=0, ftype='ellip', output='ba', unit='radians', use_filtfilt=False): if unit == 'radians': b,a = signal.iirdesign(wp, ws, gpass, gstop, analog, ftype, output) elif unit == 'hz': sampling = float(mgr.get_param('sampling_frequency')) try: wp = wp/sampling ws = ws/sampling except TypeError: wp = [i/sampling for i in wp] ws = [i/sampling for i in ws] b,a = signal.iirdesign(wp, ws, gpass, gstop, analog, ftype, output) if use_filtfilt: import filtfilt #samples_source = read_data_source.MemoryDataSource(mgr.get_samples(), False) for i in range(int(mgr.get_param('number_of_channels'))): print("FILT FILT CHANNEL "+str(i)) mgr.get_samples()[i,:] = filtfilt.filtfilt(b, a, mgr.get_samples()[i]) samples_source = read_data_source.MemoryDataSource(mgr.get_samples(), False) else: print("FILTER CHANNELs") filtered = signal.lfilter(b, a, mgr.get_samples()) print("FILTER CHANNELs finished") samples_source = read_data_source.MemoryDataSource(filtered, True) info_source = copy.deepcopy(mgr.info_source) tags_source = copy.deepcopy(mgr.tags_source) new_mgr = read_manager.ReadManager(info_source, samples_source, tags_source) return new_mgr
def lowpass(s, f, order=2, fs=1000.0, use_filtfilt=True): ''' @brief: for a given signal s rejects (attenuates) the frequencies higher then the cuttof frequency f and passes the frequencies lower than that value by applying a Butterworth digital filter @params: s: array-like signal f: int the cutoff frequency order: int Butterworth filter order fs: float sampling frequency @return: signal: array-like filtered signal ''' b, a = signal.butter(order, f / fs) if use_filtfilt: return filtfilt(b, a, s) return signal.lfilter(b, a, s)
def lowpass(data, in_t, cutoff, order=4): """ data: vector of data in_t: sample times cutoff: cutoff period in the same units as in_t returns vector same as data, but with high frequencies removed """ # Step 1: Determine dt from data (complicated due to TRIM time series precision legacy) mean_dt = mean(diff(in_t)) # print 'Data Time Step: %8.4f'%mean_dt # test size of data for vectors only Wn = mean_dt / cutoff # 90 = half # of minutes in 3 hours. # print 'Using %1ith Order Butterworth Filter, with %g time-unit cutoff (Wn=%12.6f).'%(order,2*cutoff,Wn) B, A = butter(order, Wn) data_filtered = filtfilt(B, A, data) return data_filtered
def bpFilt(fpL, fpH, fsL, fsH, nyqf, y): nyqf = float(nyqf) ord, wn = filter_design.buttord((fpL / nyqf, fpH / nyqf), (fsL / nyqf, fsH / nyqf), 1, 12, analog=0) b, a = butter(ord, wn, btype="bandpass", analog=0, output='ba') fy = filtfilt(b, a, y) return fy
def filter(mgr, wp, ws, gpass, gstop, analog=0, ftype='ellip', output='ba', unit='radians', use_filtfilt=False): if unit == 'radians': b, a = signal.iirdesign(wp, ws, gpass, gstop, analog, ftype, output) elif unit == 'hz': sampling = float(mgr.get_param('sampling_frequency')) try: wp = wp / sampling ws = ws / sampling except TypeError: wp = [i / sampling for i in wp] ws = [i / sampling for i in ws] b, a = signal.iirdesign(wp, ws, gpass, gstop, analog, ftype, output) if use_filtfilt: import filtfilt #samples_source = read_data_source.MemoryDataSource(mgr.get_samples(), False) for i in range(int(mgr.get_param('number_of_channels'))): print("FILT FILT CHANNEL " + str(i)) mgr.get_samples()[i, :] = filtfilt.filtfilt( b, a, mgr.get_samples()[i]) samples_source = read_data_source.MemoryDataSource( mgr.get_samples(), False) else: print("FILTER CHANNELs") filtered = signal.lfilter(b, a, mgr.get_samples()) print("FILTER CHANNELs finished") samples_source = read_data_source.MemoryDataSource(filtered, True) info_source = copy.deepcopy(mgr.info_source) tags_source = copy.deepcopy(mgr.tags_source) new_mgr = read_manager.ReadManager(info_source, samples_source, tags_source) return new_mgr
def start_CSP(self, signal_time, to_frequency = 128, baseline = True,\ base_time = 4, filt = 'ellip', method = 'pfu', train_tags = None): """Produces CSP filter from the data. THIS VERSION CALCULATES ONE FILTER FOR ALL FREQUENCIES The filter is stored in a variable P Parameters: ----------- signal_time : float Time in seconds of signal to take as a class for maximalization to_frequency [= 128Hz] : int The frequency to which signal will be resampled baseline [= True] : bool If true a base line of base_time seconds will be taken as a class for minimalization [If baseline = True] base_time [= 4] : float Time in seconds of baseline to take as minimalization class filt [= 'ellip']: string ['ellip', 'cov', 'cheby', None] a filter to use. If method is 'maxcontrast' the variable is set to None method [= 'pfu'] : string ['pfu', 'regular','maxcontrast'] method of calculation CSP filter train_tags : list a list of tags to process. Each list entry is a tuple with first element position of tag in seconds, and second is a frequency of stimulation """ if not self.__is_int(to_frequency): raise ValueError, 'to_frequency is not int!' self.method = method signal = self.parsed_data.prep_signal( to_frequency, self.electrodes, montage=self.montage, montage_channels=self.montage_channels) if train_tags == None: all_tags = self.parsed_data.get_train_tags(ccof=True) else: all_tags = train_tags N = len(self.electrodes) if method == 'maxcontrast' or method == 'minimalentropy': baseline = True filt = None cov_pre = np.zeros([N, N]) cov_post = np.zeros([N, N]) pre_i = 0 post_i = 0 for i, frq in enumerate(self.frequencies): if filt == 'ellip': filt_b, filt_a = ellip(3, 0.1 , 100, \ [2*(frq - 1) / float(to_frequency), 2*(frq + 1) / float(to_frequency)],\ btype='pass') signal_tmp = np.array( [filtfilt(filt_b, filt_a, x) for x in signal]) elif filt == 'cheby': filt_b, filt_a = cheby2(1, 10, [ 2 * (frq - 1) / float(to_frequency), 2 * (frq + 1) / float(to_frequency) ], 'pass') signal_tmp = np.array( [filtfilt(filt_b, filt_a, x) for x in signal]) elif filt == 'conv': t_vec = np.linspace(0, 0.5 - 1.0 / to_frequency, 0.5 * to_frequency) sin = np.sin(t_vec * 2 * np.pi) sin /= sum(sin**2) M = len(sin) K = len(signal[0, :]) signal_tmp = np.array([ np.convolve(sin, x, mode='full')[M:K + M] for x in signal ]) elif filt == None: signal_tmp = signal tags = [x for (x, y) in all_tags if y == frq] rest_tags = [x for (x, y) in all_tags if y != frq] for idx in xrange(min(len(tags), len(rest_tags))): s_post = signal_tmp[:, to_frequency * (tags[idx] ) : to_frequency * (tags[idx] +\ signal_time)] dane_B = np.matrix(s_post) R_B = dane_B * dane_B.T / np.trace(dane_B * dane_B.T) cov_post += R_B post_i += 1 if baseline: if method == 'maxcontrast' or method == 'minimalentropy': s_pre = signal_tmp[:, to_frequency *\ (tags[idx] + 1) : to_frequency * (tags[idx] + signal_time)] dane_A = np.matrix(s_pre) X = np.matrix( self.__get_model_matrix(frq, s_pre.shape[1], to_frequency)) Y = dane_A - (X.T * np.linalg.inv(X * X.T) * X * dane_A.T).T cov_pre += Y * Y.T / np.trace(Y * Y.T) pre_i += 1 else: s_pre = signal_tmp[:, to_frequency * (tags[idx] -\ 1 - base_time) : to_frequency * (tags[idx] -1)] dane_A = np.matrix(s_pre) R_A = dane_A * dane_A.T / np.trace(dane_A * dane_A.T) cov_pre += R_A pre_i += 1 if not baseline: for idx in rest_tags: s_pre = signal_tmp[:, to_frequency * (idx ) : to_frequency *\ (idx + signal_time)] dane_A = np.matrix(s_pre) R_A = dane_A * dane_A.T / np.trace(dane_A * dane_A.T) cov_pre += R_A pre_i += 1 if method == 'regular' or method == 'maxcontrast': self.P[:, :], self.vals = self.__get_filter( cov_post / post_i, cov_pre / pre_i) elif method == 'pfu': self.P[:, :] = pfu_csp(cov_pre / pre_i, cov_post / post_i) elif method == 'minimalentropy': self.P[:, :], self.vals = self.__get_min_entropy(cov_pre / pre_i)
def hpFilt(fp, fs, nyqf, y, disp=False): nyqf = float(nyqf) ord, wn = filter_design.buttord(fp / nyqf, fs / nyqf, 1, 12, analog=0) b, a = butter(ord, wn, btype="highpass", analog=0, output='ba') fy = filtfilt(b, a, y) return fy
def start_CSP(self, signal_time, to_frequency = 128, baseline = True,\ base_time = 4, filt = 'ellip', method = 'pfu', train_tags = None): """Produces CSP filter from the data. THIS VERSION CALCULATES ONE FILTER FOR ALL FREQUENCIES The filter is stored in a variable P Parameters: ----------- signal_time : float Time in seconds of signal to take as a class for maximalization to_frequency [= 128Hz] : int The frequency to which signal will be resampled baseline [= True] : bool If true a base line of base_time seconds will be taken as a class for minimalization [If baseline = True] base_time [= 4] : float Time in seconds of baseline to take as minimalization class filt [= 'ellip']: string ['ellip', 'cov', 'cheby', None] a filter to use. If method is 'maxcontrast' the variable is set to None method [= 'pfu'] : string ['pfu', 'regular','maxcontrast'] method of calculation CSP filter train_tags : list a list of tags to process. Each list entry is a tuple with first element position of tag in seconds, and second is a frequency of stimulation """ if not self.__is_int(to_frequency): raise ValueError, 'to_frequency is not int!' self.method = method signal = self.parsed_data.prep_signal(to_frequency, self.electrodes, montage=self.montage, montage_channels=self.montage_channels) if train_tags == None: all_tags = self.parsed_data.get_train_tags(ccof = True) else: all_tags = train_tags N = len(self.electrodes) if method == 'maxcontrast' or method == 'minimalentropy': baseline = True filt = None cov_pre = np.zeros([N, N]) cov_post = np.zeros([N, N]) pre_i = 0 post_i = 0 for i, frq in enumerate(self.frequencies): if filt == 'ellip': filt_b, filt_a = ellip(3, 0.1 , 100, \ [2*(frq - 1) / float(to_frequency), 2*(frq + 1) / float(to_frequency)],\ btype='pass') signal_tmp = np.array([filtfilt(filt_b, filt_a, x) for x in signal]) elif filt == 'cheby': filt_b, filt_a = cheby2(1, 10, [2*(frq - 1)/float(to_frequency), 2*(frq + 1)/float(to_frequency)], 'pass') signal_tmp = np.array([filtfilt(filt_b, filt_a, x) for x in signal]) elif filt == 'conv': t_vec = np.linspace(0, 0.5-1.0/to_frequency, 0.5 * to_frequency) sin = np.sin(t_vec * 2 * np.pi) sin /= sum(sin**2) M = len(sin) K = len(signal[0,:]) signal_tmp = np.array([np.convolve(sin, x, mode = 'full')[M:K + M] for x in signal]) elif filt == None: signal_tmp = signal tags = [x for (x, y) in all_tags if y == frq] rest_tags = [x for (x, y) in all_tags if y != frq] for idx in xrange(min(len(tags),len(rest_tags))): s_post = signal_tmp[:, to_frequency * (tags[idx] ) : to_frequency * (tags[idx] +\ signal_time)] dane_B = np.matrix(s_post) R_B = dane_B * dane_B.T / np.trace(dane_B * dane_B.T) cov_post += R_B post_i += 1 if baseline: if method == 'maxcontrast' or method == 'minimalentropy': s_pre = signal_tmp[:, to_frequency *\ (tags[idx] + 1) : to_frequency * (tags[idx] + signal_time)] dane_A = np.matrix(s_pre) X = np.matrix(self.__get_model_matrix(frq, s_pre.shape[1], to_frequency)) Y = dane_A - (X.T * np.linalg.inv(X * X.T) * X * dane_A.T).T cov_pre += Y * Y.T / np.trace(Y * Y.T) pre_i += 1 else: s_pre = signal_tmp[:, to_frequency * (tags[idx] -\ 1 - base_time) : to_frequency * (tags[idx] -1)] dane_A = np.matrix(s_pre) R_A = dane_A * dane_A.T / np.trace(dane_A * dane_A.T) cov_pre += R_A pre_i += 1 if not baseline: for idx in rest_tags: s_pre = signal_tmp[:, to_frequency * (idx ) : to_frequency *\ (idx + signal_time)] dane_A = np.matrix(s_pre) R_A = dane_A * dane_A.T / np.trace(dane_A * dane_A.T) cov_pre += R_A pre_i += 1 if method == 'regular' or method == 'maxcontrast': self.P[:,:], self.vals = self.__get_filter(cov_post / post_i, cov_pre / pre_i) elif method == 'pfu': self.P[:, :] = pfu_csp(cov_pre / pre_i, cov_post / post_i) elif method == 'minimalentropy': self.P[:, :], self.vals = self.__get_min_entropy(cov_pre / pre_i)
def filtruj(b, a, signal): from filtfilt import filtfilt for tag in range(signal.shape[0]): for chan in range(signal.shape[1]): signal[tag,chan,:] = filtfilt(b, a, signal[tag,chan,:]) return signal
def filtruj(b, a, signal): from filtfilt import filtfilt for tag in range(signal.shape[0]): for chan in range(signal.shape[1]): signal[tag, chan, :] = filtfilt(b, a, signal[tag, chan, :]) return signal