def plot_thread(): while True: plt.figure(0) plt.clf() idx, = np.where(fps.positions[0, :] > 0) pos = fps.positions[:, idx] plt.plot(pos[0, :], pos[1, :], label='Axis 1') # plt.plot(pos[1, :], label='Axis 2') # plt.plot(pos[2, :], label='Axis 3') plt.xlim(pos[0, 0], pos[0, -1]) avg_x = running_mean(pos[1, :], 100) plt.plot(pos[0, :len(avg_x)], avg_x, label='Axis 1 (100pts)') peak_peak = 2. * (np.sum(np.abs(pos[1, :] - avg_x)) / len(avg_x)) ts = 1000.0 * np.average(np.diff(pos[0, :])) plt.title('estimated sample time: {0:.2f} ms\n' 'peak-peak {1:.1f}nm'.format(ts, peak_peak * 1000.0) ) np.save("interf_data.np", pos) if 0: plt.figure(1) plt.clf() fftplot(pos, [1], [2], column_names={1: 'x', 2: 'y'}, scale=1000.0, ) plt.title('FFT [nm] vs [Hz]') plt.pause(0.5)
def plot_thread(): while True: plt.figure(0) plt.clf() idx, = np.where(fps.positions[0, :] > 0) pos = fps.positions[:, idx] plt.plot(pos[0, :], pos[1, :], label='Axis 1') # plt.plot(pos[1, :], label='Axis 2') # plt.plot(pos[2, :], label='Axis 3') plt.xlim(pos[0, 0], pos[0, -1]) avg_x = running_mean(pos[1, :], 100) plt.plot(pos[0, :len(avg_x)], avg_x, label='Axis 1 (100pts)') peak_peak = 2. * (np.sum(np.abs(pos[1, :] - avg_x)) / len(avg_x)) ts = 1000.0 * np.average(np.diff(pos[0, :])) plt.title('estimated sample time: {0:.2f} ms\n' 'peak-peak {1:.1f}nm'.format(ts, peak_peak * 1000.0)) np.save("interf_data.np", pos) if 0: plt.figure(1) plt.clf() fftplot( pos, [1], [2], column_names={ 1: 'x', 2: 'y' }, scale=1000.0, ) plt.title('FFT [nm] vs [Hz]') plt.pause(0.5)
def plot_loop(dev): # t0 = time.time() # dev.monitor(sample_rate=0.6554, wait_for=2048) dev.monitor(sample_rate=2. * 0.3277, wait_timestamp=0.1) # t1 = time.time() dev.stop() # print(pos) # return data = dev.position_data # for i in range(num_pos): # print('%-20s\t%f\t%f\t%f' # '' % (dev._timestamps[i], dev._positions[0][i], # dev._positions[1][i], dev._positions[2][i],) # ) plt.figure(1) plt.clf() col_names = {1: 'x', 2: 'y', 3: 'z'} colors = 'bgk' ax1, ax2, freqs, spectra = fftplot(data, [1, 2, 3], column_names=col_names, left_label='Amplitude [nm]', scale=1.0, left_colors=colors) plt.xlim(0, 400) plt.ylim(0, 5.0) plt.draw() title = ['Sample rate: %f ms [data points=%d]' '' % (dev.sample_rate, len(data[0, :])) ] for i in range(1, 4): max_i = np.argmax(spectra[:, i]) max_freq = freqs[max_i] nm = spectra[max_i, i] color = colors[i - 1] # title.append('%s[%f]=%f nm' % (col_names[i], max_freq, nm)) # plt.axvline(max_freq) plt.text(max_freq + 1, 0.9 * nm, '%.1f Hz' % max_freq, color=color) # plt.text(max_freq + 1, 0.75 * nm, '%.2f nm' % nm, color=color) # plt.text(max_freq, 0, '%.1f Hz' % max_freq, color=color) plt.title('\n'.join(title)) plt.legend(loc='best') plt.savefig('fft.png') plt.figure(2) plt.clf() ts = data[0, :] print('ts=%d filt=%d' % (len(ts), len(dev._filtered[0]))) plt.plot(ts, dev._filtered[0]) plt.plot(ts, data[1, :], alpha=0.1) plt.pause(0.1) plt.show() np.save('test', data) for i in range(3): print('average position (ax=%d)' % i, np.average(dev._positions[i])) # print('total positions in %g seconds: %d' % (t1 - t0, len(data[0, :]))) # print('sample rate %f (%d)' % (dev.sample_rate, dev._sample_rate)) dev._positions = [[] for i in range(3)]