Jan-27-2022, 07:20 PM
I am sorry, code here:
import serial import struct import matplotlib.pyplot as plt import scipy.fft import numpy as np from scipy.fftpack import fft ser = serial.Serial(port='COM4', baudrate=2000000) SAMPLES=20000 s = struct.Struct('<' + str(SAMPLES) + 'f') T = 1.0 / 20000.0 unpacked_data1 = [] def serial_read(): ser.reset_input_buffer() ser.write("r".encode()) serial_data = ser.read(SAMPLES*4) unpacked_data = s.unpack(serial_data) unpacked_data1[1:20000] = unpacked_data[1:20000] ser.close serial_read() plt.plot(unpacked_data1[1000:3000]) plt.show() x = np.linspace(0.0, SAMPLES*T, SAMPLES) y = unpacked_data1 yf = scipy.fftpack.fft(y) xf = np.linspace(0.0, 1.0/(2.0*T), SAMPLES//2) fig, ax = plt.subplots() ax.plot(xf, 2.0/SAMPLES * np.abs(yf[:SAMPLES//2])) plt.show()