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()
Exemple #2
0
    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]
    def filterSignal(self, cutoff, order, type):
        """ Filters the current signal with a specified filter.

        :param cutoff:
            The cut off frequency of the filter.
        :type cutoff:
            float
        :param order:
            The order of the filter to use.
        :type order:
            int
        :param type:
            The type of filter to use, either "low" or "high", for low pass
            filter, or high pass filter.
        :type type:
            str
        """
        self.logger.debug("Entering filterSignal (%s, %s, %s)" % (cutoff, order, type))

        # Get signal parameters
        sample_rate = float(self.parameters["sample rate"])

        if type == "high":
            [b, a] = butter(order, cutoff / (sample_rate / 2), btype=type)
            self.signal = lfilter(b, a, self.signal)
        #    b = firwin(251, cutoff=[50, 200], nyq=(sample_rate / 2))
        #    a = 1
        #    self.signal = lfilter(b, a, self.signal)

        #    [b, a] = iirdesign(wp=cutoff / (sample_rate / 2), ws = 50 / (sample_rate / 2), gpass=1, gstop=12, ftype="butter")
        else:

            [b, a] = butter(order, cutoff / (sample_rate / 2), btype=type)

            self.signal = lfilter(b, a, self.signal)
Exemple #4
0
    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)
Exemple #5
0
def decode_efm():
    """ Decode EFM from STDIN, assuming it's a 28Mhz 8bit raw stream  """
    datao = np.fromstring(sys.stdin.read(SAMPLES), dtype=np.uint8).astype(np.int16)
    datao = sps.detrend(datao, type='constant')  # Remove DC

    datao = auto_gain(datao, 10000, 'pre-filter')  # Expand before filtering, since we'll lose much of signal otherwise

    low_pass = sps.butter(4, 1.75 / FREQ_MHZ, btype='lowpass')  # Low pass at 1.75 Mhz
    datao = sps.lfilter(low_pass[0], low_pass[1], datao)

    high_pass = sps.butter(4, 0.01333 / FREQ_MHZ, btype='highpass')  # High pass at 13.333 khz
    datao = sps.lfilter(high_pass[0], high_pass[1], datao)

    # This is too slow, need to work out a way to do it in scipy
    de_emphasis_filter = biquad_filter(-1.8617006585639506, 0.8706642683920058, 0.947680874725466, -1.8659578411373265, 0.9187262110931641)
    datao = np.fromiter(run_filter(de_emphasis_filter, datao), np.int16)  # De-emph - 26db below 500khz

    # Could tie edge_pll and run_filter together as generators, but we want to see the filter output

    bit_gen = edge_pll(datao, EFM_PIXEL_RATE)  # This is a ultra-naive PLL that returns a bit-stream of 1 = edge, 0 = no-edge
    try:
        while 1:
            run_until_start_code(bit_gen)
            eat_three_bits(bit_gen)
            process_efm_frame(bit_gen, 31)  # 31 14 bit EFM codes in a frame
    except StopIteration:
        printerr('Hit the end of the bitstream')

    datao = np.clip(datao, 0, 255).astype(np.uint8)
    sys.stdout.write(datao.tostring())
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   
Exemple #7
0
 def __init__(self, fn, channels, to_freq, montage_channels, montage, idx=1, csp_time=[0, 0.3], a_time=0.5):
     self.data = sp.signalParser(fn)
     self.tags = self.data.get_p300_tags(idx=idx, samples=False)
     signal = self.data.prep_signal(to_freq, channels, montage_channels, montage)
     signal2 = np.zeros(signal.shape)
     self.fs = to_freq
     signal2 = np.zeros(signal.shape)
     self.wrong_tags = self.data.get_p300_tags(idx=idx, rest=True, samples=False)
     artifacts_data = np.zeros([len(channels), self.fs * a_time, len(self.tags)])
     for i, p in enumerate(self.tags):
         artifacts_data[..., i] = signal[
             :, p * self.data.sampling_frequency : (p + a_time) * self.data.sampling_frequency
         ]
     self.a_features, self.bands = artifactsCalibration(artifacts_data, self.data.sampling_frequency)
     signal2 = np.zeros(signal.shape)
     self.fs = to_freq
     self.channels = channels
     self.idx = idx
     b, a = ss.butter(3, 2 * 1.0 / self.fs, btype="high")
     b_l, a_l = ss.butter(3, 20.0 * 2 / self.fs, btype="low")
     for e in xrange(len(self.channels)):
         tmp = filtfilt(b, a, signal[e, :])
         signal2[e, :] = filtfilt(b_l, a_l, tmp)
     self.signal_original = signal2
     self.t1, self.t2 = self.show_mean(csp_time, "Cz", dont_plot=False)
     P, vals = self.train_csp(signal2, [self.t1, self.t2])
     self.P = P
     self.signal = np.dot(P[:, 0], signal2)
Exemple #8
0
def test_fir():
    print "Testing dsp-fir"

    #ref = ones(512)
    ref = (2.0 * random.rand(512)) - 1.0

    #test short mono fir
    writeaudio(ref)
    h = signal.firwin(21, 0.4)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    writeaudio(expected, 'expected.wav')
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(expected, readaudio(), 1e-6)

    #test long mono fir
    writeaudio(ref)
    h = signal.firwin(312, 0.4)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(expected, readaudio(), 1e-6)

    #test short stereo fir, mono coeffs
    writeaudio(transpose([ref,-ref]))
    h = signal.firwin(21, 0.4)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(transpose([expected, -expected]), readaudio(), 1e-6)

    #test long stereo fir, mono coeffs
    writeaudio(transpose([ref,-ref]))
    h = signal.firwin(312, 0.4)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(transpose([expected, -expected]), readaudio(), 1e-6)

    #test asymmetric mono fir
    writeaudio(ref)
    impulse = concatenate(([1], zeros(499)))
    b, a = signal.butter(2, 500.0/24000, 'low')
    h = signal.lfilter(b, a, impulse)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(expected, readaudio(), 1e-6)

    #test asymmetric stereo fir
    writeaudio(transpose([ref,-ref]))
    impulse = concatenate(([1], zeros(499)))
    b, a = signal.butter(2, 500.0/24000, 'low')
    h = signal.lfilter(b, a, impulse)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(transpose([expected, -expected]), readaudio(), 1e-6)

    os.remove('test_coeffs.txt')
	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')
Exemple #10
0
 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)
Exemple #11
0
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
Exemple #12
0
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
Exemple #13
0
    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)
 def bandfilter(data, fa=None, fb=None, Fs=1000.0, order=4, zerophase=True, bandstop=False):
     N = len(data)
     assert len(shape(data)) == 1
     padded = zeros(2 * N, dtype=data.dtype)
     padded[N / 2 : N / 2 + N] = data
     padded[: N / 2] = data[N / 2 : 0 : -1]
     padded[N / 2 + N :] = data[-1 : N / 2 - 1 : -1]
     if not fa == None and not fb == None:
         if bandstop:
             b, a = butter(order, array([fa, fb]) / (0.5 * Fs), btype="bandstop")
         else:
             b, a = butter(order, array([fa, fb]) / (0.5 * Fs), btype="bandpass")
     elif not fa == None:
         # high pass
         b, a = butter(order, fa / (0.5 * Fs), btype="high")
         assert not bandstop
     elif not fb == None:
         # low pass
         b, a = butter(order, fb / (0.5 * Fs), btype="low")
         assert not bandstop
     else:
         assert 0
     if zerophase:
         return filtfilt(b, a, padded)[N / 2 : N / 2 + N]
     else:
         return lfilter(b, a, padded)[N / 2 : N / 2 + N]
     assert 0
