本文整理汇总了Python中scipy.signal.butter函数的典型用法代码示例。如果您正苦于以下问题:Python butter函数的具体用法?Python butter怎么用?Python butter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了butter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: makeFiltered
def makeFiltered(self):
filtered = np.zeros(len(self.data))
b1, a1 = butter(1, 0.0003, 'lowpass')
filtered = filtfilt(b1, a1, self.data)
filtered = self.data - filtered
b2, a2 = butter(1, 0.025, 'lowpass')
self.filt_data = filtfilt(b2, a2, filtered)
示例2: filter_feepmt
def filter_feepmt(feep):
"""
input: an instance of class FeePmt
output: buttersworth parameters of the equivalent FEE filter
"""
# high pass butterswoth filter ~1/RC
b1, a1 = signal.butter(1, feep.freq_LHPFd, 'high', analog=False)
b2, a2 = signal.butter(1, feep.freq_LHPFd, 'low', analog=False)
b0 = b2*(feep.Zin/(1+feep.C1/feep.C2))+b1*(feep.Zin*feep.R1/(feep.Zin+feep.R1))
a0 = a1
# LPF order 1
b1l, a1l = signal.butter(1, feep.freq_LPF1d, 'low', analog=False)
# LPF order 4
b2l, a2l = signal.butter(4, feep.freq_LPF2d, 'low', analog=False)
# convolve HPF, LPF1
a_aux = np.convolve(a0, a1l, mode='full')
b_aux = np.convolve(b0, b1l, mode='full')
# convolve HPF+LPF1, LPF2
a = np.convolve(a_aux, a2l, mode='full')
b_aux2 = np.convolve(b_aux, b2l, mode='full')
b = feep.A2*b_aux2
return b, a
示例3: __init__
def __init__(self,window_size, low, high):
fs_Hz = 250;
fn = fs_Hz/2
self.filtered_data = np.array((window_size,1))
#######################################
# Filter Creation
# -------------------------------------
#
# Create a filter using the scipy module,
# based on specifications suggested by
# Pan-Tompkins (bandpass from 5-15Hz)
#
#
# 1) Establish constants:
# a) filter_order = 2
# b) high pass cutoff = 15Hz
# c) low pass cutoff = 5Hz
# 2) Calculate the coefficients, store in variables
filter_order = 2
f_high = high
f_low = low
self.high_pass_coefficients = signal.butter(filter_order,f_low/fn, 'high')
self.low_pass_coefficients = signal.butter(filter_order,f_high/fn, 'low')
示例4: __init__
def __init__(self,source, nchannels, order, fc, btype='low'):
Wn = np.asarray(np.atleast_1d(fc)).copy() #Scalar inputs are converted to 1-dimensional arrays
self.samplerate = source.samplerate
Wn= Wn/float(self.samplerate)*2 # wn=1 corresponding to half the sample rate
if btype=='low' or btype=='high':
self.filt_b=np.zeros((nchannels,order+1))
self.filt_a=np.zeros((nchannels,order+1))
if len(Wn)==1: #if there is only one Wn value for all channel just repeat it
self.filt_b, self.filt_a = signal.butter(order, Wn, btype=btype)
self.filt_b=np.kron(np.ones((nchannels,1)),self.filt_b)
self.filt_a=np.kron(np.ones((nchannels,1)),self.filt_a)
else: #else make nchannels different filters
for i in range((nchannels)):
self.filt_b[i,:], self.filt_a[i,:] = signal.butter(order, Wn[i], btype=btype)
else:
self.filt_b=np.zeros((nchannels,2*order+1))
self.filt_a=np.zeros((nchannels,2*order+1))
if Wn.ndim==1: #if there is only one Wn pair of values for all channel just repeat it
self.filt_b, self.filt_a = signal.butter(order, Wn, btype=btype)
self.filt_b=np.kron(np.ones((nchannels,1)),self.filt_b)
self.filt_a=np.kron(np.ones((nchannels,1)),self.filt_a)
else:
for i in range((nchannels)):
self.filt_b[i,:], self.filt_a[i,:] = signal.butter(order, Wn[:,i], btype=btype)
self.filt_a=self.filt_a.reshape(self.filt_a.shape[0],self.filt_a.shape[1],1)
self.filt_b=self.filt_b.reshape(self.filt_b.shape[0],self.filt_b.shape[1],1)
self.nchannels = nchannels
LinearFilterbank.__init__(self,source, self.filt_b, self.filt_a)
示例5: __init__
def __init__(self, source, nchannels, order, fc, btype="low"):
Wn = fc.copy()
Wn = atleast_1d(Wn) # Scalar inputs are converted to 1-dimensional arrays
self.samplerate = source.samplerate
try:
Wn = Wn / self.samplerate * 2 + 0.0 # wn=1 corresponding to half the sample rate
except DimensionMismatchError:
raise DimensionMismatchError("Wn must be in Hz")
if btype == "low" or btype == "high":
self.filt_b = zeros((nchannels, order + 1))
self.filt_a = zeros((nchannels, order + 1))
if len(Wn) == 1: # if there is only one Wn value for all channel just repeat it
self.filt_b, self.filt_a = signal.butter(order, Wn, btype=btype)
self.filt_b = kron(ones((nchannels, 1)), self.filt_b)
self.filt_a = kron(ones((nchannels, 1)), self.filt_a)
else: # else make nchannels different filters
for i in xrange((nchannels)):
self.filt_b[i, :], self.filt_a[i, :] = signal.butter(order, Wn[i], btype=btype)
else:
self.filt_b = zeros((nchannels, 2 * order + 1))
self.filt_a = zeros((nchannels, 2 * order + 1))
if Wn.ndim == 1: # if there is only one Wn pair of values for all channel just repeat it
self.filt_b, self.filt_a = signal.butter(order, Wn, btype=btype)
self.filt_b = kron(ones((nchannels, 1)), self.filt_b)
self.filt_a = kron(ones((nchannels, 1)), self.filt_a)
else:
for i in xrange((nchannels)):
self.filt_b[i, :], self.filt_a[i, :] = signal.butter(order, Wn[:, i], btype=btype)
self.filt_a = self.filt_a.reshape(self.filt_a.shape[0], self.filt_a.shape[1], 1)
self.filt_b = self.filt_b.reshape(self.filt_b.shape[0], self.filt_b.shape[1], 1)
self.nchannels = nchannels
LinearFilterbank.__init__(self, source, self.filt_b, self.filt_a)
示例6: butter_high_low_pass
def butter_high_low_pass(lowcut, highcut, sampling_rate, order=5):
nyq_freq = sampling_rate*0.5
lower_bound = lowcut/nyq_freq
higher_bound = highcut/nyq_freq
b_high, a_high = butter(order, lower_bound, btype='high')
b_low, a_low = butter(order, higher_bound, btype='low')
return b_high, a_high, b_low, a_low
示例7: filter_data
def filter_data(self,eeg_data):
#FILTER CONSTANTS
fs = self.fs
fn = self.fn
filter_order = 2 #2nd order filter
f_high = 50
f_low = 5
wn = [59,61] #Nyquist filter window
[b,a] = signal.butter(filter_order,f_high/fn, 'low')
[b1,a1] = signal.butter(filter_order,f_low/fn, 'high')
[bn,an] = signal.butter(4,[x/fn for x in wn], 'stop')
filtered_eeg = []
spectogram = []
notched = []
high_passed = []
low_passed = []
print(eeg_data)
for i in range(len(eeg_data[0])):
channel = eeg_data[:,i]
high_passed = signal.filtfilt(b1,a1,channel); # high pass filter
low_passed = signal.filtfilt(b,a,high_passed); # low pass filter
y = signal.filtfilt(bn,an,low_passed); # notch filter
filtered_eeg.append(y);
self.filtered_eeg = filtered_eeg
return filtered_eeg
# if __name__ == '__main__':
# main()
示例8: filter
def filter(self):
path = os.getcwd()+'/trialGraspEventDetection_dataFiles'
self.Fgr = np.sum(self.values[:, 9:15], axis=1) # SAI
self.Fgl = np.sum(self.values[:, 0:7], axis=1) # SAI
# can use this to plot in matlab graspeventdetection_plot.m
np.savetxt(path+'/SAI_Fgr.txt', self.Fgr)
# can use this to plot in matlab
np.savetxt(path+'/SAI_Fgl.txt', self.Fgl)
# 0.55*pi rad/samples
b1, a1 = signal.butter(1, 0.55, 'high', analog=False)
self.f_acc_x = signal.lfilter(b1, a1, self.acc_x, axis=-1, zi=None)
self.f_acc_y = signal.lfilter(b1, a1, self.acc_y, axis=-1, zi=None)
self.f_acc_z = signal.lfilter(b1, a1, self.acc_z, axis=-1, zi=None)
# self.f_eff = signal.lfilter(b1, a1, self.eff, axis=-1, zi=None)
# type(eff)
self.FAII = np.sqrt(np.square(self.f_acc_x) +
np.square(self.f_acc_y) +
np.square(self.f_acc_z))
# can use this to plot in matlab
np.savetxt(path+'/FAII.txt', self.FAII)
# subtract base values from the values array
self.values1 = self.values - self.values.min(axis=0)
# pass the filter for each sensor
self.fvalues1 = np.zeros(self.values1.shape)
# 0.48*pi rad/samples
b, a = signal.butter(1, 0.48, 'high', analog=False)
for i in range(16):
self.fvalues1[:, i] = signal.lfilter(b, a, self.values1[:, i],
axis=-1, zi=None)
self.FAI = np.sum(self.fvalues1, axis=1)
# can use this to plot in matlab
np.savetxt(path+'/FAI.txt', self.FAI)
示例9: butter_bandpass
def butter_bandpass(fs, lowcut=None, highcut=None, order=10):
"""
Parameters
----------
fs : float
Sample Rate
lowcut : float
Low pass frequency cutoff
highcut : float
High pass frequency cutoff
order : int
Butterworth filter order [Default = 10, i.e., 10th order Butterworth filter]
Reference
---------
http://scipy-cookbook.readthedocs.io/items/ButterworthBandpass.html
"""
from scipy.signal import butter
nyq = 0.5 * fs
if lowcut and highcut:
high = highcut / nyq
low = lowcut / nyq
sos = butter(order, [low, high], analog=False, btype='band', output='sos')
elif highcut:
high = highcut / nyq
sos = butter(order, high, btype='low', output='sos')
elif lowcut:
low = lowcut / nyq
sos = butter(order, low, btype='high', output='sos')
else:
print('Error -- must supply lowcut, highcut or both')
sos = None
return sos
示例10: temporally_filter
def temporally_filter(spatial, tfiltN, flash):
print("Temporally Filtering")
padlen=0
if tfiltN[2]==1: #%if only high pass temporal filter
[b,a]= butter(tfiltN[0],tfiltN[1],btype='highpass')
padlen=3
else: #%if band pass temporal filter
[b,a]=butter(tfiltN[0],[tfiltN[1],tfiltN[2]], btype='bandpass')
padlen=6
temporal=np.array(spatial, dtype = np.float32)
if len(flash) == 2:
temporal[flash[0]:flash[1]+1,:,:]=np.mean(spatial[4:flash[0]+1, :, :], 0)
bg=np.mean(temporal[:flash[0] + 1, :, :],0)
else:
bg = np.min(temporal, 0)
for t in np.arange(len(temporal)):
temporal[t, :, :] -= bg
#temporal[0, :, :] = 0
p = 0.0
for y in range(np.size(temporal, 1)):
for x in range(np.size(temporal, 2)):
temporal[:, y, x]=filtfilt(b,a, temporal[:, y, x], padlen=padlen)
if (100 * y / np.size(temporal, 1)) > p:
p = (100 * y / np.size(temporal, 1))
print(" %d%%\r" % (100 * y / np.size(temporal, 1))),
print('Temporally Filtered')
return temporal
示例11: __init__
def __init__(self, channels, sample_rate, leds=None):
self.leds = leds # Not needed if just plotting
self.channels = channels
self.sample_rate = sample_rate
self.nyquist = float(sample_rate) / 2
# Filter order - higher the order the sharper
# the curve
order = 3
# Cut off frequencies:
# Low pass filter
cutoff = 200 / self.nyquist
# Numerator (b) and denominator (a)
# polynomials of the filter.
b, a = butter(order, cutoff, btype='lowpass')
self.low_b = b
self.low_a = a
# High pass filter
cutoff = 4000 / self.nyquist
b, a = butter(order, cutoff, btype='highpass')
self.high_b = b
self.high_a = a
# Keep track of max brightness for each
# colour
self.max = [0.0, 0.0, 0.0]
# Make different frequencies fall faster
# bass needs to be punchy.
self.fall = [15.0, 2.5, 5.0]
示例12: prepare_audio_filters
def prepare_audio_filters():
tf_rangel = 100000
tf_rangeh = 170000
# audio filters
tf = SysParams["audio_lfreq"]
N, Wn = sps.buttord(
[(tf - tf_rangel) / (freq_hz / 2.0), (tf + tf_rangel) / (freq_hz / 2.0)],
[(tf - tf_rangeh) / (freq_hz / 2.0), (tf + tf_rangeh) / (freq_hz / 2.0)],
5,
15,
)
Faudl = filtfft(sps.butter(N, Wn, btype="bandpass"))
tf = SysParams["audio_rfreq"]
N, Wn = sps.buttord(
[(tf - tf_rangel) / (freq_hz / 2.0), (tf + tf_rangel) / (freq_hz / 2.0)],
[(tf - tf_rangeh) / (freq_hz / 2.0), (tf + tf_rangeh) / (freq_hz / 2.0)],
5,
15,
)
Faudr = filtfft(sps.butter(N, Wn, btype="bandpass"))
N, Wn = sps.buttord(0.016 / (afreq / 2.0), 0.024 / (afreq / 2.0), 5, 15)
FiltAPost = filtfft(sps.butter(N, Wn))
N, Wn = sps.buttord(3.1 / (freq / 2.0), 3.5 / (freq / 2.0), 1, 20)
SysParams["fft_audiorf_lpf"] = Faudrf = filtfft(sps.butter(N, Wn, btype="lowpass"))
SysParams["fft_audiolpf"] = FiltAPost # * FiltAPost * FiltAPost
SysParams["fft_audio_left"] = Faudrf * Faudl * fft_hilbert
SysParams["fft_audio_right"] = Faudrf * Faudr * fft_hilbert
示例13: butter_bandpass
def butter_bandpass(lowcut, highcut, samplingrate, order=4):
nyq = 0.5 * samplingrate
low = lowcut / nyq
high = highcut / nyq
print high, low
if high >=1. and low == 0.:
b = np.array([1.])
a = np.array([1.])
elif high < 0.95 and low > 0. :
wp = [1.05*low,high-0.05]
ws = [0.95*low,high+0.05]
print wp,ws
order,wn = buttord(wp,ws,0., 30.)
b, a = butter(order, wn, btype='band')
elif high>= 0.95:
print 'highpass',low,1.2*low,0.8*low
order,wn = buttord( 15*low,0.05*low,gpass=0.0, gstop=10.0)
print order,wn
b, a = butter(order, wn, btype='high')
elif low <= 0.05:
print 'lowpass',high
order,wn = buttord( high-0.05,high+0.05,gpass=0.0, gstop=10.0)
b, a = butter(order, wn, btype='low')
return b, a
示例14: __init__
def __init__(self, fs, filter_type):
self.filter_type = filter_type # Of two types. Alpha and Bandpass
self.fs = fs # sample frequency: 220hz
if self.filter_type == 'alpha':
# butter = butterworth filter function
# An optimal (in one way -- smoothness) for designing a filter. Simple and popular.
# signal.butter is from scipy. Is there an equivalent in Java? Could be developed in Python and copied
# and pasted into Java. The values of these arrays is really only dependent on sampling frequency and thus
# likely to be the same for all uses of the app
# Returns two arrays of floats. Could b
self.b, self.a = signal.butter(5# order of filter. More coefficients = better filtration at cost of speed
, np.array([8.,12.])# boundaries of filter (8-12hz for Alpha
/(self.fs/2.) # /2 because this function needs normalized frequency
, 'bandpass')
elif self.filter_type == 'bandpass':
self.b, self.a = signal.butter(5, np.array([2., 36.] # bandpass filter has different freq cutoffs
)/(self.fs/2.), 'bandpass')
else:
print('Filter type ''%s'' not supported.'%self.filter_type)
return
self.nb = len(self.b)
self.na = len(self.a)
示例15: generate_noise
def generate_noise(D,N):
"""Generate data for the changepoint detection. Data can either be of type 0
or type 1, but when it's a combination fo both, we define a target label
Input
- D,N Dimenstionality arguments D dimensions over N samples
Output
- Data in format
X is a matrix in R^{N x D}
y is a matrix in R^{N,} not to donfuse with {N,1}"""
#Check if we have even D, so we can split the array in future
assert D%2 == 0, 'We need even number of dimensions'
ratioP = 0.5 #balance of targets
X = np.random.randn(N,D)
y = np.zeros(N)
mark = np.zeros(N)
#Generate two filter cofficients
filters = {}
filters['b1'],filters['a1'] = signal.butter(4,2.0*cutoff1/fs,btype='lowpass')
filters['b0'],filters['a0'] = signal.butter(4,2.0*cutoff0/fs,btype='lowpass')
for i in xrange(N):
if np.random.rand() > 0.5: #Half of the samples will have changepoint, other half wont
Dcut = np.random.randint(pattern_len,D-pattern_len)
signalA = signal.filtfilt(filters['b1'],filters['a1'],X[i])
signalB = signal.filtfilt(filters['b0'],filters['a0'],X[i])
X[i] = np.concatenate((signalA[:Dcut],signalB[Dcut:]),axis=0) #Concatenate the two signals
if True: #Boolean: do you want to introduce a pattern at the changepoint?
Dstart = int(Dcut - pattern_len/2)
X[i,Dstart:Dstart+pattern_len] = pattern
y[i] = 1 #The target label
mark[i] = Dcut
else:
mode = int(np.random.rand()>ratioP)
X[i] = signal.filtfilt(filters['b'+str(mode)],filters['a'+str(mode)],X[i])
y[i] = 0 #The target label
return X,y,mark