signal = np.zeros(N) # vektor dolžine N, kamor se shranjujejo vzorci flag = False # flag to notify when vector signal is filled with samples previousBOOL = False # na začetku je roka odprta - RMS signala je pod EMG_mejo ################################## #print(plt.style.available) ##plt.style.use('/Users/iripuga/Documents/1.Delo/404/_bci_/BCI-Robotska-Roka/pythonCode/stylelib/bci-style.mplstyle') # Uvoz podatkov altfile = 'a-very-light-test.txt' option = 'stream' #input("Read .txt file or start stream? Type 'txt' or 'stream' >> ") print('option >>>', option) if option == 'txt': """Read a multi-channel time series from file.""" nepopkolan = bci.importData(option) #uvozi offline podatke data = np.array(bci.popcol(nepopkolan, 8)) #rešimo se stolpca z datumom - data je že numpy array data = data[0, :, :] #3D matriko damo v 2D print('Velikost numpy podatkovnega polja:', data.shape) samples = data.shape[0] x = np.arange(0, samples, 1) print(x.shape) # podatki y1 = data[:, 1] # [vrstica, stolpec] y2 = data[:, 2] y3 = data[:, 3] y4 = data[:, 4] ax = data[:, 5]
N = 200 * 5 # sample spacing toleranca = 0.01 # Med 0 in 1 - določa kakšna odstopanja od EMG_meje spremenijo bool vrednost i = 0 # iterator za posodabljanje vzorcev v vektorju signal #signal = np.zeros(N) # vektor dolžine N, kamor se shranjujejo vzorci flag = False # flag to notify when vector signal is filled with samples previousBOOL = False # na začetku je roka odprta - RMS signala je pod EMG_mejo #initialize Serial connection serial = roka.initializeServo() #Acquire data matrika = [] N_min = 0 N_max = N podatki, knjiznica = bci.importData("txt") size = knjiznica["EXG Channel 0"].size for i in range(int(size / N)): moc = (plot.PowerFromTxt(200, 1.0, 50.0, 35.0, N_min, N_max, knjiznica)) N_min = N_max N_max = N_max + N matrika.append(moc) if (moc < 5.0): roka.sendData("120,120,120,120,120", serial) print("relax") elif moc < 30.0: roka.sendData("60,60,60,60,60", serial) print("medium") else: roka.sendData("40,30,30,30,30", serial) print("contracted")
def plotGraphFromTxt(Fs, low, high, Q, N_min, N_max): # Sample rate and desired cutoff frequencies (in Hz). fs = Fs lowcut = low highcut = high f0 = 50.0 # Frequency to be removed from signal (Hz) Q = Q # Quality factor w0 = f0 / (Fs / 2) # Normalized Frequency # Design notch filter b, a = signal.iirnotch(w0, Q) zi = signal.lfilter_zi(b, a) #Import data podatki, knjiznica = bci.importData("txt") podatki = knjiznica["EXG Channel 0"] podatki = podatki[N_min:N_max] #print(podatki) #Initialize plot diagram #plt.xkcd() fig, (ax_orig, ax_fft, ax_fft_bandpassFilters, ax_fft_filtfilt) = plt.subplots(4, 1) N = podatki.size T = 1.0 / Fs x = np.linspace( 0.0, N, N ) #preposto razporedim vse vzorce po času (kar je prb 48 min + 60s + 200Hz = 576000, kar je skor realna številka ki je 576178 ax_orig.plot(x, podatki) ax_orig.set_title('Original Signal') y = fft(podatki) x = np.linspace(0.0, 100, N // 2) ax_fft.plot(x, 2.0 / N * np.abs(y[0:N // 2])) ax_fft.set_title('FFT Signal (not filtered)') ax_fft.grid() ax_fft.set_xlim([-5, 60]) xn = podatki #lahko bi isto naredil sfp.ifft(y) pa bi bil na istem #Bandpass filter, da se znebiš motenj pri nizkih in visokih frekvencah y = fft(butter_bandpass_filter(sfp.ifft(y), lowcut, highcut, fs, order=6)) x = np.linspace(0.0, 100, N // 2) ax_fft_bandpassFilters.plot(x, 2.0 / N * np.abs(y[0:N // 2])) ax_fft_bandpassFilters.set_title('FFT Signal bandpass filter') ax_fft_bandpassFilters.grid() ax_fft_bandpassFilters.set_xlim([-5, 60]) #Notch filter da se znebiš 50Hz motnje (ki jo dobiš iz omrežne frekvence/napetosti/elektrike) y = fft(signal.filtfilt(b, a, xn)) y = fft(butter_bandpass_filter(sfp.ifft(y), lowcut, highcut, fs, order=6)) x = np.linspace(0.0, 100, N // 2) ax_fft_filtfilt.plot(x, 2.0 / N * np.abs(y[0:N // 2])) ax_fft_filtfilt.set_title('FFT Signal w/ notch and bandpass filter') ax_fft_filtfilt.grid() ax_fft_filtfilt.set_xlim([-5, 60]) y_fft = y y = sfp.ifft( y) #da iz frekvenčnega prostora prideš nazaj v časovni prostor f, Pxx = signal.periodogram(y, fs=fs, return_onesided=False) #print("f = ",f) #print("Pxx = ",Pxx) #print(len(f)) #plt.xkcd() plt.figure(num=2) #plt.xlim([0, 50]) plt.plot(f, Pxx) #močnostni diagram plt.xlabel('frequency [Hz]') plt.ylabel('PSD [V**2/Hz]') plt.show() return y, f, Pxx