Exemple #15
0
    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)
Exemple #16
0
 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
Exemple #17
0
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
def filter_data(eeg_data, fs):
    #FILTER CONSTANTS
  fn = fs/2
  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 = []


  channel =  eeg_data
  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

  return y
Exemple #19
0
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
Exemple #20
0
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
    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) 
Exemple #22
0
def bandpass_filter(series, low=200.0, high=4000.0):
    """
    Filters given series with a 2nd order bandpass filter with default
    cutoff frequencies of 200 Hz and 4 kHz.

    Parameters
    ----------
    series : pandas.Series
        The data series to filter

    Returns
    -------
    filtered_series : pandas.Series
    """
    dt = series.index[1] - series.index[0]
    fs_nyquist = (1.0/dt) / 2.0
    if low < 0.1:
        # Lowpass filter only.
        bf, af = signal.butter(2, high/fs_nyquist, btype='lowpass')
    elif high > 10000:
        # Highpass filter only.
        bf, af = signal.butter(2, low/fs_nyquist, btype='highpass')
    else:
        bf, af = signal.butter(2, (low/fs_nyquist, high/fs_nyquist),
                               btype='bandpass')
    return pd.Series(signal.filtfilt(bf, af, series).astype(np.float32),
                     index=series.index)
def butterworth_plot(fig=None, ax=None):
    """
    Plot of frequency response of the Butterworth filter with different orders.
    """

    if fig is None:
        fig, ax = plt.subplots(1, 2, figsize=(10, 4))
        
    b1, a1 = signal.butter(1, 10, 'low', analog=True)
    w, h1 = signal.freqs(b1, a1)
    ang1 = np.rad2deg(np.unwrap(np.angle(h1)))
    h1 = 20 * np.log10(abs(h1))
    b2, a2 = signal.butter(2, 10, 'low', analog=True)
    w, h2 = signal.freqs(b2, a2)
    ang2 = np.rad2deg(np.unwrap(np.angle(h2)))
    h2 = 20 * np.log10(abs(h2))
    b4, a4 = signal.butter(4, 10, 'low', analog=True)
    w, h4 = signal.freqs(b4, a4)
    ang4 = np.rad2deg(np.unwrap(np.angle(h4)))
    h4 = 20 * np.log10(abs(h4))
    b6, a6 = signal.butter(6, 10, 'low', analog=True)
    w, h6 = signal.freqs(b6, a6)
    ang6 = np.rad2deg(np.unwrap(np.angle(h6)))
    h6 = 20 * np.log10(abs(h6))
    w = w/10

    # PLOT
    ax[0].plot(w, h1, 'b', w, h2, 'r', w, h4, 'g', w, h6, 'y', linewidth=2)
    ax[0].axvline(1, color='black') # cutoff frequency
    ax[0].scatter(1, -3, marker='s', edgecolor='0', facecolor='1', s=400)
    #ax1.legend(('1', '2', '4', '6'), title='Filter order', loc='best')
    ax[0].set_xscale('log')
    fig.suptitle('Bode plot for low-pass Butterworth filter with different orders',
                 fontsize=16, y=1.05)
    #ax1.set_title('Magnitude', fontsize=14)
    ax[0].set_xlabel('Frequency / Critical frequency', fontsize=14)
    ax[0].set_ylabel('Magnitude [dB]', fontsize=14)
    ax[0].set_xlim(0.1, 10)
    ax[0].set_ylim(-120, 10)
    ax[0].grid(which='both', axis='both')
    ax[1].plot(w, ang1, 'b', w, ang2, 'r', w, ang4, 'g', w, ang6, 'y', linewidth=2)
    ax[1].axvline(1, color='black')  # cutoff frequency
    ax[1].legend(('1', '2', '4', '6'), title='Filter order', loc='best')
    ax[1].set_xscale('log')
    #ax2.set_title('Phase', fontsize=14)
    ax[1].set_xlabel('Frequency / Critical frequency', fontsize=14)
    ax[1].set_ylabel('Phase [degrees]', fontsize=14)
    ax[1].set_yticks(np.arange(0, -300, -45))
    ax[1].set_ylim(-300, 10)
    ax[1].grid(which='both', axis='both')
    plt.tight_layout(w_pad=1)
    axi = plt.axes([.115, .4, .15, .35])  # inset plot
    axi.plot(w, h1, 'b', w, h2, 'r', w, h4, 'g', w, h6, 'y', linewidth=2)
    #ax11.set_yticks(np.arange(0, -7, -3))
    axi.set_xticks((0.6, 1, 1.4))
    axi.set_yticks((-6, -3, 0))
    axi.set_ylim([-7, 1])
    axi.set_xlim([.5, 1.5])
    axi.grid(which='both', axis='both')
Exemple #24
0
    def filterBpass(self, fs, cutOffLow, cutOffHigh, bLineRemoved):
        #fs / 2 je nyq frekvencia
        B, A = butter(2, cutOffHigh / (fs / 2), btype='low') # 1st order Butterworth low-pass
        C, D = butter(2, cutOffLow / (fs / 2), btype='high') # 1st order Butterworth high-pass

        bLineRemoved_low = lfilter(B, A, bLineRemoved)
        bLineRemoved_low_high = lfilter(C, D, bLineRemoved_low)
        return bLineRemoved_low_high
def butter_bandpass(lowcut, highcut, fs, order=8,kword='lowpass'):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    if kword=='lower':
        b, a = butter(order, high, btype='lowpass')
    else:
        b, a = butter(order, [low,high], btype='band')
    return b, a
 def _design(self):
     if self.already_normalized_Wn:
         self.Z, self.P, self.K = signal.butter(self.N, self.Wn,
                                                self.filter_kind, analog=False,
                                                output='zpk')
     else:
         self.Z, self.P, self.K = signal.butter(self.N, self.normalize_Wn(),
                                                self.filter_kind, analog=False,
                                                output='zpk')
