def _firwin_ord(self, F, W, A, alg): #http://www.mikroe.com/chapters/view/72/chapter-2-fir-filters/ delta_f = abs(F[1] - F[0]) * 2 # referred to f_Ny delta_A = np.sqrt(A[0] * A[1]) if self.fir_window_name == 'kaiser': N, beta = sig.kaiserord(20 * np.log10(np.abs(fb.fil[0]['A_SB'])), delta_f) self.led_firwin_1.setText(str(beta)) fb.fil[0]['wdg_fil'][1] = beta self._update_UI() #self._load_dict() return N if self.firWindow == 'hann': gamma = 3.11 sidelobe = 44 elif self.firWindow == 'hamming': gamma = 3.32 sidelobe = 53 elif self.firWindow == 'blackman': gamma = 5.56 sidelobe = 75 else: gamma = 1 N = remezord(F, W, A, Hz = 1, alg = alg)[0] return N
def test_multi(self): width = 0.04 ntaps, beta = kaiserord(120, width) taps = firwin(ntaps, cutoff=[0.2, 0.5, 0.8], window=("kaiser", beta), pass_zero=True, scale=False) # Check the symmetry of taps. assert_array_almost_equal(taps[: ntaps // 2], taps[ntaps : ntaps - ntaps // 2 - 1 : -1]) # Check the gain at a few samples where we know it should be approximately 0 or 1. freq_samples = np.array( [ 0.0, 0.1, 0.2 - width / 2, 0.2 + width / 2, 0.35, 0.5 - width / 2, 0.5 + width / 2, 0.65, 0.8 - width / 2, 0.8 + width / 2, 0.9, 1.0, ] ) freqs, response = freqz(taps, worN=np.pi * freq_samples) assert_array_almost_equal( np.abs(response), [1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0], decimal=5 )
def highpass(CutOffFreq, SamplingRate, StopGain, TranWidth): NiquistRate = SamplingRate/2.0 N, beta = sig.kaiserord(StopGain,TranWidth/NiquistRate) print 'the order of the FIR filter is:' + str(N) + ', If this is bigger than the size of the data please adjust the width and gain of the filter' taps = sig.firwin(N, CutOffFreq, window=('kaiser', beta), pass_zero=False, scale=True, nyq=NiquistRate) return taps
def lowpass(sig): sample_rate=200 # The Nyquist rate of the signal. nyq_rate = sample_rate / 2.0 # The desired width of the transition from pass to stop, # relative to the Nyquist rate. We'll design the filter # with a 10 Hz transition width. width = 5.0/nyq_rate # The desired attenuation in the stop band, in dB. ripple_db = 10.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) #print "N is " , N # The cutoff frequency of the filter. cutoff_hz = 15 # Use firwin with a Kaiser window to create a lowpass FIR filter. taps = firwin(N, cutoff_hz/nyq_rate, window=('kaiser', beta)) # Use lfilter to filter x with the FIR filter. #print taps delay = 0.5 * (N-1) / sample_rate print "Phase delay is " , delay return lfilter(taps, 1.0, sig)
def filter_sig(sig, width=10 / sampling_rate, ripple_db=60., cutoff_hz=6000): N, beta = signal.kaiserord(ripple_db, width) taps = signal.firwin( N, cutoff_hz * 2 / sampling_rate, window=('kaiser', beta)) filtered_sig = signal.lfilter(taps, 1.0, sig) filtered_sig = np.round(filtered_sig).astype(np.int16) return filtered_sig
def test_lowpass(self): width = 0.04 ntaps, beta = kaiserord(120, width) taps = firwin(ntaps, cutoff=0.5, window=('kaiser', beta)) freq_samples = np.array([0.0, 0.25, 0.5-width/2, 0.5+width/2, 0.75, 1.0]) freqs, response = freqz(taps, worN=np.pi*freq_samples) assert_array_almost_equal(np.abs(response), [1.0, 1.0, 1.0, 0.0, 0.0, 0.0], decimal=5)
def HighPassFilter(data, vel, width=5, linearize=False): """ Function to apply a high-pass filter to data. Data must be in an xypoint container, and have linear wavelength spacing vel is the width of the features you want to remove, in velocity space (in cm/s) width is how long it takes the filter to cut off, in units of wavenumber """ if linearize: original_data = data.copy() datafcn = spline(data.x, data.y, k=3) errorfcn = spline(data.x, data.err, k=3) contfcn = spline(data.x, data.cont, k=3) linear = DataStructures.xypoint(data.x.size) linear.x = np.linspace(data.x[0], data.x[-1], linear.size()) linear.y = datafcn(linear.x) linear.err = errorfcn(linear.x) linear.cont = contfcn(linear.x) data = linear # Figure out cutoff frequency from the velocity. featuresize = 2 * data.x.mean() * vel / constants.c.cgs.value # vel MUST be given in units of cm dlam = data.x[1] - data.x[0] # data.x MUST have constant x-spacing Npix = featuresize / dlam nsamples = data.size() sample_rate = 1.0 / dlam nyq_rate = sample_rate / 2.0 # The Nyquist rate of the signal. width /= nyq_rate cutoff_hz = min(1.0 / featuresize, nyq_rate - width * nyq_rate / 2.0) # Cutoff frequency of the filter # The desired attenuation in the stop band, in dB. ripple_db = 60.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) if N % 2 == 0: N += 1 # Use firwin with a Kaiser window to create a lowpass FIR filter. taps = firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta), pass_zero=False) # Extend data to prevent edge effects y = np.r_[data.y[::-1], data.y, data.y[::-1]] # Use lfilter to filter data with the FIR filter. smoothed_y = lfilter(taps, 1.0, y) # The phase delay of the filtered signal. delay = 0.5 * (N - 1) / sample_rate delay_idx = np.searchsorted(data.x, data.x[0] + delay) smoothed_y = smoothed_y[data.size() + delay_idx:-data.size() + delay_idx] if linearize: fcn = spline(data.x, smoothed_y) return fcn(original_data.x) else: return smoothed_y
def kaiser_design(fs,width_hz,ripple_db,cutoff_hz): # The Nyquist rate of the signal. nyq_rate = fs / 2.0 width = width_hz/nyq_rate # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) # Use firwin with a Kaiser window to create a lowpass FIR filter. taps = firwin(N, cutoff_hz/nyq_rate, window=('kaiser', beta)) return taps
def FIRfilter(signal, rate, numSamples): filtered = signal nyquist = rate/2 width = 5.0/nyquist rippleDB = 60 N, beta = sig.kaiserord(rippleDB, width) cutoffHz = 20 taps = sig.firwin(N, nyquist/2000, window=('kaiser', beta), nyq = nyquist) filtered = sig.lfilter(taps, 1.0, signal) return filtered
def test03(self): width = 0.02 ntaps, beta = kaiserord(120, width) # ntaps must be odd for positive gain at Nyquist. ntaps = int(ntaps) | 1 freq = [0.0, 0.4, 0.4, 0.5, 0.5, 1.0] gain = [1.0, 1.0, 0.0, 0.0, 1.0, 1.0] taps = firwin2(ntaps, freq, gain, window=("kaiser", beta)) freq_samples = np.array([0.0, 0.4 - width, 0.4 + width, 0.45, 0.5 - width, 0.5 + width, 0.75, 1.0]) freqs, response = freqz(taps, worN=np.pi * freq_samples) assert_array_almost_equal(np.abs(response), [1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0], decimal=5)
def low_pass_filter(cutoff,width,sample_rate) : ''' Get low pass filter kernel for cutoff freq in Hz width in Hz Borrowed from http://www.scipy.org/Cookbook/FIRFilter ''' nyq_rate = sample_rate / 2.0 width = width/nyq_rate # normalize to Nyq Rate ripple_db = 30.0 # 60 dB attenuation in the stop band # Compute the order and Kaiser parameter for the FIR filter. N,beta = kaiserord(ripple_db, width) cutoff=cutoff/nyq_rate taps = firwin(N, cutoff, window=('kaiser', beta)) return taps
def bandreject(self, S, low=700, high=800): # http://www.scipy.org/Cookbook/FIRFilter # http://mpastell.com/2010/01/18/fir-with-scipy/ nyq_rate = self.spl_rate / 2.0 width = 50.0 / nyq_rate ripple_db = 60.0 N, beta = signal.kaiserord(ripple_db, width) tapsL = signal.firwin(N, low / nyq_rate, window=("kaiser", beta)) tapsH = signal.firwin(N, high / nyq_rate, window=("kaiser", beta)) tapsB = -(tapsL + tapsH) tapsB[N / 2] = tapsB[N / 2] + 1 return signal.lfilter(tapsB, 1.0, S)
def test_lowpass(self): width = 0.04 ntaps, beta = kaiserord(120, width) taps = firwin(ntaps, cutoff=0.5, window=('kaiser', beta), scale=False) # Check the symmetry of taps. assert_array_almost_equal(taps[:ntaps//2], taps[ntaps:ntaps-ntaps//2-1:-1]) # Check the gain at a few samples where we know it should be approximately 0 or 1. freq_samples = np.array([0.0, 0.25, 0.5-width/2, 0.5+width/2, 0.75, 1.0]) freqs, response = freqz(taps, worN=np.pi*freq_samples) assert_array_almost_equal(np.abs(response), [1.0, 1.0, 1.0, 0.0, 0.0, 0.0], decimal=5)
def bandpass(data, sampling, fmin, fmax, ripple_db=50, width=2.0,\ return_filter=False, verbose=False): """ This function will bandpass filter data in the given [fmin,fmax] band using a kaiser window. Arguments: data : numpy.ndarray array of data points sampling : int number of data points per second fmin : float frequency of lowpass fmax : float frequency of highpass Keyword arguments: ripple_db : int Attenuation in the stop band, in dB width : float Desired width of the transition from pass to stop, in Hz return_filter: boolean Return filter verbose : boolean """ # construct filter order, beta = signal.kaiserord(ripple_db, width*2/sampling) lowpass = signal.firwin(order, fmin*2/sampling, window=('kaiser', beta)) highpass = - signal.firwin(order, fmax*2/sampling, window=('kaiser', beta)) highpass[order//2] = highpass[order//2] + 1 bandpass = -(lowpass + highpass); bandpass[order//2] = bandpass[order//2] + 1 # filter data forward then backward data = signal.lfilter(bandpass,1.0,data) data = data[::-1] data = signal.lfilter(bandpass,1.0,data) data = data[::-1] if verbose: sys.stdout.write("Bandpass filter applied to data.\n") if return_filter: return data, bandpass else: return data
def decimator_design(f0, fr_w, fs, att, force_n = None): fpass = f0[-1] + frw[-1]/(2*np.pi) fstop = fs/2 - fpass n, beta = sig.kaiserord(att, 2*(fstop-fpass)/float(fs)) if force_n is not None: if force_n < n: raise Exception('Required order is more than specified') else: n = force_n h = sig.firwin(n, (fstop+fpass)/2, fstop-fpass,'kaiser', nyq = fs/2) return fpass, fstop, h
def low_pass_filter(torig, Corig): sample_rate = 50 # per second nyq_rate = sample_rate / 2.0 # The Nyquist rate of the signal width = 5.0 / nyq_rate # the desired width of the transition from pass to stop, # relative to the Nyquist rate ripple_db = 60.0 # the desired attenuation in the stop band, in dB N, beta = kaiserord(ripple_db, width) # compute the order and kaiser parameter # for the FIR filter cutoff_hz = 2 # the cutoff frequency of the filter taps = firwin(N, cutoff_hz / nyq_rate, window=("kaiser", beta)) # use firwin with a Kaiser # window to create a # lowpass FIR filter C = lfilter(taps, 1.0, Corig) delay = 0.5 * (N - 1) / sample_rate t = torig - delay return (t, C)
def kaiser_lowpass(delta_db, cutoff, width, fs): """ Design a lowpass filter using the Kaiser window method. """ # Convert to normalized frequencies nyq = 0.5*fs cutoff = cutoff / nyq width = width / nyq # Design the parameters for the Kaiser window FIR filter. numtaps, beta = kaiserord(delta_db, width) numtaps |= 1 # Ensure a Type I FIR filter. taps = firwin(numtaps, cutoff, window=('kaiser', beta), scale=False) return taps, beta
def kaiser_filter(func, mask, cutoff=0.1): """ Low-passes each time series using a bi-directional FIR kaiser filter. Useful in cases where the preservation of phase information is more important than strong attenuation of high frequencies. The default cutoff is the traditional resting-state cutoff. """ # load in everything func, func_aff, func_head, func_dims = loadnii(func) mask, mask_aff, mask_head, mask_dims = loadnii(mask) tmp, idx = maskdata(func, mask) # init output array filt = np.zeros(tmp.shape) # get sampling rate, nyquist frequency TR_len = func_head.values()[15][4] if TR_len > 1000: TR_len = TR_len / 1000.0 samp_rate = 1.0/TR_len nyq = samp_rate/2.0 # return a kaiser window with 60 Hz attenuation over a 0.1 Hz transition width = 0.1 ripple_db = 60.0 numtap, beta = sig.kaiserord(ripple_db, width) # enforce odd filter order if np.remainder(numtap, 2.0) == 0: numtap = numtap -1 # design and apply lowpass filter b = sig.firwin(numtap, cutoff/nyq, window=('kaiser', beta)) a = [1.0] for x in np.arange(tmp.shape[0]): filt[x, :] = sig.filtfilt(b, a, tmp[x, :], axis=0) # create a 4D output array output = np.zeros(func.shape) output[idx, :] = filt output = output.reshape(func_dims) output_aff = func_aff output_head = func_head return output, output_aff, output_head
def design(self, sample_rate, cutoff_hz, width, ripple_db): #------------------------------------------------ # Create a FIR filter and apply it to x. #------------------------------------------------ self.sample_rate = sample_rate nyq_rate = self.nyq_rate = sample_rate / 2.0 self.cutoff_hz = cutoff_hz self.width = width # The desired width of the transition from pass to stop, # relative to the Nyquist rate. We'll design the filter # with a 5 Hz transition width. #width = (audio_min * 2)/nyq_rate # The desired attenuation in the stop band, in dB. #ripple_db = 40.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width/nyq_rate) # Use firwin with a Kaiser window to create a lowpass FIR filter. taps = firwin(N, cutoff_hz/nyq_rate, window=('kaiser', beta)) self.unweighted_taps = array(taps) non_zero_coeffs = len([i for i in taps if int(i*10000000) != 0]) print 'cost', non_zero_coeffs, 'taps', N #Scaling factor input_bit_width = self.input.bits accumulator_bit_width = 9 + 18 + 1 #2 * input_bit_width + 1 sum_interval = (-2**(accumulator_bit_width-1), 2**(accumulator_bit_width-1) - 1) product_interval = (sum_interval[0] / non_zero_coeffs, sum_interval[1] / non_zero_coeffs) input_interval = (-2**(input_bit_width-1), 2**(input_bit_width-1) - 1) tap_interval = (- (product_interval[0] / input_interval[0]), product_interval[1] / input_interval[1]) tap_max = max(abs(tap_interval[0]), tap_interval[1]) print sum_interval, product_interval, input_interval, tap_interval fudge_factor = 2**(log(non_zero_coeffs, 2)-1) self.taps = (taps * tap_max * fudge_factor).astype(int32) import numpy as np assert np.all([abs(t) < 2**17-1 for t in taps]) print 'taps', [t for t in self.taps] return self.taps
def test_nyq(self): """Test the nyq keyword.""" nyquist = 1000 width = 40.0 relative_width = width / nyquist ntaps, beta = kaiserord(120, relative_width) taps = firwin(ntaps, cutoff=[300, 700], window=("kaiser", beta), pass_zero=False, scale=False, nyq=nyquist) # Check the symmetry of taps. assert_array_almost_equal(taps[: ntaps // 2], taps[ntaps : ntaps - ntaps // 2 - 1 : -1]) # Check the gain at a few samples where we know it should be approximately 0 or 1. freq_samples = np.array( [0.0, 200, 300 - width / 2, 300 + width / 2, 500, 700 - width / 2, 700 + width / 2, 800, 1000] ) freqs, response = freqz(taps, worN=np.pi * freq_samples / nyquist) assert_array_almost_equal(np.abs(response), [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0], decimal=5)
def convert_to_pow(fdata=[],sample_time=0.5): """Converts the signal to a list of powers in alpha and beta bands. **Keyword arguments:** * fdata -- signal (list of samples) * sample_time -- interval between samples Returns a list of powers in alpha/beta bands over time.""" N, beta = signal.kaiserord(80, 0.1) falpha = signal.firwin(N, [freq0, freq1], pass_zero=False, nyq = 64.0) fbeta = signal.firwin(N, [freq1, freq2], pass_zero=False, nyq = 64.0) sample_size = int(128.0 * sample_time) i = 0 out_data = [] tmp = {'F3':[], 'F4':[], 'P7':[], 'FC6':[], 'F7':[], 'F8':[], 'T7':[], 'P8':[], 'AF4':[], 'T8':[], 'AF3':[], 'O2':[], 'O1':[], 'FC5':[]} for x in fdata: i += 1 for n in emotiv.channels: tmp[n].append(x.sensors[n]['value']) if i == sample_size: i = 0 dumpable = [] for n in emotiv.channels: sample = tmp[n] sample_alpha = signal.lfilter(falpha, 1.0, sample) sample_beta = signal.lfilter(fbeta, 1.0, sample) power_alpha = reduce(lambda x, y: x + y, map(lambda z: z * z, sample_alpha)) / (sample_size * 1000000.0) power_beta = reduce(lambda x, y: x + y, map(lambda z: z * z, sample_beta)) / (sample_size * 1000000.0) dumpable.append( power_alpha ) dumpable.append( power_beta ) tmp[n] = [] out_data.append(dumpable) return out_data
def test_bandpass(self): width = 0.04 ntaps, beta = kaiserord(120, width) kwargs = dict(cutoff=[0.3, 0.7], window=('kaiser', beta), scale=False) taps = firwin(ntaps, pass_zero=False, **kwargs) # Check the symmetry of taps. assert_array_almost_equal(taps[:ntaps//2], taps[ntaps:ntaps-ntaps//2-1:-1]) # Check the gain at a few samples where we know it should be approximately 0 or 1. freq_samples = np.array([0.0, 0.2, 0.3-width/2, 0.3+width/2, 0.5, 0.7-width/2, 0.7+width/2, 0.8, 1.0]) freqs, response = freqz(taps, worN=np.pi*freq_samples) assert_array_almost_equal(np.abs(response), [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0], decimal=5) taps_str = firwin(ntaps, pass_zero='bandpass', **kwargs) assert_allclose(taps, taps_str)
def fir_filter(datalist, sampling_interval, cutoff=450.0, rolloff=10.0): """Filters hdf5 array data through a bandpass filter with upper cut off frequency of cutoff""" if not datalist: print "Empty data list" return [] nyquist_rate = 0.5 / sampling_interval width = rolloff / nyquist_rate ripple_db = 60.0 N, beta = signal.kaiserord(ripple_db, width) taps = signal.firwin(N, cutoff / nyquist_rate, window=("kaiser", beta)) filtered_data_list = [] for data in datalist: if not isinstance(data, np.ndarray): tmp = np.zeros(len(data)) tmp[:] = data[:] data = tmp filtered_data_list.append(signal.lfilter(taps, 1.0, data)) return filtered_data_list
def __init__(self, from_rate, to_rate, depth = 32): self.from_rate = from_rate self.to_rate = to_rate self.depth = depth samp_rate = calc_samp_rate(self.from_rate, self.to_rate) nyq_rate = calc_nyq_rate(samp_rate) print("upsample rate: %f kHz\n" % (samp_rate/1000)) self.ups_ratio = int(samp_rate / self.from_rate) self.dec_ratio = int(samp_rate / self.to_rate) print("ups: %d, dec: %d\n" % (self.ups_ratio, self.dec_ratio)) # width = 5.0/nyq_rate width = 100.0/nyq_rate ripple_db = 30.0 N, beta = signal.kaiserord(ripple_db, width) print("suggested N: %d, beta: %d" % (N,beta)) beta = 6 N = self.depth * self.ups_ratio print("N: %d, beta: %d" % (N,beta)) print("polyphase depth: %d\n" % (N/self.ups_ratio)) # reqmem = N * 16 / 1024.0 / 2; # print("reqmem: %fKb\n" % reqmem) audible_freq = 22000.0 w = 'blackmanharris' # w = ('kaiser', beta) self.taps = signal.firwin(N, cutoff = audible_freq, window = w, nyq = nyq_rate) self.htaps = half_filter(self.taps) self.rhtaps = reorder_half_filter(self.htaps, self.ups_ratio, self.dec_ratio) self.tapsbits = 24 self.rhetaps = float_to_fixed(self.rhtaps * self.ups_ratio, self.tapsbits) self.srcbits = 24
def keiser_bandpass_filter(signal, signal_rate, lowcut, highcut): """ Apply bandpass filter. Everything below 'lowcut' and above 'highcut' frequency level will be greatly reduced. Keiser edition. """ # The Nyquist rate of the signal. nyq_rate = 0.5 * signal_rate # The desired width of the transition from pass to stop, # relative to the Nyquist rate. We'll design the filter # with a 5 Hz transition width. width = 5.0 / nyq_rate # The desired attenuation in the stop band, in dB. ripple_db = 60.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) # Use firwin with a Kaiser window to create a lowpass FIR filter. lowpass = firwin(N, lowcut / nyq_rate, window=('kaiser', beta)) highpass = - firwin(N, highcut / nyq_rate, window=('kaiser', beta)) highpass[N/2] = highpass[N/2] + 1 bandpass = - (lowpass + highpass) bandpass[N/2] = bandpass[N/2] + 1 # Use lfilter to filter x with the FIR filter. filteredSignal = lfilter(bandpass, 1.0, signal) return filteredSignal
def build_filter(fs,cutoff,width,attenuation): """ Building low pass filter impulse response0 :param fs: sampling rate :return: 1D array impulse response of lp filter """ # define params: nyq_rate = fs / 2.0 # Nyquist frequency if type(cutoff)==list: cutoff = [c/nyq_rate for c in cutoff] else: cutoff=cutoff/nyq_rate # Compute the order and Kaiser parameter for the FIR filter: N, beta = kaiserord(attenuation, width/nyq_rate) # Use firwin with a Kaiser window to create a lowpass FIR filter: if N%2==0: N+=1# odd trick taps = firwin(N, cutoff, window=('kaiser', beta),pass_zero=True) return taps
def processExampleWiFi(self, example_label, dir_name): # Process ONLY one specific sequence example_label example_file = os.path.join(dir_name, example_label) exp_data = spio.loadmat(example_file) complexSignal = exp_data['complexSignal'][0] central_freq = exp_data['central_freq'][0][0] freq_low = exp_data['freq_low'][0][0] freq_high = exp_data['freq_high'][0][0] fs = exp_data['fs'][0][0] # 2e-6*Fs, number of guard samples before and after the signal (this is given by DARPA) guardSamp = self.num_guard_samp * fs # it is a 5GHz signal. note that the same annotation is not available in the 5GHz # dataset annotations if self.signal_BW_useful: signal_BW_useful = self.signal_BW_useful else: if freq_high > 3e9: signal_BW_useful = self.signal_BW_useful_5ghz else: signal_BW_useful = self.signal_BW_useful_2ghz # WiFi Signal information signal_BW_full = freq_high - freq_low guard_BW_wifi_singleside = (signal_BW_full - signal_BW_useful) / 2 signal_BW_wifi_singleside = signal_BW_full / 2 - guard_BW_wifi_singleside # Channel Frequency of the Example f_channel = (freq_high - (freq_high - freq_low) / 2) # Get Signal y = complexSignal t = (np.arange(y.shape[0]) + 1) / fs # Move Signal to Base Band and center it around 0 f_shift = (freq_high - central_freq) - signal_BW_full / 2 y_base = np.multiply(y, np.conj(np.exp(1j * 2 * np.pi * f_shift * t))) # Design Filter 10MHz wide + bandguard fcuts = [signal_BW_wifi_singleside, signal_BW_wifi_singleside+self.percentage_guardband_include_filter \ *signal_BW_wifi_singleside] width = fcuts[1] - fcuts[0] cutoff = fcuts[0] + width / 2 n, beta = kaiserord(40, width / (0.5 * fs)) hh = firwin(n, cutoff, window=('kaiser', beta), scale=False, nyq=0.5 * fs) # Add padding to the signal y_base_padded = np.pad(y_base, (0, n), 'constant', constant_values=0) # Filter Signal and remove padding (recall it is base band) filtered_signal = lfilter(hh, 1, y_base_padded) filtered_signal = filtered_signal[int(n / 2 - 1):int(y_base.shape[0] + n / 2 - 1)] # Get Noise Measurements for SNR AFTER-Filter guard_left_signal = filtered_signal[:int(guardSamp)] guard_right_signal = filtered_signal[int(filtered_signal.shape[0] - guardSamp):] actual_signal = filtered_signal[int(guardSamp ):int(filtered_signal.shape[0] - guardSamp)] power_signal = np.sum(np.abs(actual_signal)** 2) / actual_signal.shape[0] power_noise = np.min((np.sum(np.abs(guard_left_signal)**2)/guard_left_signal.shape[0], \ np.sum(np.abs(guard_right_signal)**2)/guard_right_signal.shape[0])) SNR = power_signal / power_noise SNRdB = 20 * np.log10(SNR) try: os.mkdir(os.path.join(dir_name, 'filtered_sig')) except: pass spio.savemat(os.path.join(dir_name, 'filtered_sig', example_label+'_filtered.mat'), \ {'f_sig': filtered_signal, \ 'SNRdb':SNRdB, 'f_channel':f_channel, \ 'freq_high':freq_high, 'freq_low':freq_low, 'fs': fs})
# plt.plot(ref_x1, ref_u1, 'bo') # plt.plot(int_x1, int_u1,'ro') # plt.show() # from http://wiki.scipy.org/Cookbook/FIRFilter sample_rate = 1/(int_x1[1]-int_x1[0]) t = int_x1 x = int_u1 # print "sample_rate = {0}".format(sample_rate) nyq_rate = sample_rate / 2.0 width = 5.0/nyq_rate ripple_db = 30.0 N, beta = kaiserord(ripple_db, width) cutoff_hz = 60.0 taps = firwin(N, cutoff_hz/nyq_rate, window=('kaiser', beta)) filtered_x = lfilter(taps, 1.0, x) # The phase delay of the filtered signal. delay = 0.5 * (N-1) / sample_rate # # Plot the raw data # plot(ref_x1, ref_u1, 'bo') # # Plot the original signal (interpolated). # plot(t, x, 'ro') # # Plot the filtered signal, shifted to compensate for the phase delay. # plot(t-delay, filtered_x, 'r-') # # Plot just the "good" part of the filtered signal. The first N-1 # # samples are "corrupted" by the initial conditions.
coeff_hpp = env.get_template("templates/fir_coeff.tpl") # PRINT FILTER PARAMETERS print('sample rate: ' + str(component.sr)) print('transition bw: ' + str(component.tbw)) print('stop-band attenuation: ' + str(component.sba)) print('cutoff freq: ' + str(component.cut)) print('-------------------------------') #------------------------------------------------ # Create a FIR filter and apply it to x. #------------------------------------------------ # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(component.sba, component.tbw) print('resulting filter order: ' + str(N)) # Use firwin with a Kaiser window to create a lowpass FIR filter. taps = firwin(N, component.cut, window=('kaiser', beta)) #------------------------------------------------ # Writes coefficients and metadata to header file #------------------------------------------------ fir = {} fir['dtype'] = 'double' fir['type'] = 'lpf' fir['name'] = component.name fir['num_coeff'] = N fir['cutoff'] = component.cut
def test_kaiserord(): assert_raises(ValueError, kaiserord, 1.0, 1.0) numtaps, beta = kaiserord(2.285 + 7.95 - 0.001, 1/np.pi) assert_equal((numtaps, beta), (2, 0.0))
def MCD_read(MCDFilePath): # open file using the neuroshare bindings fd = ns.File(MCDFilePath) #entity_type: 3 is spikes, 2 in analogue for i, entity in enumerate(fd.entities): print((i, entity.label, entity.entity_type)) # (108, 'filt0001 0059 0048 57', 2) # 0059 is the number of the corresponding channel in spk, 0048 is the number in analogue # 57 is name of channel in matrix notation, 8*8 - 4 on corner channels # use matrix notation to spk to lfp #create empty dictionary data = dict() numCh = 60 analog1 = fd.entities[numCh] # open analog signal entity print(analog1.units) #V print(analog1.sample_rate) # 25,000/second temp_data_fft = dict() #get E names chNamesList = get_chNamesList(fd, LFP='True') fft_byE = pd.DataFrame(0, index=chNamesList, columns=('delta', 'theta', 'alpha', 'beta', 'gamma')) chNamesList_spikes = get_chNamesList(fd, LFP='False') #spikes=get_spikes(fd, chNamesList_spikes) for numCh in range(60, 120): print("numCh is " + str(numCh)) analog1 = fd.entities[numCh] # open analog signal entity entity = fd.entities[numCh] print(fd.entities[numCh].label, entity.entity_type) # spikes is 0-59 # filt0001 is 60 on # len(fd.entities) 120 data1, times, count = analog1.get_data() # count 7,657,500 is number of samples # times a numeric array of when samples took place (s) # analog1.entity_type # create channel names channelName = entity.label[0:4] + entity.label[23:] channelNum = channelName.split(" ")[2] # store data with name in the dictionary data2 = np.array(data1) temp_data_fft = np.zeros(shape=(math.floor(max(times)), 50)) sec = 1 totalSec = math.floor(max(times)) # August 10, 2019 downsample to 1k/sec from 25k/sec data3 = data2[0:(totalSec * 25000)] # remove tail partial second # August 10, 2019 low pass FIR filter to eliminate aliasing sample_rate = 25000 # The Nyquist rate of the signal. nyq_rate = sample_rate / 2.0 # The desired width of the transition from pass to stop, # relative to the Nyquist rate. We'll design the filter # with a 5 Hz transition width. width = 5.0 / nyq_rate # The desired attenuation in the stop band, in dB. ripple_db = 60.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) # The cutoff frequency of the filter. cutoff_hz = 200.0 # Use firwin with a Kaiser window to create a lowpass FIR filter. taps = firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta)) # Use lfilter to filter x with the FIR filter. data4 = lfilter(taps, 1.0, data3) #down sample to 1000 samples/sec data[channelName] = signal.resample(data4, num=(1000 * totalSec), t=None, axis=0, window=None) #make an empty data frame fft_r = pd.DataFrame(0, index=np.arange(totalSec * 2), columns=('delta', 'theta', 'alpha', 'beta', 'gamma')) # fft_r.loc[:,"alpha" ], fft_r.loc[1,:] iterations = np.arange(1, totalSec, 0.5) for sec in iterations: fs = 1000 #August 10, 2019: move along the signal in 0.5s increments # take 2 full seconds of data start_signal = int((sec - 1) * fs) end_signal = int((sec) * fs) curData_temp = data[channelName][start_signal:end_signal] beta = 0.5 # default in matlab documentation w_kaiser = signal.get_window(window=('kaiser', beta), Nx=fs, fftbins=False) curData = w_kaiser * curData_temp # element wise operation #band pass filter # she would use firwin to create the filter, convolve will take the filter order = 3 #order of filter is same as number of obs that go into filter def butter_bandpass(lowcut, highcut, fs, order=order): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq b, a = signal.butter(N=order, Wn=[low, high], btype='bandpass') return b, a def butter_bandpass_filter(data, lowcut, highcut, fs, order=order): # b in coming out nan, a is fine b, a = butter_bandpass(lowcut, highcut, fs, order=order) y = lfilter(b, a, data) return y #sample rate and desired cutoff frequencies in Hz band_want = "delta" lowcut = 1 highcut = 4 y = butter_bandpass_filter(curData, lowcut=lowcut, highcut=highcut, fs=1000) power_delta = get_fft(y, band_want) fft_r.loc[sec * 2, "delta"] = power_delta band_want = "theta" lowcut = 5 highcut = 8 y = butter_bandpass_filter(curData, lowcut=lowcut, highcut=highcut, fs=1000) power_theta = get_fft(curData, band_want) fft_r.loc[sec * 2, "theta"] = power_theta band_want = "alpha" lowcut = 9 highcut = 14 y = butter_bandpass_filter(curData, lowcut=lowcut, highcut=highcut, fs=1000) power_alpha = get_fft(curData, band_want) fft_r.loc[sec * 2, "alpha"] = power_alpha band_want = "beta" lowcut = 15 highcut = 30 y = butter_bandpass_filter(curData, lowcut=lowcut, highcut=highcut, fs=1000) power_beta = get_fft(curData, band_want) fft_r.loc[sec * 2, "beta"] = power_beta band_want = "gamma" lowcut = 31 highcut = 50 y = butter_bandpass_filter(curData, lowcut=lowcut, highcut=highcut, fs=1000) power_gamma = get_fft(curData, band_want) fft_r.loc[sec * 2, "gamma"] = power_gamma #end of for loop # do averaging across all seconds for each band put in the right electrode fft_byE.loc[channelNum, :] = fft_r.mean(axis=0) # end of loop through Channels return fft_byE
def LP_filter_preprocess_signal(signal_data, T, num_samples, f_s): nyq_rate = f_s / 2.0 # The desired width of the transition from pass to stop, relative to the Nyquist rate. We'll design the filter # with a 5 Hz transition width. # width = 5.0 / nyq_rate # FIXME: large the (5.) distortion span is less ! width = 100.0 / nyq_rate # GOOD; but smoothing is higher # width = 20.0 / nyq_rate # The desired attenuation in the stop band, in dB. ripple_db = 60.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) # The cutoff frequency of the filter. cutoff_hz = 60.0 # Use firwin with a Kaiser window to create a lowpass FIR filter. taps = firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta)) # Use lfilter to filter x with the FIR filter. filtered_x = lfilter(taps, 1.0, signal_data) # ------------------------------------------------ # Plot the FIR filter coefficients. # ------------------------------------------------ if DO_PLOT and not DO_DISABLE: plt.figure(3) plt.plot(taps, 'bo-', linewidth=2) plt.title('Filter Coefficients (%d taps)' % N) plt.grid(True) plt.savefig("FIR_filter_coefficients.png") # ------------------------------------------------ # Plot the magnitude response of the filter. # ------------------------------------------------ w, h = freqz(taps, worN=8000) if DO_PLOT and not DO_DISABLE: plt.figure(4) plt.clf() plt.plot((w / np.pi) * nyq_rate, np.absolute(h), linewidth=2) plt.xlabel('Frequency (Hz)') plt.ylabel('Gain') plt.title('Frequency Response') plt.ylim(-0.05, 1.05) plt.grid(True) # Upper inset plot. if DO_PLOT and not DO_DISABLE: ax1 = plt.axes([0.42, 0.6, .45, .25]) plt.plot((w / np.pi) * nyq_rate, np.absolute(h), linewidth=2) plt.xlim(0, 8.0) plt.ylim(0.9985, 1.001) plt.grid(True) # Lower inset plot if DO_PLOT and not DO_DISABLE: ax2 = plt.axes([0.42, 0.25, .45, .25]) plt.plot((w / np.pi) * nyq_rate, np.absolute(h), linewidth=2) plt.xlim(12.0, 20.0) plt.ylim(0.0, 0.0025) plt.grid(True) plt.savefig("FIR_filter_magnitude_response.png") # ------------------------------------------------ # Plot the original and filtered signals. # ------------------------------------------------ # The phase delay of the filtered signal. delay = 0.5 * (N - 1) / f_s t = np.linspace(0, 1, num_samples) front_pad = np.zeros(int(np.floor((N - 1) / 2))) back_pad = np.zeros(int(np.ceil((N - 1) / 2))) processed_sig = np.append(np.append(front_pad, filtered_x[N - 1:]), back_pad) if DO_PLOT: plt.figure(5) # Plot the original signal. plt.plot(t, signal_data) # Plot the filtered signal, shifted to compensate for the phase delay. # plt.plot(t - delay, filtered_x, 'r-') # FIXME: # Plot just the "good" part of the filtered signal. The first N-1 # samples are "corrupted" by the initial conditions. # plt.plot(t[N - 1:] - delay, filtered_x[N - 1:], 'g', linewidth=2) # FIXME: plt.xlabel('t') plt.grid(True) plt.plot(t, processed_sig, 'g', linewidth=2) plt.savefig("FIR_LP_filtered_signal.png") plt.show() return processed_sig, T, N, f_s
import os import glob import matplotlib.pyplot as pl import matplotlib as mpl import seaborn as sb import scipy.signal as sp stopgain=-28 transwidth=0.1 samplingrate=25 PassZero=1 Nyq=samplingrate/2 cutoff=0.21 N,beta=sp.kaiserord(stopgain,transwidth/Nyq) cutoff1d=[0.0,cutoff/Nyq] alpha=0.5*(N-1) m=[] h=[] c=[] FIR=[] for i in range(N): m.append(i-alpha) h.append(cutoff1d[1]*np.sinc(cutoff1d[1]*m[i])) # If passzero=0, something here left=cutoff1d[0]
# normalized_audio = [i/max(one_channel_data) for i in one_channel_data] max_value = max(one_channel_data) normalized_audio = list(map(lambda x: x/max_value, one_channel_data)) sig.plotFFT(normalized_audio,samplerate) normalized_audio += [0]*20000 plt.title("Fourier do audio normalizado") plt.show() #exemplo de filtragem do sinal yAudioNormalizado # https://scipy.github.io/old-wiki/pages/Cookbook/FIRFilter.html nyq_rate = samplerate/2 width = 5.0/nyq_rate ripple_db = 60.0 #dB N , beta = signal.kaiserord(ripple_db, width) cutoff_hz = 4000.0 taps = signal.firwin(N, cutoff_hz/nyq_rate, window=('kaiser', beta)) filtered_audio = signal.lfilter(taps, 1.0, normalized_audio) sig.plotFFT(filtered_audio,samplerate) plt.title("Fourier do audio filtrado") plt.show() x,carrier = sig.generateSin(freq,amplitude,len(filtered_audio)/samplerate,samplerate) am_audio = list(map(lambda x,y: (x)*y, filtered_audio,carrier)) # am_audio = filtered_audio*carrier print((len(filtered_audio))) plt.plot(x,am_audio) sig.plotFFT(am_audio,samplerate) plt.title("Fourier do audio AM")
import numpy as np from matplotlib import pyplot as plt from scipy import signal sample_rate = 48e3 cutoff_hz = 8e3 width_hz = 1e3 atten_db = 60 nyq_rate = sample_rate / 2.0 N, beta = signal.kaiserord(atten_db, width_hz / nyq_rate) taps = signal.firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta)) n = np.arange(N) plt.plot(n, taps) plt.title('Fir Taps') plt.ylabel('Tap Value') plt.xlim(0, N) plt.xlabel('Tap') plt.savefig('fir.png') plt.close() _, h = signal.freqz(taps) w = np.linspace(0, nyq_rate, len(h)) plt.plot(w, 20 * np.log10(abs(h)), 'b') plt.title('Frequency Response') plt.ylim(-90, 20) plt.xlim(0, nyq_rate) plt.ylabel('Amplitude (dB)') plt.xlabel('Frequncy (Hz)') plt.savefig('freq-response.png')
import matplotlib.pyplot as plt def plot_response(fs, w, h, title): "Utility function to plot response functions" fig = plt.figure() ax = fig.add_subplot(111) ax.plot(0.5 * fs * w / np.pi, 20 * np.log10(np.abs(h))) ax.set_xlim(0, 0.5 * fs) ax.grid(True) ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('Gain (dB)') ax.set_title(title) fs = 16000.0 nyq_rate = fs / 2 cutoff = 4000.0 trans_width = 100 ripple_db = 60.0 numtaps = 64 N, beta = signal.kaiserord(ripple_db, trans_width / nyq_rate) print('order', N) taps = signal.firwin(N, cutoff / nyq_rate, window=('kaiser', beta)) w, h = signal.freqz(taps, [1], worN=2000) reversed = taps[::-1] print('taps', taps) print('reversed', reversed) print('polyphase', repr(reversed[1::2]), repr(reversed[0::2])) plot_response(fs, w, h, "Low-pass Filter") plt.show()
nyq_rate = sample_rate / 2.0 # The desired width of the transition from pass to stop, # relative to the Nyquist rate. We'll design the filter # with a 5 Hz transition width. width = 5.0 / nyq_rate # The desired attenuation in the stop band, in dB. ripple_db = 60.0 #------------------------------------------------------------------------------------------ # Create FIR Filter and apply to x # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) # The cutoff frequency of the filter. low = 20.0 high = 20000.0 # For the sake of FIR filter, take order as 11 to get better gain. N1 = 11 # Use firwin with a Kaiser window to create a bandpass FIR filter. taps = firwin(N1, [low / nyq_rate, high / nyq_rate], window=('kaiser', beta)) # Use lfilter to filter x with the FIR filter. filtered_x = lfilter(taps, 1.0, x) #-------------------------------------------------------------------------
filterRate = 20000 nyquistRate = 10000 passBandWidth = 1000 transWidth = 250 stopBandEdge = 1250 centerFreq = 5000 lpfCut = 1125 pass_nyq = passBandWidth / nyquistRate tranWid_nyq = transWidth / nyquistRate stopEdge_nyq = stopBandEdge / nyquistRate lpfCut_nyq = lpfCut / nyquistRate ripple_dB = 55.0 numtaps, beta = sigs.kaiserord(ripple_dB, tranWid_nyq) taps = sigs.firwin(numtaps=numtaps, cutoff=lpfCut, width=transWidth, window=('kaiser', beta), nyquistRate=nyquistRate) # use filtfilt def lowpass(testSignal): b = sigs.firwin2(numtaps=255, freq=[0, .1, .125, 1.0], gain=[1, 1, 0, 0]) Fsig = sigs.lfilter(b, 1, testSignal) figure(1)
def main(): # == 1) Parameters ======================================================= pair = str(sys.argv[1]) freq_band_s = str(sys.argv[2]) freq_band_t = str(sys.argv[3]) sub_a = str(sys.argv[4]) sub_b = str(sys.argv[5]) delay = int(sys.argv[6]) freq_all = { 'delta': [1., 3.], 'theta': [4., 7.], 'alpha': [8., 12.], 'beta': [13., 29.], 'gamma': [30., 45.] } fsample = 2400. patches_to_remove = [ 'Basal Ganglia Left', 'Basal Ganglia Right', 'Limbic Left', 'Limbic Right', 'Cerebellum Left', 'Cerebellum Right', 'Cerebellum Mid' ] freq_bound_s = freq_all[freq_band_s] freq_bound_t = freq_all[freq_band_t] # == 2) Get data from mat files ========================================== temp = sio.loadmat('/home/horozco/2-STE_graham/' + pair + '/' + sub_a + '_trials_labels.mat') sub_a_trials_labels = [ temp['trials_labels'][n][0][0].encode("utf-8") for n in range(21) ] del temp temp = sio.loadmat('/home/horozco/2-STE_graham/' + pair + '/' + sub_a + '_source_labels.mat') sub_a_patch_labels = [ temp['source_labels'][n][0][0].encode("utf-8") for n in range(19) ] [sub_a_patch_labels.remove(n) for n in patches_to_remove] patch_labels = sub_a_patch_labels del temp temp = sio.loadmat('/home/horozco/2-STE_graham/' + pair + '/' + sub_a + '_sources.mat') sub_a_trials_data = [temp['sources'][n][0] for n in range(21)] del temp temp = sio.loadmat('/home/horozco/2-STE_graham/' + pair + '/' + sub_b + '_trials_labels.mat') sub_b_trials_labels = [ temp['trials_labels'][n][0][0].encode("utf-8") for n in range(21) ] del temp temp = sio.loadmat('/home/horozco/2-STE_graham/' + pair + '/' + sub_b + '_sources.mat') sub_b_trials_data = [temp['sources'][n][0] for n in range(21)] del temp # == 3) Time-frequency decomposition ===================================== # source frequency band # Get transition badnwdith using MNE's rule of thumb freq_l_trans_s = min(max(freq_bound_s[0] * 0.25, 2), freq_bound_s[0]) freq_h_trans_s = min(max(freq_bound_s[1] * 0.25, 2.), fsample / 2. - freq_bound_s[1]) # Compute the filter filter_nyq = fsample / 2.0 filter_s_width = np.mean(np.array([freq_l_trans_s, freq_h_trans_s])) filter_s_n, _ = signal.kaiserord(30.0, filter_s_width / filter_nyq) filter_s_taps = signal.firwin(filter_s_n, np.array(freq_bound_s), width=filter_s_width, window='blackman', pass_zero=False, fs=fsample) # sub a # Get rid of the deep brain structures sub_a_baseline_sfreq = sub_a_trials_data[ -1] # baseline is always the last file sub_a_baseline_sfreq = sub_a_baseline_sfreq[ [0, 1, 2, 3, 6, 7, 8, 9, 10, 11, 12, 13], :] # get triggers from raw file to allign both files temp_df = pd.read_csv( '/home/horozco/3-STE_baseline_graham/baseline_start.csv', header=None) event_df = temp_df[temp_df.iloc[:, 0] == pair] event_duration_a = event_df[event_df.iloc[:, 1] == sub_a].iloc[0, -1] sub_a_baseline_sfreq = sub_a_baseline_sfreq[:, event_duration_a:( event_duration_a + 432000)] # get 3 minutes of baseline # Apply filter (forward and backward using filtfilt) sub_a_base_s_filt = signal.filtfilt(filter_s_taps, 1, sub_a_baseline_sfreq, axis=1, padlen=None) # Get power from Hilbert Transform orig_n = sub_a_base_s_filt.shape[1] hilt_n = sp.fftpack.next_fast_len(orig_n) # Make HT go faster sub_a_base_s_env = signal.hilbert(sub_a_base_s_filt, axis=1, N=hilt_n) sub_a_base_s_env = sub_a_base_s_env[:, :orig_n] # Crop out the padding sub_a_base_s_env_power = np.abs(sub_a_base_s_env)**2 # Get rid of padding (3s at the begining and at the end) to take care of # HT and filtering edge artifacts padding = int(3 * fsample) sub_a_base_s_env_power = sub_a_base_s_env_power[:, padding:-padding] sub_a_base_s = signal.decimate(sub_a_base_s_env_power, 5, axis=1, zero_phase=True) # sub b # Get rid of the deep brain structures sub_b_baseline_sfreq = sub_b_trials_data[-1] sub_b_baseline_sfreq = sub_b_baseline_sfreq[ [0, 1, 2, 3, 6, 7, 8, 9, 10, 11, 12, 13], :] # get triggers from raw file to allign both files event_df = temp_df[temp_df.iloc[:, 0] == pair] event_duration_b = event_df[event_df.iloc[:, 1] == sub_b].iloc[0, -1] sub_b_baseline_sfreq = sub_b_baseline_sfreq[:, event_duration_b:( event_duration_b + 432000)] # get 3 minutes of baseline # Apply filter (forward and backward using filtfilt) sub_b_base_s_filt = signal.filtfilt(filter_s_taps, 1, sub_b_baseline_sfreq, axis=1, padlen=None) # Get power from Hilbert Transform orig_n = sub_b_base_s_filt.shape[1] # baseline is always the last file hilt_n = sp.fftpack.next_fast_len(orig_n) # Make HT go faster sub_b_base_s_env = signal.hilbert(sub_b_base_s_filt, axis=1, N=hilt_n) sub_b_base_s_env = sub_b_base_s_env[:, :orig_n] # Crop out the padding sub_b_base_s_env_power = np.abs(sub_b_base_s_env)**2 # Get rid of padding (3s at the begining and at the end) to take care of # HT and filtering edge artifacts padding = int(3 * fsample) sub_b_base_s_env_power = sub_b_base_s_env_power[:, padding:-padding] sub_b_base_s = signal.decimate(sub_b_base_s_env_power, 5, axis=1, zero_phase=True) if freq_band_s == freq_band_t: sub_a_base_t = sub_a_base_s sub_b_base_t = sub_b_base_s else: # source frequency band # Get transition bandwidth using MNE's rule of thumb freq_l_trans_t = min(max(freq_bound_t[0] * 0.25, 2), freq_bound_t[0]) freq_h_trans_t = min(max(freq_bound_t[1] * 0.25, 2.), fsample / 2. - freq_bound_t[1]) # Compute the filter filter_nyq = fsample / 2.0 filter_t_width = np.mean(np.array([freq_l_trans_t, freq_h_trans_t])) filter_t_n, _ = signal.kaiserord(30.0, filter_t_width / filter_nyq) filter_t_taps = signal.firwin(filter_t_n, np.array(freq_bound_t), width=filter_t_width, window='blackman', pass_zero=False, fs=fsample) # sub a # Get rid of the deep brain structures sub_a_baseline_tfreq = sub_a_trials_data[-1] sub_a_baseline_tfreq = sub_a_baseline_tfreq[ [0, 1, 2, 3, 6, 7, 8, 9, 10, 11, 12, 13], :] # trim the file around triggers sub_a_baseline_tfreq = sub_a_baseline_tfreq[:, event_duration_a:( event_duration_a + 432000)] # get 3 minutes of baseline # Apply filter (forward and backward using filtfilt) sub_a_base_t_filt = signal.filtfilt(filter_t_taps, 1, sub_a_baseline_tfreq, axis=1, padlen=None) # Get power from Hilbert Transform orig_n = sub_a_base_t_filt.shape[1] hilt_n = sp.fftpack.next_fast_len(orig_n) # Make HT go faster sub_a_base_t_env = signal.hilbert(sub_a_base_t_filt, axis=1, N=hilt_n) sub_a_base_t_env = sub_a_base_t_env[:, :orig_n] # Crop out the padding sub_a_base_t_env_power = np.abs(sub_a_base_t_env)**2 # Get rid of padding (3s at the begining and at the end) to take # care of HT and filtering edge artifacts padding = int(3 * fsample) sub_a_base_t_env_power = sub_a_base_t_env_power[:, padding:-padding] sub_a_base_t = signal.decimate(sub_a_base_t_env_power, 5, axis=1, zero_phase=True) # sub b # Get rid of the deep brain structures sub_b_baseline_tfreq = sub_b_trials_data[-1] sub_b_baseline_tfreq = sub_b_baseline_tfreq[ [0, 1, 2, 3, 6, 7, 8, 9, 10, 11, 12, 13], :] # trim the file around triggers sub_b_baseline_tfreq = sub_b_baseline_tfreq[:, event_duration_b:( event_duration_b + 432000)] # get 3 minutes of baseline # Apply filter (forward and backward using filtfilt) sub_b_base_t_filt = signal.filtfilt(filter_t_taps, 1, sub_b_baseline_tfreq, axis=1, padlen=None) # Get power from Hilbert Transform orig_n = sub_b_base_t_filt.shape[1] hilt_n = sp.fftpack.next_fast_len(orig_n) # Make HT go faster sub_b_base_t_env = signal.hilbert(sub_b_base_t_filt, axis=1, N=hilt_n) sub_b_base_t_env = sub_b_base_t_env[:, :orig_n] # Crop out the padding sub_b_base_t_env_power = np.abs(sub_b_base_t_env)**2 # Get rid of padding (3s at the begining and at the end) to take # care of HT and filtering edge artifacts padding = int(3 * fsample) sub_b_base_t_env_power = sub_b_base_t_env_power[:, padding:-padding] sub_b_base_t = signal.decimate(sub_b_base_t_env_power, 5, axis=1, zero_phase=True) # == 5) Symbolic Transfer Entropy ======================================== temp_columns = [ 'sub_source', 'sub_target', 'freq_source', 'freq_target', 'neuro_source', 'neuro_target', 'trial', 'pair', 'ste' ] temp_data = np.zeros( ((sub_a_base_s.shape[0] + sub_b_base_s.shape[0]) * (sub_a_base_t.shape[0] + sub_b_base_t.shape[0]), len(temp_columns))) temp_df = pd.DataFrame(data=temp_data, columns=temp_columns) temp_df['freq_source'] = freq_band_s temp_df['freq_target'] = freq_band_t temp_df['pair'] = pair temp_df['trial'] = 'baseline' if sub_a_base_s.shape[1] != sub_b_base_s.shape[1]: n_base = min(sub_a_base_s.shape[1], sub_b_base_s.shape[1]) sub_a_base_s = sub_a_base_s[:, :n_base] sub_b_base_s = sub_b_base_s[:, :n_base] sub_a_base_t = sub_a_base_t[:, :n_base] sub_b_base_t = sub_b_base_t[:, :n_base] for n_source in range(len(patch_labels)): print("--- Starting %s ---" % patch_labels[n_source]) temp_df.loc[(48 * n_source):(47 * (n_source + 1) + n_source), 'neuro_source'] = patch_labels[n_source] temp_df.loc[(48 * n_source):(47 * (n_source + 1) + n_source), 'neuro_target'] = (patch_labels * 4) # sub_a to sub_a temp_df.loc[(48 * n_source):(48 * n_source + 11), 'ste'] = [ symb_transfer_entropy(sub_a_base_s[n_source, :], sub_a_base_t[i, :], delay, 3) for i in range(sub_a_base_s.shape[0]) ] temp_df.loc[(48 * n_source):(48 * n_source + 11), 'sub_source'] = sub_a temp_df.loc[(48 * n_source):(48 * n_source + 11), 'sub_target'] = sub_a # sub_a to sub_b temp_df.loc[(48 * n_source + 12):(48 * n_source + 23), 'ste'] = [ symb_transfer_entropy(sub_a_base_s[n_source, :], sub_b_base_t[i, :], delay, 3) for i in range(sub_a_base_s.shape[0]) ] temp_df.loc[(48 * n_source + 12):(48 * n_source + 23), 'sub_source'] = sub_a temp_df.loc[(48 * n_source + 12):(48 * n_source + 23), 'sub_target'] = sub_b # sub_b to sub_a temp_df.loc[(48 * n_source + 24):(48 * n_source + 35), 'ste'] = [ symb_transfer_entropy(sub_b_base_s[n_source, :], sub_a_base_t[i, :], delay, 3) for i in range(sub_a_base_s.shape[0]) ] temp_df.loc[(48 * n_source + 24):(48 * n_source + 35), 'sub_source'] = sub_b temp_df.loc[(48 * n_source + 24):(48 * n_source + 35), 'sub_target'] = sub_a # sub_b to sub_b temp_df.loc[(48 * n_source + 36):(48 * n_source + 47), 'ste'] = [ symb_transfer_entropy(sub_b_base_s[n_source, :], sub_b_base_t[i, :], delay, 3) for i in range(sub_a_base_s.shape[0]) ] temp_df.loc[(48 * n_source + 36):(48 * n_source + 47), 'sub_source'] = sub_b temp_df.loc[(48 * n_source + 36):(48 * n_source + 47), 'sub_target'] = sub_b temp_df.to_csv('/home/horozco/3-STE_baseline_graham/baseline_grand_matrices/' + pair +\ '_' + freq_band_s + '_' + freq_band_t + '_baseline_' +\ str(delay) + '.csv')
def calcPAC(datafileDirectory, fileName, channelNum, endTime, startTime, lcut, hcut, samplingFrequency, ripple_db, bw, attenHz, originalFileName): outf = open("/www/projects/EEG_Portal/webEEGPortal/analysisLogmain.txt", "a") outf.write(" in calc pac \n") outf.write(" in calc pac endTime " + str(endTime) + " startTime " + str(startTime) + "\n") try: if originalFileName.find('edf') != -1: edfFile = pyedflib.EdfReader(datafileDirectory + fileName) try: numChannels = edfFile.signals_in_file channels = edfFile.getSignalLabels() samplingFrequency = edfFile.getSampleFrequencies()[0] fileDuration = edfFile.file_duration beginTime = 0 endTime = int(fileDuration) print(" 1 numSignals = " + str(fileDuration)) data = np.zeros((numSignals, edfFile.getNSamples()[0])) for i in np.arange(numSignals): data[i, :] = edfFile.readSignal(i) except: edfFile._close() del edfFile edfFile = pyedflib.EdfReader(datafileDirectory + fileName) numChannels = edfFile.signals_in_file channels = edfFile.getSignalLabels() samplingFrequency = edfFile.getSampleFrequencies()[0] fileDuration = edfFile.file_duration print(" 2 numSignals = " + str(fileDuration)) beginTime = 0 endTime = int(fileDuration) data = np.zeros((numSignals, edfFile.getNSamples()[0])) for i in np.arange(numSignals): data[i, :] = edfFile.readSignal(i) edfFile._close() del edfFile elif originalFileName.find('mat') != -1: eegmat = loadmat(datafileDirectory + fileName) channels = [str(x[0]) for x in eegmat["channelLabels"][0]] samplingFrequency = int(eegmat["eegFS"]) numChannels = len(channels) fileDuration = round(len(eegmat["eegData"][0]) / samplingFrequency) beginTime = 0 endTime = int(fileDuration) data = eegmat["eegData"] eeg = data[channelNum] eeg = eeg[startTime * samplingFrequency - 1:endTime * samplingFrequency - 1] eeg = eeg - np.mean(eeg, axis=0) edges = np.linspace(-math.pi, math.pi, 21) lcut = 9.0 hcut = 13.0 #---------------------------------------------------------- # Set the sampling frequency to 100. Sample period is 0.01. # A frequency of 1 will have 100 samples per cycle, and # therefore have 4 cycles in the 400 samples. sample_rate = 250.0 nyq = sample_rate / 2.0 width = 2.0 / nyq # pass to stop transition width # The desired attenuation in the stop band, in dB. ripple_db = 40.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) #print ('N = ',N, 'beta = kaiser param = ', beta) # The cutoff frequency of the filter. cutoff_hz = lcut # Use firwin with a Kaiser window to create a lowpass FIR filter. hpftaps = firwin(N, cutoff_hz / nyq, window=('kaiser', beta), pass_zero=False) #print (hpftaps[:10]) #---------------------------------------------------------- # now create the taps for a high pass filter. # by multiplying tap coefficients by -1 and # add 1 to the centre tap ( must be even order filter) hpftaps = [-1 * a for a in hpftaps] print(len(hpftaps)) midPoint = int(np.round(len(hpftaps) / 2)) if midPoint % 2 != 0: midPoint = midPoint - 1 hpftaps[midPoint] = hpftaps[midPoint] + 1 #---------------------------------------------------------- # Now calculate the tap weights for a lowpass filter at say 15hz cutoff_hz = hcut lpftaps = firwin(N, cutoff_hz / nyq, window=('kaiser', beta), pass_zero=False) # Subtract 1 from lpf centre tap for gain adjust for hpf + lpf lpftaps[midPoint] = lpftaps[midPoint] - 1 taps = [sum(pair) for pair in zip(hpftaps, lpftaps)] denom = [0] * len(taps) denom[0] = 1 #denom[-1] = 1 [a, f] = group_delay([taps, denom], int(nyq)) #print ( " num taps = " + str(len(taps))) #print ( " taps [:10] = " + str(taps[:10])) #print ( " a = " + str(a) ) #print ( " f = " + str(len(f)) ) bAlpha = taps bAlphax = np.array(a) * sample_rate / (2 * math.pi) #print ( " bAlpha **** = " + str(bAlpha) ) k = [f[i] for i, m in enumerate(bAlphax) if m >= 9 and m <= 13] #print ( " k = " + str(k) ) gdAlpha = math.floor(np.mean(k)) #print ( "gdAlpha = " + str(gdAlpha ) ) fGamma = np.arange(20, 101, 5) #print ( " fGamma = " + str(fGamma) ) bw = 20 #bandwidth attendB = ripple_db #attenuation attenHz = 4 #transition band filtersGamma = {} for fI in range(len(fGamma)): lcut = fGamma[fI] - bw / 2 hcut = fGamma[fI] + bw / 2 Fstop1 = (lcut - attenHz) / nyq Fpass1 = lcut / nyq Fpass2 = hcut / nyq Fstop2 = (hcut + attenHz) / nyq Astop1 = attendB Apass = 1 Astop2 = attendB ################# hpftaps = firwin(N, lcut / nyq, window=('kaiser', beta)) hpftaps = [-1 * a for a in hpftaps] midPoint = int(np.round(len(hpftaps) / 2)) if midPoint % 2 != 0: midPoint = midPoint - 1 hpftaps[midPoint] = hpftaps[midPoint] + 1 lpftaps = firwin(N, hcut / nyq, window=('kaiser', beta)) lpftaps[midPoint] = lpftaps[midPoint] - 1 taps = [sum(pair) for pair in zip(hpftaps, lpftaps)] denom = [0] * len(taps) denom[0] = 1 [a, f] = group_delay([taps, denom], int(nyq)) x = np.array(a) * sample_rate / (2 * math.pi) k = [f[i] for i, m in enumerate(x) if m >= 9 and m <= 13] val = math.floor(np.mean(k)) ################# filtersGamma[fI] = [] filtersGamma[fI].append(taps) filtersGamma[fI].append(val) MIs = [] # phase (alpha) #print ( " bAlpha = " + str(bAlpha[:20] ) ) #print ( " eeg = " + str(eeg[:20] ) ) ph = lfilter(bAlpha, 1, eeg) #print ( " ph before angle = " + str(ph[:10] ) ) ph = np.append(ph[gdAlpha + 1:], np.zeros(gdAlpha)) ph = np.angle(hilbert(ph)) #print ( " ph after angle = " + str(ph[:10] ) ) ##amplitude for gamma range + MI for key, value in filtersGamma.items(): #amplitude amp = lfilter(value[0], 1, eeg) gd = value[1] amp = np.append(amp[gd + 1:], np.zeros(gd)) amp = abs(hilbert(amp)) #compute raw modulation index #print (" amp = " + str(amp[:20]) ) #print (" ph = " + str(ph[:20]) ) #print (" edges = " + str(edges) ) MI = getMI(amp, ph, edges) MIs.append(MI) #print (" MI = " + str(MI)) #break #print ( " MIs = " + str(MIs)) outf.write(" before save in calc pac \n") np.savetxt( "/home/siddhartha/self/andre/outplots/outpac_fGamma_" + str(channelNum) + ".txt", fGamma) np.savetxt( "/home/siddhartha/self/andre/outplots/outpac_MIs_" + str(channelNum) + ".txt", MIs) except Exception as e: traceback.print_exc(file=sys.stdout) outf.write(str(e)) return
def calcPAC(filePath, channelNum, startTime, stopTime, lcut, hcut, sample_rate, ripple_db, bw, attenHz): # ''' # Set the sampling frequency to 100. Sample period is 0.01. # A frequency of 1 will have 100 samples per cycle, and # therefore have 4 cycles in the 400 samples. # The desired attenuation in the stop band, in dB. # ripple_db = 40.0 # # bw = 20 #bandwidth # attendB = ripple_db #attenuation # # attenHz = 4 #transition band # ''' try: #eegData = loadmat('/content/drive/My Drive/andre/EEG181.mat') eegData = loadmat(filePath) eegFS = 250 # sampling frequency eegData = eegData["eegData"] eeg1 = eegData[channelNum] print ( eeg1[:5]) eeg1 = eeg1[startTime * eegFS - 1 : stopTime * eegFS - 1 ] eeg1 = eeg1 - np.mean(eeg1, axis = 0) eeg2 = eegData[15] print ( eeg2[:5]) eeg2 = eeg2[startTime * eegFS - 1 : stopTime * eegFS - 1 ] eeg2 = eeg2 - np.mean(eeg2, axis = 0) # edges for phase edges = np.linspace(-math.pi,math.pi,21) x = np.linspace(0,200,5000); s1 = np.sin( x * pi / 180 ) s2 = np.sin( x * pi / 18 ) s2 = np.array ( [x*y for x,y in zip(s1,s2) ] ) s3 = s1 + s2 eeg = s3 #edges = np.array(list(edges).append(math.pi)) # lcut = 9.0 # hcut = 13.0 nyq = sample_rate / 2.0 width = 2.0/nyq # pass to stop transition width # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) #print ('N = ',N, 'beta = kaiser param = ', beta) # The cutoff frequency of the filter. cutoff_hz = lcut winLen = N # to be in conformance with MATLAB ( check documentation of fir1 in MATLAB, it automatically adds 1 for even sized windows) if N % 2 ==0: winLen = N + 1 # Use firwin with a Kaiser window to create a lowpass FIR filter. hpftaps = firwin(winLen, cutoff_hz/nyq, window=('kaiser', beta), pass_zero=False) #print (hpftaps[:10]) #---------------------------------------------------------- # now create the taps for a high pass filter. # by multiplying tap coefficients by -1 and # add 1 to the centre tap ( must be even order filter) hpftaps = [-1*a for a in hpftaps] print ( len(hpftaps)) midPoint = int(np.round(len(hpftaps)/2)) if midPoint % 2 != 0: midPoint = midPoint -1 hpftaps[midPoint] = hpftaps[midPoint] + 1 #---------------------------------------------------------- # Now calculate the tap weights for a lowpass filter at say 15hz cutoff_hz = hcut lpftaps = firwin(winLen, cutoff_hz/nyq, window=('kaiser', beta), pass_zero=False) # Subtract 1 from lpf centre tap for gain adjust for hpf + lpf lpftaps[midPoint] = lpftaps[midPoint] - 1 taps = [sum(pair) for pair in zip(hpftaps, lpftaps)] denom = [0]*len(taps) denom[0] = 1 #denom[-1] = 1 [a,f] = group_delay( [ taps , denom ] , int(nyq)) #print ( " num taps = " + str(len(taps))) #print ( " taps [:10] = " + str(taps[:10])) #print ( " a = " + str(a) ) #print ( " f = " + str(len(f)) ) bAlpha = taps bAlphax = np.array(a) * sample_rate/(2*math.pi) #print ( " bAlpha **** = " + str(bAlpha) ) k = [f[i] for i,m in enumerate(bAlphax) if m >= 9 and m <= 13] #print ( " k = " + str(k) ) gdAlpha = math.floor(np.mean(k)) #print ( "gdAlpha = " + str(gdAlpha ) ) fGamma = np.arange(20,101,5) #print ( " fGamma = " + str(fGamma) ) # bw = 20 #bandwidth attendB = ripple_db #attenuation # attenHz = 4 #transition band filtersGamma = {} for fI in range ( len(fGamma) ): lcut = fGamma[fI]-bw/2 hcut = fGamma[fI]+bw/2 Fstop1 = (lcut - attenHz) / nyq Fpass1 = lcut / nyq Fpass2 = hcut / nyq Fstop2 = (hcut + attenHz) / nyq Astop1 = attendB Apass = 1 Astop2 = attendB ################# hpftaps = firwin(N, lcut/nyq, window=('kaiser', beta)) hpftaps = [-1*a for a in hpftaps] midPoint = int(np.round(len(hpftaps)/2)) if midPoint % 2 != 0: midPoint = midPoint -1 hpftaps[midPoint] = hpftaps[midPoint] + 1 lpftaps = firwin(N, hcut/nyq, window=('kaiser', beta)) lpftaps[midPoint] = lpftaps[midPoint] - 1 taps = [sum(pair) for pair in zip(hpftaps, lpftaps)] denom = [0]*len(taps) denom[0] = 1 [a,f] = group_delay( [ taps , denom ] , int(nyq)) x = np.array(a) * sample_rate/(2*math.pi) k = [f[i] for i,m in enumerate(x) if m >= 9 and m <= 13] val = math.floor(np.mean(k)) ################# filtersGamma[fI] = [] filtersGamma[fI].append (taps) filtersGamma[fI].append (val) PLVs = [] # phase (alpha) #print ( " bAlpha = " + str(bAlpha[:20] ) ) #print ( " eeg = " + str(eeg[:20] ) ) #ph = lfilter(bAlpha,1,eeg) ##print ( " ph before angle = " + str(ph[:10] ) ) #ph = np.append(ph[gdAlpha+1:], np.zeros(gdAlpha)) #ph = np.angle(hilbert(ph)) #print ( " ph after angle = " + str(ph[:10] ) ) ##amplitude for gamma range + MI for key, value in filtersGamma.items(): #amplitude amp1 = lfilter(value[0],1,eeg1) amp2 = lfilter(value[0],1,eeg2) gd = value[1] amp1 = np.append(amp1[gd+1:], np.zeros(gd)) amp2 = np.append(amp2[gd+1:], np.zeros(gd)) ph1 = np.angle(hilbert(amp1)) ph2 = np.angle(hilbert(amp2)) phd = ph1 - ph2 #compute raw modulation index #print (" amp = " + str(amp[:20]) ) #print (" ph = " + str(ph[:20]) ) #print (" edges = " + str(edges) ) PLVs.append((1/len(phd))*(abs(sum(np.exp(1j*phd))))) #break print ( " MIs = " + str(PLVs)) plt.plot(fGamma,PLVs) plt.show() #plt.savefig("PAC_channel_") except: traceback.print_exc(file=sys.stdout) return
def design_low_pass(cls, sample_rate, cutoff_hz, width_hz, ripple_db): nyq_rate = sample_rate / 2.0 N, beta = signal.kaiserord(ripple_db, width_hz / nyq_rate) taps = signal.firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta)) unweighted_taps = list(taps) return taps
def calcPLV(filePath, channelNum, startTime, stopTime, lcut, hcut, sample_rate, ripple_db, bw, attenHz): try: eegData = loadmat('/content/drive/My Drive/andre/EEG181.mat') eegFS = 250 # sampling frequency eegData = eegData["eegData"] eeg = eegData[14] #print ( eeg[:5]) eeg = eeg[50 * eegFS - 1 : 80 * eegFS - 1 ] eeg = eeg - np.mean(eeg, axis = 0) x = np.linspace(0,200,5000); s1 = np.sin( x * pi / 180 ) s2 = np.sin( x * pi / 18 ) s2 = np.array ( [x*y for x,y in zip(s1,s2) ] ) s3 = s1 + s2 eeg = s3 # edges for phase edges = np.linspace(-math.pi,math.pi,21) #edges = np.array(list(edges).append(math.pi)) lcut = 9.0 hcut = 13.0 #---------------------------------------------------------- # Set the sampling frequency to 100. Sample period is 0.01. # A frequency of 1 will have 100 samples per cycle, and # therefore have 4 cycles in the 400 samples. sample_rate = 250.0 nyq = sample_rate / 2.0 width = 2.0/nyq # pass to stop transition width # The desired attenuation in the stop band, in dB. ripple_db = 40.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) #print ('N = ',N, 'beta = kaiser param = ', beta) # The cutoff frequency of the filter. cutoff_hz = lcut # Use firwin with a Kaiser window to create a lowpass FIR filter. hpftaps = firwin(N, cutoff_hz/nyq, window=('kaiser', beta), pass_zero=False) #print (hpftaps[:10]) #---------------------------------------------------------- # now create the taps for a high pass filter. # by multiplying tap coefficients by -1 and # add 1 to the centre tap ( must be even order filter) hpftaps = [-1*a for a in hpftaps] print ( len(hpftaps)) midPoint = int(np.round(len(hpftaps)/2)) if midPoint % 2 != 0: midPoint = midPoint -1 hpftaps[midPoint] = hpftaps[midPoint] + 1 #---------------------------------------------------------- # Now calculate the tap weights for a lowpass filter at say 15hz cutoff_hz = hcut lpftaps = firwin(N, cutoff_hz/nyq, window=('kaiser', beta), pass_zero=False) # Subtract 1 from lpf centre tap for gain adjust for hpf + lpf lpftaps[midPoint] = lpftaps[midPoint] - 1 taps = [sum(pair) for pair in zip(hpftaps, lpftaps)] denom = [0]*len(taps) denom[0] = 1 #denom[-1] = 1 [a,f] = group_delay( [ taps , denom ] , int(nyq)) #print ( " num taps = " + str(len(taps))) #print ( " taps [:10] = " + str(taps[:10])) #print ( " a = " + str(a) ) #print ( " f = " + str(len(f)) ) bAlpha = taps bAlphax = np.array(a) * sample_rate/(2*math.pi) #print ( " bAlpha **** = " + str(bAlpha) ) k = [f[i] for i,m in enumerate(bAlphax) if m >= 9 and m <= 13] #print ( " k = " + str(k) ) gdAlpha = math.floor(np.mean(k)) #print ( "gdAlpha = " + str(gdAlpha ) ) fGamma = np.arange(20,101,5) #print ( " fGamma = " + str(fGamma) ) bw = 20 #bandwidth attendB = ripple_db #attenuation attenHz = 4 #transition band filtersGamma = {} for fI in range ( len(fGamma) ): lcut = fGamma[fI]-bw/2 hcut = fGamma[fI]+bw/2 Fstop1 = (lcut - attenHz) / nyq Fpass1 = lcut / nyq Fpass2 = hcut / nyq Fstop2 = (hcut + attenHz) / nyq Astop1 = attendB Apass = 1 Astop2 = attendB ################# hpftaps = firwin(N, lcut/nyq, window=('kaiser', beta)) hpftaps = [-1*a for a in hpftaps] midPoint = int(np.round(len(hpftaps)/2)) if midPoint % 2 != 0: midPoint = midPoint -1 hpftaps[midPoint] = hpftaps[midPoint] + 1 lpftaps = firwin(N, hcut/nyq, window=('kaiser', beta)) lpftaps[midPoint] = lpftaps[midPoint] - 1 taps = [sum(pair) for pair in zip(hpftaps, lpftaps)] denom = [0]*len(taps) denom[0] = 1 [a,f] = group_delay( [ taps , denom ] , int(nyq)) x = np.array(a) * sample_rate/(2*math.pi) k = [f[i] for i,m in enumerate(x) if m >= 9 and m <= 13] val = math.floor(np.mean(k)) ################# filtersGamma[fI] = [] filtersGamma[fI].append (taps) filtersGamma[fI].append (val) MIs = [] # phase (alpha) #print ( " bAlpha = " + str(bAlpha[:20] ) ) #print ( " eeg = " + str(eeg[:20] ) ) ph = lfilter(bAlpha,1,eeg) #print ( " ph before angle = " + str(ph[:10] ) ) ph = np.append(ph[gdAlpha+1:], np.zeros(gdAlpha)) ph = np.angle(hilbert(ph)) #print ( " ph after angle = " + str(ph[:10] ) ) ##amplitude for gamma range + MI for key, value in filtersGamma.items(): #amplitude amp = lfilter(value[0],1,eeg) gd = value[1] amp = np.append(amp[gd+1:], np.zeros(gd)) amp = abs(hilbert(amp)) #compute raw modulation index #print (" amp = " + str(amp[:20]) ) #print (" ph = " + str(ph[:20]) ) #print (" edges = " + str(edges) ) MI = getMI(amp,ph,edges) MIs.append(MI) #print (" MI = " + str(MI)) #break #print ( " MIs = " + str(MIs)) plt.plot(fGamma,MIs) plt.show() except: traceback.print_exc(file=sys.stdout) return def tridisolve(d, e, b, overwrite_b=True): """Symmetric tridiagonal system solver, from Golub and Van Loan pg 157. Note: Copied from NiTime Parameters ---------- d : ndarray main diagonal stored in d[:] e : ndarray superdiagonal stored in e[:-1] b : ndarray RHS vector Returns ------- x : ndarray Solution to Ax = b (if overwrite_b is False). Otherwise solution is stored in previous RHS vector b """ N = len(b) # work vectors dw = d.copy() ew = e.copy() if overwrite_b: x = b else: x = b.copy() for k in range(1, N): # e^(k-1) = e(k-1) / d(k-1) # d(k) = d(k) - e^(k-1)e(k-1) / d(k-1) t = ew[k - 1] ew[k - 1] = t / dw[k - 1] dw[k] = dw[k] - t * ew[k - 1] for k in range(1, N): x[k] = x[k] - ew[k - 1] * x[k - 1] x[N - 1] = x[N - 1] / dw[N - 1] for k in range(N - 2, -1, -1): x[k] = x[k] / dw[k] - ew[k] * x[k + 1] if not overwrite_b: return x
def main(): data, samplerate = sf.read( "10convert.com_High-School-Musical-1-We-re-All-in-This-Together-Lyrics-1080pHD_iFu8Z-cV0Xk (online-audio-converter.com).wav" ) sinal = signalMeu() f = 44100 audio = data[0:10 * samplerate] dados = [] for e in audio[:, 0]: dados.append(e) t = np.linspace(0, 10, len(dados)) # 5a plt.plot(t, dados) plt.title("Audio original x Tempo") plt.show() sinal.plotFFT(dados, f) plt.show() # 5b dados = np.asarray(dados) normal = dados / max(dados) plt.plot(t, normal) plt.title("Audio normalizado x Tempo") plt.show() sinal.plotFFT(normal, f) plt.show() # 5c # exemplo de filtragem do sinal yAudioNormalizado # https://scipy.github.io/old-wiki/pages/Cookbook/FIRFilter.html nyq_rate = f / 2 width = 5.0 / nyq_rate ripple_db = 60.0 #dB N, beta = signal.kaiserord(ripple_db, width) cutoff_hz = 4000.0 taps = signal.firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta)) yFiltrado = signal.lfilter(taps, 1.0, normal) plt.plot(t, yFiltrado) plt.title("Audio passa baixa x Tempo") plt.show() sinal.plotFFT(yFiltrado, f) plt.show() # 5d t, s = sinal.generateSin(14000, 1, 10, f) modulado = yFiltrado * s plt.plot(t, modulado) plt.title("Audio modulado x Tempo") plt.show() sinal.plotFFT(modulado, f) plt.show() sf.write("save.wav", modulado, f)
def _design_firwin_filter( ftype=None, cutoff_hz=None, sfreq=None, width_hz=None, ripple_db=None, window=None ): """calculate odd length, symmetric, linear phase FIR filter coefficients FIRLS at https://scipy-cookbook.readthedocs.io/items/FIRFilter.html Parameters ---------- ftype : string filter type, one of 'lowpass' , 'highpass', 'bandpass', 'bandstop' cutoff_hz : float or 1D array_like cutoff frequency in Hz, e.g., 5.0, 30.0 for lowpass or highpass. 1D array_like, e.g. [10.0, 30.0] for bandpass or bandstop sfreq : float sampling frequency, e.g., 250.0, 500.0 width_hz : float transition band width start to stop in Hz ripple_db : float attenuation in the stop band, in dB, e.g., 24.0, 60.0 Returns ------- taps : np.array coefficients of FIR filter. """ for kwarg in [ftype, cutoff_hz, sfreq, width_hz, ripple_db, window]: assert kwarg is not None check_filter_params( ftype=ftype, cutoff_hz=cutoff_hz, sfreq=sfreq, width_hz=width_hz, ripple_db=ripple_db, window=window, ) # Nyquist frequency nyq_rate = sfreq / 2.0 # transition band width in normalizied frequency width = width_hz / nyq_rate # order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) if N % 2 == 0: N = N + 1 # enforce odd number of taps # create a FIR filter using firwin if ftype.lower() == "lowpass": if window.lower() == "kaiser": taps = firwin( N, cutoff_hz, window=("kaiser", beta), pass_zero="lowpass", fs=sfreq, ) else: taps = firwin(N, cutoff_hz, window=window, pass_zero="lowpass", fs=sfreq) elif ftype.lower() == "highpass": if window.lower() == "kaiser": taps = firwin( N, cutoff_hz, window=("kaiser", beta), pass_zero="highpass", fs=sfreq, ) else: taps = firwin(N, cutoff_hz, window=window, pass_zero="highpass", fs=sfreq) elif ftype.lower() == "bandpass": if window.lower() == "kaiser": taps = firwin( N, cutoff_hz, window=("kaiser", beta), pass_zero="bandpass", fs=sfreq, ) else: taps = firwin(N, cutoff_hz, window=window, pass_zero="bandpass", fs=sfreq) elif ftype.lower() == "bandstop": if window.lower() == "kaiser": taps = firwin( N, cutoff_hz, window=("kaiser", beta), pass_zero="bandstop", fs=sfreq, ) else: taps = firwin(N, cutoff_hz, window=window, pass_zero="bandstop", fs=sfreq) return taps
def HIGHPASS(x): hpf_data = open("hpf.txt", "w") # The Nyquist rate of the signal. sample_rate = FILTER_RATE nyq_rate = sample_rate / 2.0 width = 250.0 / nyq_rate # The desired attenuation in the stop band, in dB. ripple_db = 60.0 # Compute the order and Kaiser parameter for the FIR filter. N, beta = kaiserord(ripple_db, width) # The cutoff frequency of the filter. bpCut1 = 3875 / nyq_rate bpCut2 = 6125 / nyq_rate # Use firwin with a Kaiser window to create a lowpass FIR filter. taps = firwin(N, [bpCut1, bpCut2], width=width, window=('kaiser', beta), pass_zero=False) # ------------------------------------------------ # Apply filter to signal # ------------------------------------------------ filtered_x = lfilter(taps, 1.0, x) # ------------------------------------------------ # Plot the FIR filter coefficients. # ------------------------------------------------ plt.figure() plt.plot(taps, 'bo-', linewidth=2) plt.title('Filter Coefficients (%d taps)' % N) plt.grid(True) plt.savefig( 'hpf_coeff.png', dpi=600, # Dots per inch frameon='false', aspect='normal', bbox_inches='tight', pad_inches=0) plt.close() hpf_data.write("Coeffs: \n") hpf_data.write(np.array2string(taps, 5, 5, True, ',')) hpf_data.write("\n\n") # ------------------------------------------------ # Plot the magnitude response of the filter. # ------------------------------------------------ plt.figure() plt.clf() w, h = freqz(taps, worN=8000) plt.plot((w / scipy.pi) * nyq_rate, scipy.absolute(h), linewidth=2) plt.xlabel('Frequency (Hz)') plt.ylabel('Gain') plt.title('Frequency Response') plt.ylim(-0.05, 1.05) plt.grid(True) # Upper inset plot. plt.ax1 = plt.axes([0.22, 0.55, .35, .25]) plt.title("Passband Ripple") plt.plot((w / scipy.pi) * nyq_rate, scipy.absolute(h), linewidth=2) plt.xlim(8950, 9200) plt.ylim(0.995, 1.0025) plt.grid(True) # Lower inset plot plt.ax2 = plt.axes([0.22, 0.2, .35, .25]) plt.title("Stopband Ripple") plt.plot((w / scipy.pi) * nyq_rate, scipy.absolute(h), linewidth=2) plt.xlim(8600.0, 8900.0) plt.ylim(0.0, 0.011) plt.grid(True) plt.savefig( 'hpf_Fresponse.png', dpi=600, # Dots per inch frameon='false', aspect='normal', bbox_inches='tight', pad_inches=0) plt.close() hpf_data.write("normalized freq: \n") hpf_data.write( np.array2string(w, precision=5, suppress_small=True, separator=',')) hpf_data.write("\n\n") hpf_data.write("complex freq: \n") hpf_data.write( np.array2string(h, precision=5, suppress_small=True, separator=',')) hpf_data.write("\n\n") # ------------------------------------------------ # Plot the original and filtered signals. # ------------------------------------------------ # create time array for time plot timearray = scipy.arange(0, numSamp, 1) timearray = timearray / sampRate t = timearray * 1000 # scale to milliseconds # The phase delay of the filtered signal. delay = 0.5 * (N - 1) / sample_rate plt.figure() # Plot the original signal. plt.plot(t, x) # Plot the filtered signal, shifted to compensate for the phase delay. plt.plot(t - delay, filtered_x, 'r-') # Plot just the "good" part of the filtered signal. The first N-1 # samples are "corrupted" by the initial conditions. plt.plot(t[N - 1:] - delay, filtered_x[N - 1:], 'g', linewidth=4) plt.xlabel('t') plt.grid(True) plt.savefig( 'hpf_filtSig.png', dpi=600, # Dots per inch frameon='false', aspect='normal', bbox_inches='tight', pad_inches=0) plt.close() graph_spectrogram(filtered_x, sampRate, 'hpf') hpf_data.close() return
exit(0) if sys.argv[1] == "fir": import numpy as np from scipy.signal import kaiserord, firwin, freqz import math fs = float(sys.argv[2]) passband_freq = float(sys.argv[3]) / fs stopband_freq = float(sys.argv[4]) / fs stopband_atten = float(sys.argv[5]) width = abs(passband_freq - stopband_freq) / 0.5 (tap_count, beta) = kaiserord(ripple=stopband_atten, width=width) #if len(sys.argv) > 6: tap_count = int( sys.argv[6] ) taps = firwin( numtaps = tap_count, \ cutoff = ((passband_freq+stopband_freq)/2)/0.5, \ window = ('kaiser', beta) ) #nn = 25 #taps = [] #for ii in range(0,nn): taps.append(1.0/nn) import matplotlib.pyplot as plt w, h = freqz(taps, worN=8000) fig = plt.figure() plt.title('Digital filter frequency response')
def __init__(self, signal: np.array, sample_rate=100, nsamples=100, ripple_db=60.0, cutoff_hz=2.0, with_rate=1.0, win='kaiser', higpas=False): """ Fir filter: Inputs: signal: is the data in an np.array. sample_rate: What rate was the signal reqorded at? ripple_db: The desierd attenuation in the stopbond, in dB. cutoff_hz: The cutoff frequency of the filter. """ if higpas == True: print("--Init highpass --") else: print("--Init lowpass --") assert (len(signal) > 1) # self._signal = signal self._signal = np.pad(signal, (3, 6), 'constant') print(np.shape(self._signal)) self._sample_rate = sample_rate self._cutoff_hz = cutoff_hz self._ripple_db = ripple_db self._window_type = win self._nsamples = len(signal) self._t = np.arange(len(signal)) / sample_rate self._nyq_rate = sample_rate / 2 self._width = with_rate / self._nyq_rate if cutoff_hz / self._nyq_rate % 2 == 0: while self._cutoff_hz / self._nyq_rate % 2 == 0: print(self._cutoff_hz) self._cutoff_hz -= 0.001 else: print("not even") print(self._cutoff_hz / self._nyq_rate) # Compute the Kaizer parameter for the FIR filter. self._N = 0 count = 0 print("ever loop") while self._N % 2 == 0: print("N = {} is even".format(self._N)) count += 0.000001 self._N, self._beta = kaiserord(self._ripple_db, self._width + count) print("N = {} is od with with={}".format(self._N, self._width + count)) self._ishigpass = higpas #Spectral inversion if higpass is true if self._ishigpass == True: print("Higpass filter init") print(self._N) self._bands = (0, self._cutoff_hz, self._cutoff_hz + 0.5, self._cutoff_hz + 1.0, self._cutoff_hz + 2, self._nyq_rate) self._desierd = (0, 0, 1, 1, 0, 0) assert (len(self._bands) == len(self._desierd)) self._taps = firls(self._N, self._bands, self._desierd, nyq=self._nyq_rate) # self._taps = firwin(self._N ,self._cutoff_hz/self._nyq_rate , pass_zero=False) # self._taps = -self._taps # self._taps[self._N//2] += 1 else: self._taps = firwin(self._N, cutoff_hz / self._nyq_rate, window=(win, self._beta)) self._freqz = freqz(self._taps, worN=8000) self._h_dB = 20 * np.log(np.abs(self._freqz[1]) + 0.00001) self._filterd_x = lfilter(self._taps, 1.0, signal) print("filterd median ={}".format(np.median(self._filterd_x))) delay = 0.5 * (self._N - 1) / self._sample_rate self._good_t = self._t[self._N - 1:] - delay, self._good_signal = self._filterd_x[self._N - 1:] self._supblot_limmits = list() m, s = self.get_statistics() # Step responce for the lit system. self._responce = False
""" FIR filter design example; requires SciPy 0.9 or later. """ import numpy as np from scipy.signal import firwin2, freqz, kaiserord from matplotlib.pylab import plot, show, grid, xlabel, ylabel, legend, title # Bandpass with a notch. desired_freq = [0.0, 0.1, 0.2, 0.56, 0.6, 0.64, 0.8, 0.9, 1.0] desired_gain = [0.0, 0.0, 1.0, 1.0, 0.75, 1.0, 1.0, 0.0, 0.0] # Use firwin2 with a Kaiser window to design the filter. ntaps, beta = kaiserord(ripple=65, width=0.08) taps = firwin2(ntaps, desired_freq, desired_gain, window=('kaiser', beta)) # Compute the filter's response. w, h = freqz(taps, [1.0], worN=8000) # Plot the desired and actual responses. plot(desired_freq, desired_gain, 'g-', label='Desired') plot(w/np.pi, np.abs(h), 'b', label='Computed') xlabel('Frequency (rel. to Nyquist)') ylabel('Gain') title('FIR Filter Frequency Response (%d taps)' % ntaps) legend() grid(True) show()
def step(self, node): N, beta = signal.kaiserord(node.getIn('R'), node.getIn('W')) node.setOut('N', N) node.setOut('B', beta)
def FIR_filter(sample_rate = 1 / (float(CONFIG['Track Generation']['Timestep']) * 10**(-9)), ripple_db = 20.0, cutoff_hz = 50000): nyq_rate = sample_rate / 2.0 width = 10000.0/nyq_rate N, beta = kaiserord(ripple_db, width) taps = firwin(N, cutoff_hz/nyq_rate, window=('kaiser', beta)) return taps
from matplotlib.backends.backend_tkagg import * window = Tk() window.title("filtersweep") window.protocol("WM_DELETE_WINDOW", window.quit) sfreq = 48000 nfreq = sfreq // 2 fsamples = nfreq // 10 def avg_coeffs(n): return np.array([1 / n] * n) nopt, bopt = signal.kaiserord(-20, 10 / nfreq) def kaiser_coeffs(n, beta=0.5): return signal.firwin(n, 0.01, window=('kaiser', beta), scale=True) # https://plotly.com/python/v3/fft-filters/ def win_sinc_coeffs(n, beta=0.5): xs = np.arange(n) sinc = np.sinc(2 * 0.01 * (xs - (n - 1)) / 2) window = signal.windows.kaiser(n, beta) sw = sinc * window return sw / np.sum(sw)
ts = data_all[['time']].values linacce = data_all[['linacce_x', 'linacce_y', 'linacce_z']].values orientations = data_all[['rv_w', 'rv_x', 'rv_y', 'rv_z']].values positions = data_all[['pos_x', 'pos_y', 'pos_z']].values positions[:, 2] = 0 gravity = data_all[['grav_x', 'grav_y', 'grav_z']].values steps = np.genfromtxt(args.path + '/step.txt') labels = np.ones(ts.shape[0], dtype=np.int) * class_map[args.placement] # Pro-processing sample_rate = 200 nyq_rate = sample_rate / 2.0 transition_width = 5.0 / nyq_rate ripple_db = 60.0 order, beta = kaiserord(ripple_db, transition_width) cutoff_hz = 5 taps = firwin(order, cutoff_hz / nyq_rate, window=('kaiser', beta)) threshold = 1.0 linacce_filtered = lfilter(taps, 1.0, linacce, axis=0) acce_grav = geometry.align_3dvector_with_gravity(linacce_filtered, gravity) print('Detecting steps') step_locs, chn = detect_steps(linacce_filtered, gravity, labels, threshold) print('Step from system: %d, estimated: %d' % (steps[-1, 1], step_locs.shape[0])) # Compose the detected steps est_steps = np.concatenate( [ts[step_locs],
from scipy.signal import kaiserord, firwin, freqz import matplotlib.pyplot as plt fs = 1000.0 cutoff = 175 width = 24 # The Kaiser method accepts just a single parameter to control the pass # band ripple and the stop band rejection, so we use the more restrictive # of the two. In this case, the pass band ripple is 0.005, or 46.02 dB, # so we will use 65 dB as the design parameter. # Use `kaiserord` to determine the length of the filter and the # parameter for the Kaiser window. numtaps, beta = kaiserord(65, width / (0.5 * fs)) numtaps # 167 beta # 6.20426 # Use `firwin` to create the FIR filter. taps = firwin(numtaps, cutoff, window=('kaiser', beta), scale=False, nyq=0.5 * fs) # Compute the frequency response of the filter. ``w`` is the array of # frequencies, and ``h`` is the corresponding complex array of frequency
def spectral_filt(self, t, x, cutoff_freq=1., stop_band=1., atten_db=60.): """Low-pass filter a time series. Design and use a low-pass FIR filter using functions from scipy.signal. It applies a forward-backward filter (for zero phase-shift) with replication of the signal at the ends (to avoid `losses` in the filtered signal). From: http://www.scipy.org/Cookbook/FIRFilter Parameters ---------- t : 1-D array The time/space domain of the series. x : 1-D array The signal. cutoff_freq : float The frequency limit for low-pass filtering. stop_band : float The `width` (in freq) of the stop band (around `cutoff_freq`). atten_db : float (in dB) The `slope` of the transition (from 1 to 0) in the `stop_band`. Output ------ filtered_x : 1-D array The filtered inputed signal. """ from numpy import cos, sin, pi, absolute, arange from scipy.signal import kaiserord, lfilter, firwin self.cutoff_freq = cutoff_freq self.stop_band = stop_band self.atten_db = atten_db # sampling rate (freq) self.sample_freq = 1. / (t[1] - t[0]) print 'sample freq: %.3f samples per unit time' % self.sample_freq # The Nyquist rate of the signal self.nyq_freq = self.sample_freq / 2.0 print 'Nyquist freq: %.3f' % self.nyq_freq # The desired width of the transition from pass to stop, # relative to the Nyquist rate. width = self.stop_band / self.nyq_freq print 'stop band width %.3f' % self.stop_band # The desired attenuation in the stop band, in dB (between 20-120). self.atten_db = atten_db print 'stop band attenuation %.3f dB' % self.atten_db # Compute the order and Kaiser parameter for the FIR filter. self.N, self.beta = kaiserord(atten_db, width) # The cutoff frequency of the filter. self.cutoff_freq = cutoff_freq print 'cutoff freq: %.3f' % self.cutoff_freq # Use firwin with a Kaiser window to create a lowpass FIR filter. self.b = firwin(self.N, self.cutoff_freq / self.nyq_freq, window=('kaiser', self.beta)) self.a = np.array([1.0]) # filter x with a `zero phase-shift` FIR filter. filtered_x = sg.filtfilt(self.b, self.a, x) return filtered_x
sample_rate = 100000.0 nyquist_rate = sample_rate/2.0 width = 10000.0/nyquist_rate cutoff = 2000.0/nyquist_rate ripple_db = 60.0 # Bandpass filter high_pass = (tx_freq+100.0)/nyquist_rate low_pass = (tx_freq-100.0)/nyquist_rate high_cutoff = (tx_freq+300.0)/nyquist_rate low_cutoff = (tx_freq-300.0)/nyquist_rate ord, wn = signal.buttord([low_pass, high_pass],[low_cutoff,high_cutoff],1.0,30.0) b,a = signal.butter(ord,wn, btype="band") ## Low pass filter N, beta = signal.kaiserord(ripple_db, width) taps = signal.firwin(N, cutoff/nyquist_rate, window=('kaiser', beta)) parser = argparse.ArgumentParser(description="Echidna client") parser.add_argument('--server', '-s', type=str, default = "localhost", help="select the server to connect to") parser.add_argument('--port','-p',type=int, default=5555, help="select the port to connect to") parser.add_argument('--file','-f',type=argparse.FileType('a'),default=sys.stdout, help="file to write to") parser.add_argument('--time','-t',type=int,default=10, help="experiment duration") parser.add_argument('--radar','-r',action="store_true",default=False,
def test_kaiserord(): assert_raises(ValueError, kaiserord, 1.0, 1.0) numtaps, beta = kaiserord(2.285 + 7.95 - 0.001, 1 / np.pi) assert_equal((numtaps, beta), (2, 0.0))