def data_gen():
    #endless loop which gets data
    while True:
        data = np.zeros(0)
        while c.hasSampleAvailable():
            sample = c.getSampleFromBuffer()
            data = np.append(data, sample[0])
        yield data
Beispiel #2
0
def data_gen():
    #endless loop which gets data
    # filter = MyAmazingFilter(...)
    while True:
        data = np.zeros(0)
        while c.hasSampleAvailable():
            sample = c.getSampleFromBuffer()
            data = np.append(data,sample[0])
            # for filtering add here:
            # data = filter.dofilter(data)
        yield data
Beispiel #3
0
def getDataThread(qtPanningPlot1, qtPanningPlot2):
    """
    Takes unfiltered data from input and filters out the noise.
    """
    # creates stopband filter for 50Hz
    scale = 2**11  # scale for input (raw data between -1 and 1)
    fs = 250
    fc = 50
    sos = sig.butter(2, [(fc - 5) / fs * 2, (fc + 5) / fs * 2],
                     'stop',
                     output='sos')
    filt = IIRFilter(sos)

    global data  # data collected for future figures
    # i and last used to create a time buffer
    # used to reduce the number of times each word is printed
    # and reduce the influence of the wire noise
    i = 0
    last = 0
    # endless loop with sleeps not for timing but for multitasking
    # collects data from input
    while running:
        # loop as fast as we can to empty the kernel buffer
        while c.hasSampleAvailable():
            sample = c.getSampleFromBuffer()
            sample = sample[channel] * scale  # scales input
            # filters out 50Hz
            sample2 = filt.filter(int(np.round(sample)))
            # adds samples to real time graphs
            qtPanningPlot1.addData(sample)
            qtPanningPlot2.addData(sample2)
            # saves unfiltered data for figures later
            data.append(sample)

            # detects whether the object was pushed or pulled
            if sample2 >= 200 and i >= last:
                print('PULL')
                last = i + 200
            elif sample2 <= -100 and i >= last:
                print('PUSH')
                last = i + 150
            else:
                i += 1

        # let Python do other stuff and sleep a bit
        sleep(0.1)
Beispiel #4
0
def getDataThread(qtPanningPlot1,qtPanningPlot2):

    # any filter initialisation goes here
    # filter1 = MyAmazingFilterClass(...)
    # filter2 = MyAmazingFilterClass(...)
    
    # endless loop which sleeps not for timing but for multitasking
    while running:
        # loop as fast as we can to empty the kernel buffer
        while c.hasSampleAvailable():
            sample = c.getSampleFromBuffer()
            v1 = sample[channel_of_window1]
            v2 = sample[channel_of_window2]
            # for filtering of data just add a filter function:
            # v1 = filter1.dofilter(v1)
            # v2 = filter2.dofilter(v2)
            qtPanningPlot1.addData(v1)
            qtPanningPlot2.addData(v2)
        # let Python do other stuff and sleep a bit
        sleep(0.1)
Beispiel #5
0
def getDataThread(qtPanningPlot1, qtPanningPlot2, ringbuffer, frequency, n):

    ############################################
    #set up variables

    #normalise (Wn for butterworth filter is in half-cycles / sample)
    norm = 2 / fs
    plotbuffer = []

    #calculate coefficients
    wide = np.array([(frequency - 11) * norm, (frequency + 11) * norm])

    #sos
    sos_wide = sig.butter(10, wide, btype='bandpass', output='sos')

    #access master IIR filter with coefficients
    master_wide = iir.IIR_filter(sos_wide)

    ############################################

    while running:
        # loop as fast as we can to empty the kernel buffer
        while c.hasSampleAvailable():
            sample = c.getSampleFromBuffer()
            v1 = 10**2 * sample[channel_of_window1]
            #filter data
            v2 = master_wide.dofilter(v1)
            #detect strength of peak
            detect = ((th * m * 10 * abs(v2)))
            print(detect)
            #if in range
            if detect > n:
                #digital outputs
                #3 = red light
                #2 = blue light
                c.digital_out(3, 1)
                c.digital_out(2, 0)

            #if it is close
            elif n > detect > 2:
                c.digital_out(3, 0)
                c.digital_out(2, 1)

            #if no signal is detected
            else:
                c.digital_out(3, 0)
                c.digital_out(2, 0)

            #add filtered data to ringbuffer
            ringbuffer = np.append(ringbuffer, v2)
            #plot incoming signal
            qtPanningPlot1.addData(v1)
            #check if there is data in the ringbuffer
            #if data is found then add it to plotbuffer and reset ringbuffer
            if not ringbuffer == []:
                result = ringbuffer
                ringbuffer = []
                plotbuffer = np.append(plotbuffer, result)

            #only keep the most recent 50 samples of data
            plotbuffer = plotbuffer[-50:]
            #calculate the spectrum
            spectrum = np.fft.rfft(plotbuffer)
            # absolute value
            spectrum2 = m * np.absolute(spectrum) / len(spectrum)
            #plot spectrum
            qtPanningPlot2.addData(spectrum2)
#!/usr/bin/python3

import pyusbdux as dux

dux.open()

dux.start(8)

while True:
    sample = dux.getSampleFromBuffer()
    print(sample)