Exemple #27
0
def filtracja(kan,fs, czest):
	n=1
	[bx,ax]=ss.butter(n, czest/(fs/2.),btype='highpass')
	y1a = ss.filtfilt(bx,ax,kan)
	
	[b,a]=ss.butter(n, [48./(fs/2.), 52./(fs/2.)],btype='bandstop')
	y1 = ss.filtfilt(b,a,y1a)
 
	return y1
    def __init__(self):
        print "Instantiating..."

        self._joint_vec = np.zeros(6)
        self._rate = rospy.Rate(10)
        self._pc = None
        self._pc_prev = np.array([0, 0, 0])
        self._R_capsule_world = None
        self._capsule_lin_vel = None
        self._capsule_ang_vel = None
        self._pa = None
        self._pa_prev = np.array([0, 0, 0])
        self._R_EPM_world = None
        self._fm = np.zeros(3)
        self._fm_prev = np.zeros(3)
        self._tm = np.zeros(3)
        self._tm_prev = np.zeros(3)
        self._coupling_status_msg = Float64()
        self._df_dx_msg = Vector3()
        self._df_dx_raw_msg = Vector3()
        self._dcapz_dEPMz_msg = Vector3()
        self._dcapz_dEPMz_raw_msg = Vector3()
        self._mag_wrench_msg = Wrench()

        # print "To publish: " , self._to_publish
        self._dipole = DipoleField(1.48, 1.48, SC.ACTUATOR_MAG_H, SC.CAPSULE_MAG_H)
        # print "Mag info:", SC.ACTUATOR_MAG_H

        # Initial conditions for Butterworth fulter
        fs = 100.0
        nyq = 0.5 * fs
        cutoff = 0.5
        order = 2
        self._butter_zi_dfdx = np.zeros((3, order))
        self._butter_params_dfdx = butter(order, cutoff / nyq, btype="lowpass", analog=False)
        self._butter_zi_dz = np.zeros((3, order))
        self._butter_params_dz = butter(order, cutoff / nyq, btype="lowpass", analog=False)

        # Instantiate publishers and subscribers
        self._coupling_status_pub = rospy.Publisher("/MAC/coupling_status_topic", Float64, queue_size=1000)
        self._df_dx_pub = rospy.Publisher("/MAC/df_dx_topic", Vector3, queue_size=1000)
        self._df_dx_raw_pub = rospy.Publisher("/MAC/df_dx_raw_topic", Vector3, queue_size=1000)
        self._mag_wrench_pub = rospy.Publisher("/MAC/mag_wrench_topic", Wrench, queue_size=1000)
        self._dcapz_dEPMz_pub = rospy.Publisher("/MAC/dcapz_dEPMz_topic", Vector3, queue_size=1000)
        self._dcapz_dEPMz_raw_pub = rospy.Publisher("/MAC/dcapz_dEPMz_raw_topic", Vector3, queue_size=1000)
        self._robot_sub = rospy.Subscriber("/mitsubishi_arm/joint_states", JointState, self._robot_pose_cb)
        self._capsule_sub = rospy.Subscriber("/MAC/mac/odom", Odometry, self._capsule_pose_cb)

        # Robot info
        self._robot = self._wait_and_get_robot()
        self._tree = kdl_tree_from_urdf_model(self._robot)
        self._chain_to_magnet = self._tree.getChain("base_link", "magnet_center")
        self._fksolver_magnet = KDL.ChainFkSolverPos_recursive(self._chain_to_magnet)
        self._q_cur = KDL.JntArray(self._chain_to_magnet.getNrOfJoints())

        # Initialize timer
        pub_timer = rospy.Timer(rospy.Duration(0.01), self._determine_coupling_state)
    def DesignFilter(self):
        
        nyq_rate = self.fs / 2

        self.coefb_low, self.coefa_low = sp.butter(self.filter_order, 
                                        [self.f_low / nyq_rate], btype='low')
        
        self.coefb_high, self.coefa_high = sp.butter(self.filter_order, 
                                            [self.f_high / nyq_rate], btype='high')
def prepare_audio_filters():
    forder = 256
    forderd = 0

    tf_rangel = 100000
    tf_rangeh = 170000
    # audio filters
    tf = SP["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)],
        1,
        15,
    )
    Baudl, Aaudl = sps.butter(N, Wn, btype="bandpass")

    tf = SP["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)],
        1,
        15,
    )
    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,
    )
    Baudr, Aaudr = sps.butter(N, Wn, btype="bandpass")

    N, Wn = sps.buttord(0.016 / (afreq / 2.0), 0.024 / (afreq / 2.0), 2, 15)
    audiolp_filter_b, audiolp_filter_a = sps.butter(N, Wn)

    # USE FIR
    audiolp_filter_b = sps.firwin(257, 0.020 / (afreq / 2.0))
    audiolp_filter_a = [1.0]

    N, Wn = sps.buttord(3.1 / (freq / 2.0), 3.5 / (freq / 2.0), 1, 20)
    audiorf_filter_b, audiorf_filter_a = sps.butter(N, Wn, btype="lowpass")

    [Baudrf_FDLS, Aaudrf_FDLS] = fdls.FDLS_fromfilt(audiorf_filter_b, audiorf_filter_a, forder, forderd, 0)
    SP["fft_audiorf_lpf"] = Faudrf = np.fft.fft(Baudrf_FDLS, blocklen)

    [Baudiolp_FDLS, Aaudiolp_FDLS] = fdls.FDLS_fromfilt(audiolp_filter_b, audiolp_filter_a, forder, forderd, 0)

    FiltAPost = np.fft.fft(Baudiolp_FDLS, blocklen)
    SP["fft_audiolpf"] = FiltAPost * FiltAPost  # * FiltAPost

    [Baudl_FDLS, Aaudl_FDLS] = fdls.FDLS_fromfilt(Baudl, Aaudl, forder, forderd, 0)
    [Baudr_FDLS, Aaudr_FDLS] = fdls.FDLS_fromfilt(Baudr, Aaudr, forder, forderd, 0)

    Faudl = np.fft.fft(Baudl_FDLS, blocklen)
    Faudr = np.fft.fft(Baudr_FDLS, blocklen)

    SP["fft_audio_left"] = Faudrf * Faudl * fft_hilbert
    SP["fft_audio_right"] = Faudrf * Faudr * fft_hilbert
Exemple #31
0
def shave(signal, factor=4, wn=0.005, plot=False, clip=None):
    """
    This function reduces the number of peaks in a signal. It does this by first
    passing the signal through a Butterworth low-pass filter to get a trend. Then
    the a standard deviation of the signal is calculated and a band is by
    adding/substracting the trend with a standard deviation times a factor.

    :param signal: The signal to be shaved
    :param factor: The factor used to multiply the standard deviation when calculating
       the allowed band
    :param wn: Cutoff frequency of the Butterworth low-pass filter.
    :param plot: True to plot the original signal, new signal and band
    :param clip: List with the low and high clip values.

    :return: Shaved signal.
    """

    if clip is not None:
        clip_signal = np.clip(signal, *clip)
        bad_samples = (signal > clip[1]) | (signal < clip[0])
    else:
        clip_signal = signal
        bad_samples = [False] * len(clip_signal)

    butter_params = butter(5, wn)
    mean_signal = np.mean(clip_signal)
    clip_signal = np.hstack((mean_signal, clip_signal, mean_signal))
    trend = filtfilt(*butter_params, clip_signal)[1:-1]
    clip_signal = clip_signal[1:-1]
    core_signal = clip_signal - trend
    std_signal = np.std(core_signal)

    upper_bound = trend + factor * std_signal
    lower_bound = trend - factor * std_signal

    bad_samples |= (signal > upper_bound) | (signal < lower_bound)

    bad_samples_idx = np.where(bad_samples)[0]

    for i, i_signal in enumerate(bad_samples_idx):
        i_0 = prev_good(bad_samples_idx, i)
        i_1 = next_good(bad_samples_idx, i)

        if i_0 >= len(bad_samples):
            i_0 = i_1
            i_1 = next_good(bad_samples_idx, bad_samples_idx.index(i_1 - 1))

        if i_1 < 0:
            i_1 = i_0
            i_0 = prev_good(bad_samples_idx, bad_samples_idx.index(i_0))

        clip_signal[i_signal] = lerp(i_signal, i_0, i_1, clip_signal[i_0],
                                     clip_signal[i_1])

    if plot:
        import matplotlib.pyplot as plt
        plt.plot(signal, "b")
        plt.plot(clip_signal, "y")
        plt.plot(upper_bound, "g")
        plt.plot(lower_bound, "g")
        plt.plot(trend, "m")
        plt.plot(bad_samples_idx, signal[bad_samples_idx], ".r")
        plt.show()

    return clip_signal
