from spectralc1 import spectral import h5py with h5py.File('C:/Users/Olivier/Downloads/D4.mat', 'r') as file: y = list(file['D']) fs = 3200 y = np.array(y).T plt.figure() plt.plot(y) #plt.plot(t,y) #plt.xlabel('t(s)') sp0 = spectral(fs, fs, overlap=0.75, win='hann', averaging='lin', nAverage=50) #sp1 = spec.spectral(fs, fs*10, overlap=0.75, win='blackman', averaging='lin',nAverage=10) #sp2 = spec.spectral(fs, fs*10, overlap=0.75, win='flattop', averaging='lin',nAverage=10) #sp3 = spec.spectral(fs, fs*10, overlap=0.75, win='boxcar', averaging='lin',nAverage=10) Y0 = [] f = [] i = 0 def cb(): global Y0, f, i i += 1 Y0, f = sp0.get() print(np.max(Y0[10:]))
cnt = 0 buffer = collections.deque() #read/outgoing data buffer buffer2 = collections.deque() #spectrum outgoing data buffer df = 1 #FFT resolution (s) fftlines = fs / df #fft_block_size = fftlines*10 #data block for FFT calculation (set larger than FFT lines to allow averaging) ntracks = 3 spectrum = [] for i in range(ntracks): spectrum.append( spectral(fs, fftlines, overlap=0.75, win='hann', averaging='lin', nAverage=10)) def read_from_port(s, appendData, appendData2): syncB(s, s.read(l_fmt * 2)) #sync data with 2 samples while True: if s.inWaiting() > l_fmt * l_packet: read_byte = s.read(l_fmt * l_packet) #read 0.1 s of data if read_byte is not None: appendData(read_byte) #append element to right of buffer appendData2(read_byte) time.sleep(0.09) #reduce CPU load
l_fmt = struct.calcsize(fmt) l_fmt2 = struct.calcsize(fmt2) stopped = False cnt = 0 buffer = collections.deque() #read/outgoing data buffer buffer2 = collections.deque() #spectrum outgoing data buffer df = 1 #FFT resolution (s) fftlines = fs/df ntracks = 3 spectrum = [] for i in range(ntracks): spectrum.append(spectral(fs, fftlines, overlap=0.75, win='hann', averaging='exp',nAverage=10)) #create hdf5 file if os.path.exists("file.h5"): shutil.copy2("file.h5","file_old.h5") os.remove("file.h5") file = h5py.File('file.h5') #timeseries: contains all tracks, raw int16 data (as downloaded) dsts = file.create_dataset('ts', (ntracks,0), maxshape=(ntracks, None), chunks=(ntracks, l_packet), dtype='i2', compression="lzf") dsts_cnt = 0 def read_from_port(s, appendData, appendData2): syncB(s, s.read(l_fmt*2)) #sync data with 2 samples while True: if s.inWaiting() > l_fmt*l_packet: read_byte = s.read(l_fmt*l_packet) #read 0.1 s of data
} } #open serial connection with Arduno try: ser = serial.Serial(port, baud, timeout=1) except serial.SerialException: ser.close() raise spectrum = [] for i in range(ntracks): spectrum.append( spectral(fs, fftlines, overlap=overlap, win='hann', averaging='exp', nAverage=10)) #create hdf5 file if os.path.exists("file.h5"): shutil.copy2("file.h5", "file_old.h5") os.remove("file.h5") file = h5py.File('file.h5') #timeseries: contains all tracks, raw int16 data (as downloaded) dsts = file.create_dataset('ts', (ntracks, 0), maxshape=(ntracks, None), chunks=(ntracks, l_packet), dtype='i2', compression="lzf") dsts_cnt = 0