Skip to content

Commit

Permalink
Fix AWG frequency, plot data, and store
Browse files Browse the repository at this point in the history
  • Loading branch information
ange1a-j14 committed Jul 23, 2024
1 parent d6053cf commit ed58376
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 44 deletions.
2 changes: 1 addition & 1 deletion acquire.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
# function for Data Acquisition
data = rp_s.acq_data(1, convert=True)

plt.plot(10*data)
plt.plot(data)
plt.ylabel('Amplitude [V]')
plt.xlabel('Samples')
plt.show()
100 changes: 62 additions & 38 deletions acquire_automatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,39 @@
import matplotlib.pyplot as plt
import redpitaya_scpi as scpi
import numpy as np
from scipy.fftpack import fft
import math
import util
from datetime import datetime
import csv

IP = 'rp-f0c04a.local'
rp_s = scpi.scpi(IP)
print('Connected to ' + IP)

# Set up waveform
wave_form = "ARBITRARY"
freq = 100 # good range 10-200Hz
ampl = 0.3 # good range 0-0.6V
store_data = False # whether or not to save data to CSV

##### Create Waveform #####

N = 16384 # Number of samples in buffer
decimation = 85
smpl_rate = 125e6//decimation
SMPL_RATE_DEC1 = 125e6 # sample rate for decimation=1 in Samples/s (Hz)
decimation = 8192
smpl_rate = SMPL_RATE_DEC1//decimation
burst_time = N / smpl_rate

wave_form = 'ARBITRARY'
freq = 1 / burst_time
ampl = 0.3 # good range 0-0.6V

# t, y from exampled RP arbitrary wavegen:
# t = np.linspace(0, 1, N)*2*math.pi
# y = np.sin(t) + 1/3*np.sin(3*t) # same overall period as regular sin wave
t, y = util.bounded_frequency_waveform(20, 1000, length=N, sample_rate=smpl_rate)
t, y = util.bounded_frequency_waveform(50, 55, length=N, sample_rate=smpl_rate)
y = util.linear_convert(y) # convert range of waveform to [-1, 1] to properly set ampl
plt.plot(t, y)
plt.show()

# Reset Generation and Acquisition
##### Reset Generation and Acquisition ######
rp_s.tx_txt('GEN:RST')
rp_s.tx_txt('ACQ:RST')

Expand All @@ -45,53 +53,69 @@
##### Acqusition #####
# Function for configuring Acquisition
rp_s.acq_set(dec=decimation, trig_delay=0)
# print(rp_s.get_settings())
rp_s.tx_txt('ACQ:START')
time.sleep(1)
rp_s.tx_txt('ACQ:TRig NOW')
# print(rp_s.get_settings())
time.sleep(1)

# Wait for trigger
while 1:
rp_s.tx_txt('ACQ:TRig:STAT?') # Get Trigger Status
print('got status')
if rp_s.rx_txt() == 'TD': # Triggered?
break

print('triggered')
## ! OS 2.00 or higher only ! ##
while 1:
rp_s.tx_txt('ACQ:TRig:FILL?')
if rp_s.rx_txt() == '1':
break

# Read data and plot
# function for Data Acquisition
print(rp_s.get_settings())
##### Analaysis #####
# Read data and plot function for Data Acquisition
pd_data = rp_s.acq_data(chan=1, convert=True) # Volts
speaker_data = rp_s.acq_data(chan=2, convert=True) # Volts
print("data shape:", np.array(pd_data).shape)
print(rp_s.get_settings())

plt.plot(speaker_data, color="black", label="Speaker")
plt.plot(pd_data, color="blue", label="PD")
plt.legend()
plt.ylabel('Amplitude [V]')
plt.xlabel('Samples')
displacement_data, converted_signal, freq = util.displacement_waveform(np.array(speaker_data), smpl_rate, ampl)
y_displ, _, _ = util.displacement_waveform(ampl*y, smpl_rate, ampl)

fig, ax = plt.subplots(nrows=3)

time_data = np.linspace(0, N-1, num=N) / smpl_rate
ax[0].plot(time_data, pd_data, color='blue', label='PD')
ax[0].plot(time_data, speaker_data, color='black', label='Speaker')
ax[0].plot(time_data, ampl*y, label='Original signal')
ax[0].legend()
ax[0].set_ylabel('Amplitude (V)')
ax[0].set_xlabel('Time (s)')

ax[1].plot(freq, np.abs(fft(speaker_data)), color='black', label='Speaker')
ax[1].plot(freq, np.abs(converted_signal), color='green', label='Displacement')
ax[1].loglog()
ax[1].set_xlabel('Frequency (Hz)')
ax[1].set_ylabel('$|\^{V}|$')
ax[1].legend()

ax[2].plot(time_data, displacement_data, color='black', label='Speaker')
ax[2].plot(time_data, y_displ, label='Original signal')
ax[2].set_ylabel('Expected Displacement (Microns)')
ax[2].set_xlabel('Time (s)')
ax[2].legend()
plt.tight_layout()
plt.show()

# Store data in txt file
path = "/Users/angelajia/Code/College/SMI/data/"
filename = f"{datetime.now()}.txt"
file_path = os.path.join(path, filename)

with open(file_path, 'x') as f:
# f.write(np.array2string(t, threshold=N+1))
# f.write("\n" + np.array2string(y, threshold=N+1))
f.write("\n")
for x in pd_data:
f.write(str(x))
f.write("\n")
f.write("STARTING SPEAKER DATA\n")
for y in speaker_data:
f.write(str(y))
f.write("\n")
if store_data:
# Store data in csv file
path = "/Users/angelajia/Code/College/SMI/data/"
filename = f"{datetime.now()}.csv"
file_path = os.path.join(path, filename)

with open(file_path, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Time (s)', 'Speaker (V)', 'Speaker (Microns)', 'PD (V)'])
all_data = np.vstack((time_data, speaker_data, displacement_data, pd_data)).T
for row in all_data:
writer.writerow(row)

##### Reset when closing program #####
rp_s.tx_txt('GEN:RST')
rp_s.tx_txt('ACQ:RST')
16 changes: 11 additions & 5 deletions acquire_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@

wave_form = "SINE"
freq = 100
ampl = 0.5
ampl = 0.1

# Reset Generation and Acquisition
rp_s.tx_txt('GEN:RST')
rp_s.tx_txt('ACQ:RST')

##### Generation #####
# Function for configuring Source
rp_s.sour_set(1, wave_form, ampl, freq, burst=False)
rp_s.sour_set(1, wave_form, ampl, freq)

# Enable output
rp_s.tx_txt('OUTPUT1:STATE ON')
rp_s.tx_txt('SOUR1:TRig:INT')

##### Acqusition #####
# Function for configuring Acquisition
rp_s.acq_set(dec=32, trig_delay=0)
rp_s.acq_set(dec=128, trig_delay=0)
rp_s.tx_txt('ACQ:START')
time.sleep(1)
rp_s.tx_txt('ACQ:TRig AWG_PE')
rp_s.tx_txt('ACQ:TRig NOW')
time.sleep(1)

# Wait for trigger
Expand All @@ -49,8 +49,14 @@
# Read data and plot
# function for Data Acquisition
data = rp_s.acq_data(chan=1, convert=True)
speaker_data = rp_s.acq_data(chan=2, convert=True)

plt.plot(data)
plt.plot(speaker_data)
plt.ylabel('Amplitude [V]')
plt.xlabel('Samples')
plt.show()
plt.show()

##### Reset when closing program #####
rp_s.tx_txt('GEN:RST')
rp_s.tx_txt('ACQ:RST')

0 comments on commit ed58376

Please sign in to comment.