added FFT using numpy
This commit is contained in:
parent
2429d5d1dd
commit
ad667bf2e5
50
test.py
50
test.py
|
@ -87,10 +87,13 @@ Query('WAVEFORM:FORMAT?')
|
||||||
[status, wfLen] = Query('WAVEFORM:STATUS?').split(',')
|
[status, wfLen] = Query('WAVEFORM:STATUS?').split(',')
|
||||||
if status == "IDLE" and int(wfLen) >= 0 :
|
if status == "IDLE" and int(wfLen) >= 0 :
|
||||||
print("---- get waveform data")
|
print("---- get waveform data")
|
||||||
|
start_time = time.time()
|
||||||
osc.write('WAVEFORM:DATA?')
|
osc.write('WAVEFORM:DATA?')
|
||||||
binary = osc.read_bytes(int(wfSize) +11 + 1) #
|
binary = osc.read_bytes(int(wfSize) +11 + 1) #
|
||||||
|
end_time = time.time()
|
||||||
# print(binary)
|
# print(binary)
|
||||||
|
duration = end_time - start_time
|
||||||
|
print("Duration for obtaining waveform:", duration, "seconds")
|
||||||
|
|
||||||
#---------- read data from internal memory, 1.12 million points, take like
|
#---------- read data from internal memory, 1.12 million points, take like
|
||||||
# Write('STOP')
|
# Write('STOP')
|
||||||
|
@ -133,5 +136,48 @@ header = binary[:11]
|
||||||
data_points = binary[11:]
|
data_points = binary[11:]
|
||||||
# Decode the 1-byte data points
|
# Decode the 1-byte data points
|
||||||
decoded_data = [byte for byte in data_points]
|
decoded_data = [byte for byte in data_points]
|
||||||
print("Decoded data:", decoded_data)
|
# print("Decoded data:", decoded_data)
|
||||||
|
|
||||||
|
|
||||||
|
#===================== FFT
|
||||||
|
import numpy as np
|
||||||
|
signal = np.array(decoded_data)
|
||||||
|
signal = signal[:-1]
|
||||||
|
|
||||||
|
timeStep = float(para[4])
|
||||||
|
sampling_rate = 1./timeStep # Sampling rate (Hz)
|
||||||
|
duration = wfSize * timeStep # Duration of signal (seconds)
|
||||||
|
num_samples = wfSize
|
||||||
|
timeList = np.linspace(0, duration, num_samples)
|
||||||
|
|
||||||
|
# Perform FFT
|
||||||
|
fft_result = np.fft.fft(signal)
|
||||||
|
frequencies = np.fft.fftfreq(num_samples, 1 / sampling_rate)
|
||||||
|
|
||||||
|
# Plot
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# Plot signal
|
||||||
|
plt.figure(figsize=(14, 6))
|
||||||
|
plt.subplot(1, 2, 1)
|
||||||
|
plt.plot(timeList*1e6, signal)
|
||||||
|
plt.title('Signal')
|
||||||
|
plt.xlabel('Time (us)')
|
||||||
|
plt.ylabel('Amplitude')
|
||||||
|
plt.grid(True)
|
||||||
|
|
||||||
|
# Plot FFT
|
||||||
|
plt.subplot(1, 2, 2)
|
||||||
|
# plt.plot(frequencies[:num_samples//2] / 1e6, np.abs(fft_result)[:num_samples//2])
|
||||||
|
plt.semilogy(frequencies[:num_samples//2] / 1e6, np.abs(fft_result)[:num_samples//2])
|
||||||
|
plt.title('FFT of Signal')
|
||||||
|
plt.xlabel('Frequency (MHz)')
|
||||||
|
plt.ylabel('Magnitude')
|
||||||
|
plt.grid(True)
|
||||||
|
|
||||||
|
plt.xlim(0, 100) # Set the X-range from 0 to 10 MHz
|
||||||
|
|
||||||
|
|
||||||
|
plt.tight_layout()
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user