Exemple #32
0
def calculate_eeg_area(epoch_df, sf=2048):
    y = epoch_df.drop('time', axis=1).mean(axis=1)
    b2, a2 = signal.butter(4, 200/(sf/2), btype='lowpass')
    envelope = signal.filtfilt(b2, a2, np.abs(y))
    area = np.trapz(envelope, epoch_df['time'].values)
    return area
Exemple #33
0
observations = np.loadtxt(file_observations)
observations = observations[:np.argmax(observations)]
observations = observations[(observations <= 0).nonzero()[0][-1] + 1:1000]
times = np.arange(len(observations)) * tstep_observation

fig = plt.figure(figsize=[myplot.figure_width, myplot.figure_width * 0.5])
ax = plt.gca()
ax.plot(times, observations, 'b', label='Experimental observations')

ax.set(xlabel=r'Time [s]',
       ylabel=r'neutron count (per $\Delta t$)',
       title=r"Estimation of the time of reactivity insertion.")
#ax.xlabel(r'$t' + myplot.unit('s') + r'$')
#plt.ylabel(r'neutron count (per $\Delta t$)')

b, a = signal.butter(5, 0.05)
observations_smoothed = signal.filtfilt(b, a, observations)
ax.plot(times,
        observations_smoothed,
        'y',
        linewidth=3,
        label='Smoothed signal')

max_resting = max(
    observations_smoothed[:min(50, math.ceil(5 / tstep_observation))])

ind_start = np.asarray(
    observations_smoothed >= 2 * max_resting).nonzero()[0][0]
ax.axvline(ind_start * tstep_observation,
           color='k',
           label='Approximate time of reactivity insertion',
Exemple #34
0
import shlex

#Defining H(z) from numerator and denominator coefficents
def H(z,num,den):
	Num = np.polyval(num,z**(-1))
	Den = np.polyval(den,z**(-1))
	return Num/Den

#Reading the soundfile 	
x,fs = sf.read('Sound_Noise.wav')
order = 4
fc = 4000.0
Wn = 2*fc/fs

#Passing butterworth filter
num,den = signal.butter(order,Wn,'low')

#Preparing own routine filter design
#Constructing H(z)
k = np.arange(len(x))
w = 2*np.pi*k/len(x)
z = np.exp(1j * w)
H_z = H(z,num,den)

#Computing DFT(FFT) of x(n)
X = np.fft.fft(x)

#Multiplying X and H to give Y and computig IDFT(IFFT) to give y(n)
Y = np.multiply(H_z,X)
y = np.fft.ifft(Y).real
Exemple #35
0
#Kutter signalene
data_1, i1 = make_nice(data_1, N)
data_2 = make_nice_around(data_2, N, i1 +
                          100)  #legger til 100 fordi indexen er etter klipp
data_3 = make_nice_around(data_3, N, i1 + 100)

#plotter kuttede signaler
plt.plot(data_1, 'blue')
plt.plot(data_2, 'green')
plt.plot(data_3, 'red')
plt.title('Cut signals')
plt.show()

#FILTRERING SKJER HER
soshp = signal.butter(10, 50, 'hp', fs=Fs, output='sos')  #Høypassfilter
soslp = signal.butter(10, 1000, 'lp', fs=Fs, output='sos')  #Lavpassfilter

data_1 = signal.sosfilt(soshp, data_1)
data_1 = signal.sosfilt(soslp, data_1)

data_2 = signal.sosfilt(soshp, data_2)
data_2 = signal.sosfilt(soslp, data_2)

data_3 = signal.sosfilt(soshp, data_3)
data_3 = signal.sosfilt(soslp, data_3)

#Plotter filtrert filtrert data
plt.plot(data_1, 'blue')
plt.plot(data_2, 'green')
plt.plot(data_3, 'red')
Exemple #36
0
def butter_low_pass(low_cut, fs, order=5):
    nyq = 0.5 * fs
    low = low_cut / nyq
    b, a = butter(order, low, btype='low')
    return b, a
Exemple #37
0
 def butter_lowpass(cutoff, fs, order):
     nyq = 0.5 * fs
     normal_cutoff = cutoff / nyq
     return butter(order, normal_cutoff, btype='low', analog=False)
Exemple #38
0
def butter_highpass(cutoff, fs, order=10):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq #[0.30, 0.95]
    b, a = signal.butter(order, [0.2, 0.48], btype='bandpass', analog=False, output='ba')
    return b, a
Exemple #39
0
    def load_MonitoringVI_file(self, filename, temp_list = None, process_therm = 1):
        '''Reads in thermometer data text file created by  MonitoringVI, and plots the temperature as a function of time.

        temp_list is a list of tuples, [(heater_voltage,temperature), ...] which are plotted on top of the temperature
        versus time points. This allows one to visually check the calibration, temp_list.

        process_therm is the column number of the thermometer whose data is processed by several filtering algorithms and
        plotted.
        '''


        pos = filename.rfind(os.sep)

        try:
            with io.open(filename[:pos+1]+ 'Make_ScanData.m',mode='r') as f:
                while 1:
                    line  = f.readline()
                    if line == '': # End of file is reached
                        break
                    elif line.find('ScanData.Heater_Voltage') >= 0:
                        Voltages = line[line.find('['):line.find(']')+1]
                        break
        except:
            print('Unable to find or read Make_ScanData.m for list of heater voltages')
            Voltages = 'Unknown'

        with io.open(filename,mode='r') as f:

            temp_data_header = ''
            while temp_data_header.strip() =='':
                temp_data_header = f.readline()

            therm_list = [t for t in temp_data_header.strip().split('\t')[1:] if (t.strip() != 'None') & (t.strip() != '')]


        temp_data = np.loadtxt(filename, dtype=np.float, comments='#', delimiter=None, converters=None, skiprows=3, usecols=None, unpack=False, ndmin=0)

        num_col  = temp_data.shape[1]
        start_col = 1 #index of first column in data that has thermometer data
        if process_therm > num_col - start_col:
            print('process_therm = {} exceeds number of thermometers in data. Choose an lower number. Aborting...'.format(process_therm))
            return

        # Gaussian Filter
        num_pts_in_gaussian_window = 20
        b = gaussian(num_pts_in_gaussian_window, 10)
        ga = filters.convolve1d(temp_data[:,process_therm], b/b.sum())

        # buterworth Filter
        npts = temp_data[:,process_therm].size
        end = temp_data[-1,0]
        dt = end/float(npts)
        nyf = 0.5/dt
        b, a = butter(4, .1)#1.5/nyf)
        fl = filtfilt(b, a, temp_data[:,process_therm])

        #Spline Fit
        sp = UnivariateSpline(temp_data[:,0], temp_data[:,process_therm])

        #weiner filter
        wi = wiener(temp_data[:,process_therm], mysize=40, noise=10)

        fig1 = plt.figure( facecolor = 'w',figsize = (10,10))
        ax = fig1.add_subplot(1,1,1)

        if isinstance(temp_list, list):
            for temp_tuple in temp_list:
                hline = ax.axhline(y = temp_tuple[1],linewidth=1, color='g', alpha = 0.3 ,linestyle = ':',   label = None)


        color_incr = 1.0/(num_col-start_col)
        for therm_num in range(start_col, num_col): # plot all thermometer data present
            line = ax.plot(temp_data[:,0], temp_data[:,therm_num],color=(0,color_incr*therm_num,0), alpha = 0.4 if therm_num != 1 else 1, linewidth = 3,label = therm_list.pop(0) if therm_list[0] != None else 'Therm{0}'.format(therm_num))

        #plot filter outputs for THE FIRST thermometer only
        line2 = ax.plot(temp_data[:,0], ga, 'y', linewidth = 3, label = 'Gaussian Conv') # Gaussian Convolution
        line3 = ax.plot(temp_data[:,0], fl, 'c', linewidth = 3, label = 'Butterworth') # butterworth
        line4 = ax.plot(temp_data[:,0], sp(temp_data[:,0]), 'k', linewidth = 3, label = 'Spline') # bspline
        line5 = ax.plot(temp_data[:,0], wi, 'r', linewidth = 3, label = 'Weiner') # weiner

        ax.grid(b=True, which='major', color='b', alpha = 0.2, linestyle='-')
        ax.grid(b=True, which='minor', color='b', alpha = 0.2,linestyle='--')
        ax.set_title('Heater Voltages = {}'.format(Voltages), fontsize=12)
        ax.set_ylabel('Temperature [Kelvin]')
        ax.set_xlabel('Seconds')
        ax.legend(loc = 'best', fontsize=10,scatterpoints =1, numpoints = 1, labelspacing = .1)
        plt.show()
Exemple #40
0
# integer filter

# sampling rate
fs = 1000

# cutoffs
f1 = 45
f2 = 55

# scaling factor in bits
q = 14
# scaling factor as facor...
scaling_factor = 2**q

# let's generate a sequence of 2nd order IIR filters
sos = signal.butter(2, [f1 / fs * 2, f2 / fs * 2], 'stop', output='sos')

sos = np.round(sos * scaling_factor)

# print coefficients
for biquad in sos:
    for coeff in biquad:
        print(int(coeff), ",", sep="", end="")
    print(q)

# plot the frequency response
b, a = signal.sos2tf(sos)
w, h = signal.freqz(b, a)
pl.plot(w / np.pi / 2 * fs, 20 * np.log(np.abs(h)))
pl.xlabel('frequency/Hz')
pl.ylabel('gain/dB')
Exemple #41
0
def butter_lowpass(curoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype='lowpass', analog=False)
    return b, a
 def _filter(self, samples):
     b, a = signal.butter(self.settings["filter"]["order"],
                          self.settings["filter"]["cutoff"],
                          self.settings["filter"]["response"],
                          fs=self.settings["sr"])
     return signal.filtfilt(b, a, samples)
Exemple #43
0
 def bandpass(self, lowcut, highcut, order=4):
     nyq = 0.5 * self.fs
     low = lowcut / nyq
     high = highcut / nyq
     b, a = butter(order, [low, high], btype='bandpass')
     self.filters.append((b, a))
def butter_bandpass(lowcut, highcut, fs, order):
    nyq = 0.5 * fs  # fs / 2
    low = lowcut / nyq  # lowcut * 2 / fs
    high = highcut / nyq
    b, a = signal.butter(order, [low, high], btype='band')
    return b, a
 def _filter_data(data: List[float]) -> List[float]:
     b, a = signal.butter(3, 0.05)
     return signal.lfilter(b, a, data)
Exemple #46
0
import mne
import collections
from scipy.ndimage.filters import maximum_filter1d as maxfilter
from scipy.signal import butter, filtfilt

execfile("local_settings.py")
execfile("src/findpeak.py")
execfile("src/normalize.py")

name = "Jared_04_03_17"

data_dir = op.realpath(op.join("..", "..", "data"))

raw = mne.io.read_raw_fif(op.join(temp_dir, name + "_noblink.fif"))
events = raw.copy().load_data().pick_channels(['Erg1'])
events.filter(l_freq=40, h_freq=None, picks=[0], phase='zero')

x = events.get_data()
y = np.maximum(0, x)

sample_rate = 512
b, a = butter(2, 2.5 / (512.0 / 2), 'low')
y = filtfilt(b, a, y)

yd = np.hstack([[[0]], np.diff(y)])
peaks = findpeaks(np.squeeze(yd), 0.99, 512 * 3)

# plt.plot(np.hstack([x.T/np.max(x),yd.T/np.max(yd)]))
# for i in peaks:
#   plt.axvline(x=i,color='red')
    plt.show()
    plt.figure()
    #plt.plot(abs(fft(Out[35000:65000])))
    #plt.plot(abs(Four))
    # for Ind in range(frameLen,Ln - frameLen,frameLen):
    #     Out[Ind-Flen//2:Ind+Flen//2] = Out[Ind-Flen//2:Ind+Flen//2]*wnd
    return real(Out)


dir = 'result'
cutOff = 1000  # Cutoff frequency
nyq = 0.5 * Fr1
# cutOff1 = 1450
# cutOff2 = 1550
# fc1 = cutOff1 / nyq
# fc2 = cutOff2 / nyq
# print(fc1, fc2)
cutOff = 150
fc = cutOff / nyq
# prin t(fc)
B, A = sgn.butter(9, 0.2)
U, V = sgn.freqz(B, A)  # рисует передаточную функцию фильтра
plt.plot(U, abs(V))
out = restSign(np.array(Dat, dtype=np.float64), frameLen, result)
fout = fft(out[:1024])
plt.cla()
plt.figure()
plt.plot(fout)
plt.show()
out = sgn.lfilter(B, A, out)
write('result4.wav', Fr1, np.asarray(out, dtype=np.int16))
Exemple #48
0
def put_audio():
    """
        Getting Constants from Constants.py
        Variables must be extracted from the Variables_dict who is managed by Disk_IO.
    """
    """ Variables and constants
    """
    f_hp                = 35
    Variables_dict      = recall_pickled_dict()                     # Load Variables_dict
    SampRate            = Variables_dict["v.SamplingFreq"]          # Sampling Frequency, Hz
    Length              = Variables_dict["v.Length"]                # Pile length, m
    Speed               = Variables_dict["v.Speed"]                 # Wave speed
    samples_acq         = Variables_dict["v.samples_acq"]           # N. of Samples acquired: c.n_blocks multiple
    AveragesRequired    = int(Variables_dict["v.AveragesRequired"]) # Required Averages number
    #
    FORMAT              = aa.PCM_FORMAT_S32_LE                      # Int 4 Bytes signed
    CHANNELS            = int(2)
    byte_width          = 4
    bytes_size          = CHANNELS * byte_width * c.n_frames        # Bytes in each period

    """ Create the data_out alsaaudio object instance
        In PCM_NORMAL mode, the call will block if the kernel buffer is full, and until enough sound has been 
        played to allow the sound data to be buffered. The call always returns the size of the data provided.
    """
    #data_out = aa.PCM(aa.PCM_PLAYBACK, aa.PCM_NONBLOCK)  # aa.PCM_NORMAL  aa.PCM_NONBLOCK
    data_out = aa.PCM(aa.PCM_PLAYBACK, aa.PCM_NORMAL)
    data_out.setchannels(CHANNELS)
    data_out.setrate(SampRate)
    data_out.setformat(FORMAT)
    data_out.setperiodsize(c.n_frames)
    #
    """ x1x2: vector of interleaved channels, Ch1, Ch2, output signal
    """
    #
    A               = 0.5 * (2**31 - 1)                                 # Amplitude referr. to 32 bit int f.s.
    dt              = 1.0 / 96000                                       # Sampling period
    T               = 1                                                 # Total signal time length
    N               = int(T / dt)                                       # Number of samples
    L_echo_0        = Length                                            # Toe echo
    k_A_echo_0      = -1 * 0.1                                          # Echo_0 reflexion coefficient: 1 => free; -1 => vincolated
    T_echo_0        = 2 * Length / Speed                                # Return time of echo_0
    indx_echo_0     = round(T_echo_0 / dt)                              # Index of the 1.st echo_0 sample
    T_effective     = hammer_DT                                         # Pulse equivalent length
    T_pulse         = T_effective * 3/2                                 # Length at the sine base, valid only for the half sine
    N_pulse         = round(T_pulse / dt)                               # Number of pulse samples
    t               = np.arange(N_pulse) * dt                           # Time length of the pulse sample
    pulse           = np.sin(np.pi * t / T_pulse)                       # Pulse as a positive half sine
    pulse_echo_0    = k_A_echo_0 * pulse                                # Pulse echo_0
    x1f             = np.zeros(N)                                       # Array of 0's, ch1
    x2f             = np.zeros(N)                                       # Array of 0's, ch2
    x1f[0:N_pulse]  = pulse                                             # Put pulse at index 0 for ch1
    x2f[0:N_pulse]  = pulse                                             # Put pulse at index 0 for ch2
    x2f[indx_echo_0:indx_echo_0+N_pulse] = pulse_echo_0                 # Put echo_0 delayed pulse on ch2
    
    #
    # Insertion of a HP 1.st order filter
    fnyquist        = 0.5*SampRate
    w_d             = f_hp / fnyquist                                  # Digital pulsation, 0..1, where 1 => fnyquist
    b, a            = butter(1, w_d, btype='high')                     # Digital HP Butterworth filter first order  
    y2f_hp_f        = lfilter(b, a, x2f)                               # Filtering the signal
    #
    pk              = np.max(x1f)                                       # Normalize amplitude
    x1f             *= A / pk                                           # Float type, original, ch1
    y2f_hp_f        *= A / pk                                           # Float type, HP filtered, ch2
    x1              = x1f.astype(np.int32)                              # Convert to int32, ch1
    x2              = y2f_hp_f.astype(np.int32)                         # Convert to int32, 
    x1x2            = np.zeros(CHANNELS*c.N_of_samples, dtype=np.int32) # Array for interleaved Ch1, Ch2 data
    #
    x1x2[0::2]  = x1                                                    # Fill x1 at even indexes: ch1
    x1x2[1::2]  = x2                                                    # Fill x1 at odd indexes:  ch2
    #
    out_big_buffer    = bytearray(bytes_size * c.n_blocks)
    out_big_buffer[:] = pack('%ii' % int(2*c.N_of_samples), *x1x2)      # Pack from numpy.int32 to bytes
    out_short_buffer  = bytearray(bytes_size)                           # Output array written one frame at time
    #
    if DEBUG:
        tp  = np.arange(0, N, 1) * dt
        sel = int(1.2 * indx_echo_0)

        fig = plt.figure(1)
        fig.set_size_inches(w = 15, h = 9)
        fig.subplots_adjust(hspace = 0.35)
        plt.subplot(311)
        plt.plot(tp[0:sel], x1[0:sel])
        plt.title('Hammer')
        plt.xlabel('Time [s]')
        plt.ylabel('Force')
        plt.grid(True)
        #
        plt.subplot(312)
        plt.plot(tp[0:sel], x2f[0:sel])
        plt.title('Acceleration')
        plt.xlabel('Time [s]')
        plt.ylabel('Acceleration')
        plt.grid(True)
        #
        plt.subplot(313)
        plt.plot(tp[0:sel], y2f_hp_f[0:sel])
        plt.title('Acceleration, HP filtered')
        plt.xlabel('Time [s]')
        plt.ylabel('Acceleration')
        plt.grid(True)
        #
        plt.show()
    #   #

    time.sleep(3)
    for n in range(AveragesRequired):
        print("Started pulse N°", n + 1)
        beg = 0
        end = bytes_size
        for i in range(c.n_blocks):
            out_short_buffer[:] = out_big_buffer[beg:end]
            size = data_out.write(out_short_buffer)
            #print(size)
            beg = end
            end += bytes_size
        #   #
        time.sleep(4.5)
    #   #
    data_out.close()
    print("put_audio.py terminated")
Exemple #49
0
# ac_freq = globs['magnetom_ac_frequency']
ac_amps = globs['magnetom_rabi_spectrum_amps']
ac_freqs = globs['magnetom_rabi_spectrum_freqs']
fsweep_requested = (globs['magnetom_rabi_sweep_initial'],
                    globs['magnetom_rabi_sweep_final'])

# Compute actual bounds of sweep
amp_bounds = (bad_amp_calib(fsweep_requested[0]),
              bad_amp_calib(fsweep_requested[1]))
# amp_bounds = (amp_calib(fsweep_requested[0], shot_id), amp_calib(fsweep_requested[1], shot_id))
fsweep_actual = (poly_freq_calib(amp_bounds[0]),
                 poly_freq_calib(amp_bounds[1]))
rabi_range = abs(fsweep_actual[1] - fsweep_actual[0])

# apply butterworth filter (same as rabi_sweep)
filter_b, filter_a = signal.butter(8, 2 * filter_cutoff / lockin_sample_rate)
filter_Q = signal.filtfilt(filter_b, filter_a, lockin_Q)

# Repeat Rabi sweep frequency calibrations for lockin trace:
amp_map = np.vectorize(amp_map)
rf_amps_lockin = amp_map(t_lockin, t0, rabiT, amp_bounds[0], amp_bounds[1])
rabi_freqs_lockin = poly_freq_calib(rf_amps_lockin)
ac_res_time_lockins = [
    t_lockin[np.argmin(abs(rabi_freqs_lockin - ac_freq))]
    for ac_freq in ac_freqs
]
ac_res_time_lockin = np.mean(ac_res_time_lockins)
ac_res_rabi_freqs = [
    rabi_freqs_lockin[np.argmin(abs(rabi_freqs_lockin - ac_freq))]
    for ac_freq in ac_freqs
]
Exemple #50
0
camera.resolution = res
camera.framerate = 10

# Initialize the buffer and start capturing
rawCapture = PiYUVArray(camera, size=res)
stream = camera.capture_continuous(rawCapture,
                                   format="yuv",
                                   use_video_port=True)

# Measure the time needed to process 300 images to estimate the FPS
t = time.time()

# To filter the noise in the image we use a 3rd order Butterworth filter

# Wn = 0.02, the cut-off frequency, acceptable values are from 0 to 1
b, a = butter(3, 0.1)

line_pos = CAMERA_CENTER
first_frame = True

# start car
motor.duty_cycle = MOTOR_BRAKE + 120000
track_width = 0

# PID constants for sensitivity
_p = 1.0
i = 0.09
d = 0.08

# array to keep track of pid data
error_history = np.array([], dtype=int)
def butter_bandpass(highcut, fs, order=5):
    nyq = 0.5 * fs
    high = highcut / nyq
    b, a = signal.butter(order, high, 'high', analog=False)
    return b, a
Exemple #52
0
    from uk import pts, body

    # generate 3D trajectory
    N = 100
    Np = 1
    x0 = np.array([[0., 0., 0., 0., 0., 500.]]).T
    xs = np.diag(
        np.hstack((np.array([1, 1, 1]) * 1., np.array([1, 1, 1]) * 10.)))
    x = np.cumsum(np.dot(xs, np.random.randn(6, N)), axis=1) + np.kron(
        np.ones((1, N)), x0)

    g = 10. * np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]],
                       dtype=float).T

    # smooth trajectory
    B, A = sig.butter(1, 0.05)
    x = sig.filtfilt(B, A, x)

    th = np.random.rand() * 2 * np.pi
    om = np.array([[0, 0, 1], [0, 0, 0], [-1, 0, 0]])
    R = sp.linalg.expm(th * om)
    t = np.dot(R, -x0[3:6, :]) + x0[3:6, :]
    A = np.array([[2000., 0, 0], [0, 2000., 0], [0, 0, 1.]])
    d = np.array([0., 0., 0., 0.])

    cams = [{
        'R': np.identity(3),
        't': np.zeros((3, 1)),
        'A': A,
        'd': d
    }, {
def main():
    xIMUdata = xIMU.xIMUdataClass(filePath, 'InertialMagneticSampleRate',
                                  1 / samplePeriod)
    time = xIMUdata.CalInertialAndMagneticData.Time
    gyrX = xIMUdata.CalInertialAndMagneticData.gyroscope[:, 0]
    gyrY = xIMUdata.CalInertialAndMagneticData.gyroscope[:, 1]
    gyrZ = xIMUdata.CalInertialAndMagneticData.gyroscope[:, 2]
    accX = xIMUdata.CalInertialAndMagneticData.accelerometer[:, 0]
    accY = xIMUdata.CalInertialAndMagneticData.accelerometer[:, 1]
    accZ = xIMUdata.CalInertialAndMagneticData.accelerometer[:, 2]

    indexSel = np.all([time >= startTime, time <= stopTime], axis=0)
    time = time[indexSel]
    gyrX = gyrX[indexSel]
    gyrY = gyrY[indexSel]
    gyrZ = gyrZ[indexSel]
    accX = accX[indexSel]
    accY = accY[indexSel]
    accZ = accZ[indexSel]

    # Compute accelerometer magnitude
    acc_mag = np.sqrt(accX * accX + accY * accY + accZ * accZ)

    # HP filter accelerometer data
    filtCutOff = 0.001
    b, a = signal.butter(1, (2 * filtCutOff) / (1 / samplePeriod), 'highpass')
    acc_magFilt = signal.filtfilt(b,
                                  a,
                                  acc_mag,
                                  padtype='odd',
                                  padlen=3 * (max(len(b), len(a)) - 1))

    # Compute absolute value
    acc_magFilt = np.abs(acc_magFilt)

    # LP filter accelerometer data
    filtCutOff = 5
    b, a = signal.butter(1, (2 * filtCutOff) / (1 / samplePeriod), 'lowpass')
    acc_magFilt = signal.filtfilt(b,
                                  a,
                                  acc_magFilt,
                                  padtype='odd',
                                  padlen=3 * (max(len(b), len(a)) - 1))

    # Threshold detection
    stationary = acc_magFilt < 0.05

    fig = plt.figure(figsize=(10, 5))
    ax1 = fig.add_subplot(2, 1, 1)
    ax2 = fig.add_subplot(2, 1, 2)
    ax1.plot(time, gyrX, c='r', linewidth=0.5)
    ax1.plot(time, gyrY, c='g', linewidth=0.5)
    ax1.plot(time, gyrZ, c='b', linewidth=0.5)
    ax1.set_title("gyroscope")
    ax1.set_xlabel("time (s)")
    ax1.set_ylabel("angular velocity (degrees/s)")
    ax1.legend(["x", "y", "z"])
    ax2.plot(time, accX, c='r', linewidth=0.5)
    ax2.plot(time, accY, c='g', linewidth=0.5)
    ax2.plot(time, accZ, c='b', linewidth=0.5)
    ax2.plot(time, acc_magFilt, c='k', linestyle=":", linewidth=1)
    ax2.plot(time, stationary, c='k')
    ax2.set_title("accelerometer")
    ax2.set_xlabel("time (s)")
    ax2.set_ylabel("acceleration (g)")
    ax2.legend(["x", "y", "z"])
    plt.show(block=False)

    # Compute orientation
    quat = np.zeros((time.size, 4), dtype=np.float64)

    # initial convergence
    initPeriod = 2
    indexSel = time <= time[0] + initPeriod
    gyr = np.zeros(3, dtype=np.float64)
    acc = np.array([
        np.mean(accX[indexSel]),
        np.mean(accY[indexSel]),
        np.mean(accZ[indexSel])
    ])
    mahony = ahrs.filters.Mahony(Kp=1,
                                 Ki=0,
                                 KpInit=1,
                                 frequency=1 / samplePeriod)
    q = np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float64)
    for i in range(0, 2000):
        q = mahony.updateIMU(q, gyr=gyr, acc=acc)

    # For all data
    for t in range(0, time.size):
        if (stationary[t]):
            mahony.Kp = 0.5
        else:
            mahony.Kp = 0
        gyr = np.array([gyrX[t], gyrY[t], gyrZ[t]]) * np.pi / 180
        acc = np.array([accX[t], accY[t], accZ[t]])
        quat[t, :] = mahony.updateIMU(q, gyr=gyr, acc=acc)

    # -------------------------------------------------------------------------
    # Compute translational accelerations

    # Rotate body accelerations to Earth frame
    acc = []
    for x, y, z, q in zip(accX, accY, accZ, quat):
        acc.append(q_rot(q_conj(q), np.array([x, y, z])))
    acc = np.array(acc)
    acc = acc - np.array([0, 0, 1])
    acc = acc * 9.81

    # Compute translational velocities
    # acc[:,2] = acc[:,2] - 9.81

    # acc_offset = np.zeros(3)
    vel = np.zeros(acc.shape)
    for t in range(1, vel.shape[0]):
        vel[t, :] = vel[t - 1, :] + acc[t, :] * samplePeriod
        if stationary[t] == True:
            vel[t, :] = np.zeros(3)

    # Compute integral drift during non-stationary periods
    velDrift = np.zeros(vel.shape)
    stationaryStart = np.where(np.diff(stationary.astype(int)) == -1)[0] + 1
    stationaryEnd = np.where(np.diff(stationary.astype(int)) == 1)[0] + 1
    for i in range(0, stationaryEnd.shape[0]):
        driftRate = vel[stationaryEnd[i] - 1, :] / (stationaryEnd[i] -
                                                    stationaryStart[i])
        enum = np.arange(0, stationaryEnd[i] - stationaryStart[i])
        drift = np.array(
            [enum * driftRate[0], enum * driftRate[1], enum * driftRate[2]]).T
        velDrift[stationaryStart[i]:stationaryEnd[i], :] = drift

    # Remove integral drift
    vel = vel - velDrift
    fig = plt.figure(figsize=(10, 5))
    plt.plot(time, vel[:, 0], c='r', linewidth=0.5)
    plt.plot(time, vel[:, 1], c='g', linewidth=0.5)
    plt.plot(time, vel[:, 2], c='b', linewidth=0.5)
    plt.legend(["x", "y", "z"])
    plt.title("velocity")
    plt.xlabel("time (s)")
    plt.ylabel("velocity (m/s)")
    plt.show(block=False)

    # -------------------------------------------------------------------------
    # Compute translational position
    pos = np.zeros(vel.shape)
    for t in range(1, pos.shape[0]):
        pos[t, :] = pos[t - 1, :] + vel[t, :] * samplePeriod

    fig = plt.figure(figsize=(10, 5))
    plt.plot(time, pos[:, 0], c='r', linewidth=0.5)
    plt.plot(time, pos[:, 1], c='g', linewidth=0.5)
    plt.plot(time, pos[:, 2], c='b', linewidth=0.5)
    plt.legend(["x", "y", "z"])
    plt.title("position")
    plt.xlabel("time (s)")
    plt.ylabel("position (m)")
    plt.show(block=False)

    # -------------------------------------------------------------------------
    # Plot 3D foot trajectory

    posPlot = pos
    quatPlot = quat

    extraTime = 20
    onesVector = np.ones(int(extraTime * (1 / samplePeriod)))

    # Create 6 DOF animation
    fig = plt.figure(figsize=(7, 7))
    ax = fig.add_subplot(111, projection='3d')  # Axe3D object
    ax.plot(posPlot[:, 0], posPlot[:, 1], posPlot[:, 2])
    min_, max_ = np.min(np.min(posPlot,
                               axis=0)), np.max(np.max(posPlot, axis=0))
    ax.set_xlim(min_, max_)
    ax.set_ylim(min_, max_)
    ax.set_zlim(min_, max_)
    ax.set_title("trajectory")
    ax.set_xlabel("x position (m)")
    ax.set_ylabel("y position (m)")
    ax.set_zlabel("z position (m)")
    plt.show(block=False)

    plt.show()
def high_pass_filter(sub_data,cutoff):
    nyq = 0.5 * 256
    normal_cutoff = cutoff / nyq
    b, a = signal.butter(2, normal_cutoff, btype='high', analog=False)
    hpf = signal.filtfilt(b, a, sub_data)
    return hpf
Exemple #55
0



NewSound = GuassianNoise + array





#write("New-Sound-Added-With-Guassian-Noise.wav", Frequency, NewSound) # Saving it to the file.




b,a = signal.butter(5, 500/(Frequency/2), btype='highpass') # ButterWorth filter 4350

# In[20]:


filteredSignal = signal.lfilter(b,a,NewSound)
plt.plot(filteredSignal) # plotting the signal.
plt.title('Highpass Filter')
plt.xlabel('Frequency(Hz)')
plt.ylabel('Amplitude')

write("New-Filtered-Eagle-Sound.wav", Frequency, filteredSignal)



'''
Exemple #56
0
 def butter_lowpass_filter(dat, highcut, fs, order=5):
     nyq = 0.5 * fs
     high = highcut / nyq
     b, a = butter(order, high, output='ba')
     y = lfilter(b, a, dat)
     return y
"""
start_sec = 100
end_sec = 300

start_fr = start_sec * frames_per_second
end_fr = end_sec * frames_per_second

#%%

#LFP DLC data (lstsq_dlc_norm)
#cutoff freq at 8 Hz (or maybe 10Hz, but use 8Hz for now)
order = 4
sampling_freq = 25
cutoff_freq = 4
normalized_cutoff_freq = 2 * cutoff_freq / sampling_freq
numerator_coeffs, denominator_coeffs = signal.butter(order,
                                                     normalized_cutoff_freq)
#lstsq_dlc_norm_aligned_offset_zeroed_filt_test = signal.filtfilt(numerator_coeffs, denominator_coeffs,lstsq_dlc_norm_aligned_offset_zeroed.loc[start_fr:end_fr,:],axis=0)
lstsq_dlc_norm_aligned_offset_zeroed_filt_test = signal.filtfilt(
    numerator_coeffs,
    denominator_coeffs,
    lstsq_dlc_norm_aligned_offset_zeroed,
    axis=0)

#lstsq_dlc_norm_aligned_offset_zeroed_filt_test = lstsq_dlc_norm_aligned_offset_zeroed
#z,p,k = butter(order,normalized_cutoff_freq,output='zpk')
#print(p)

#%% check the differences between filtered and unfiltered data
"""
filtered = lstsq_dlc_norm_aligned_offset_zeroed_filt_test
unfiltered = lstsq_dlc_norm_aligned_offset_zeroed.to_numpy()
    for row in reader:
        if j == 8:
            j = 0
            t = ((float(row[0]) / 1000) - timeOffest)
            if t > 0:
                time.append(t)
                voltage.append(
                    (int(row[1]) * 5 / 1024) / (7500 / (37500))
                )  # equation to convert analog reading into voltage
                current.append(((int(row[2])) - 510) * 5 / 1024 / 0.04 -
                               0.04)  # convert analog current into amps
        else:
            j += 1

# Butterworth signal filtering to smooth out raw arduino readings
b, a = signal.butter(4, 0.01, btype='lowpass')
c, d = signal.butter(8, 0.08, btype='lowpass')
filtCurrent = signal.filtfilt(b, a, data['motorCurrentTotal'], padlen=0)
filtVoltage = signal.filtfilt(b, a, data['motorVoltsTotal'], padlen=0)
filtAccel = signal.filtfilt(c, d, data['accelComp'], padlen=0)
filtAccel2 = signal.filtfilt(c, d, data['accel2comp'], padlen=0)
filtLogCurrent = signal.filtfilt(c, d, current, padlen=0)
filtLogVoltage = signal.filtfilt(c, d, voltage, padlen=0)

# State: 0 for unknown, 1 for takeoff, 2 for hover, 3 for climb, 4 for descend. Change this to enum later
state = 0
peaks = [[0, 0]]
craters = [[0, 0]]
peaksfull = [[0, 0]]
cratersfull = [[0, 0]]
maxPeak = 0
Exemple #59
0
def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = signal.butter(order, [low, high], btype='band')
    return b, a
def butter_lowpass(cutoff, fs, order=5):
    nyq = 0.5 * fs  #The Nyquist frequency is half the sampling rate.
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype='low', analog=False)
    return b, a