Compare commits

...

5 Commits

  1. 104
      main.py

@ -7,6 +7,14 @@ from matplotlib import pyplot as plt
import gex
import time
# Two presets defined for demoing the plotter with a high-pass and low-pass RC filter
# made of 1 kOhm and 100 nF
#demo = 'HP'
demo = 'LP'
#demo = 'LP2'
#demo = 'LP3' # LT1112 sallenkey at 1 kHz
class ADG:
def __init__(self, client:gex.Client):
self.client = client
@ -40,7 +48,8 @@ class ADG:
if freq is not None:
self.set_frequency(freq)
with gex.Client(gex.TrxRawUSB()) as client:
#with gex.Client(gex.TrxRawUSB()) as client:
with gex.Client(gex.TrxSerialThread('/dev/ttyACM0')) as client:
# ===============================================
# Delay between adjusting input and starting the measurement.
@ -51,43 +60,60 @@ with gex.Client(gex.TrxRawUSB()) as client:
max_allowed_shift_db = 5
# db shift compensation (spread through the frequency sweep to adjust for different slopes)
allowed_shift_compensation = -4.5
if False:
# highpass filter example (corner 340 Hz)
settling_time_s = (4700*100e-9)*10
max_allowed_shift_db = 5
allowed_shift_compensation = -4.5
# Frequency sweep parameters
f_0 = 5
f_1 = 6000
f_step = 5
f_step_begin = 5
f_step_end = 200
if True:
if demo == 'HP':
# highpass filter example (corner 340 Hz)
settling_time_s = (4700*100e-9)*10
max_allowed_shift_db = 5.6
allowed_shift_compensation = -5
if demo == 'LP':
# lowpass filter example (corner 340 Hz)
settling_time_s = (4700*100e-9)*10
max_allowed_shift_db = .5
allowed_shift_compensation = 2
if demo == 'LP2':
# lowpass filter example (corner 340 Hz)
settling_time_s = (4700*100e-9)*10
max_allowed_shift_db = 1
allowed_shift_compensation = 1.2
max_allowed_shift_db = .5
allowed_shift_compensation = 5
f_1 = 4500
f_step_end = 100
if demo == 'LP3':
# lowpass filter example (corner 340 Hz)
settling_time_s = 0.05
max_allowed_shift_db = .5
allowed_shift_compensation = 5
f_1 = 10000
f_step_end = 250
# Frequency sweep parameters
f_0 = 5
f_1 = 5000
f_step = 15
# Retry on failure
retry_count = 5
retry_delay_s = settling_time_s*10
retry_delay_s = settling_time_s*2
# Initial sample granularity
samples_per_period = 60
capture_periods = 10
# Parameters for automatic params adjustment
max_allowed_sample_rate = 36000
max_allowed_nr_periods = 80
min_samples_per_period = 4
max_allowed_sample_rate = 65000
max_allowed_nr_periods = 100
min_samples_per_period = 16
# ===============================================
allowed_shift_compensation /= (f_1 - f_0) / f_step
#allowed_shift_compensation /= (f_1 - f_0) / f_step
adc = gex.ADC(client, 'adc')
@ -98,16 +124,25 @@ with gex.Client(gex.TrxRawUSB()) as client:
table = []
last_db = None
for f in range(f_0, f_1, f_step):
f = f_0
first = True
begin_allowedshift = max_allowed_shift_db
while f <= f_1:
if not first:
f_step = round(f_step_begin + ((f - f_0) / (f_1 - f_0)) * (f_step_end - f_step_begin))
f += f_step
first = False
#dac.set_frequency(1, f)
gen.set_frequency(f)
gen.set_frequency(f)
max_allowed_shift_db += allowed_shift_compensation
max_allowed_shift_db = begin_allowedshift + allowed_shift_compensation * ((f - f_0) / (f_1 - f_0))
# Adjust measurement parameters
while True:
desiredf = f*samples_per_period
if desiredf > max_allowed_sample_rate:
desiredf = max_allowed_sample_rate
oldspp = samples_per_period
samples_per_period = math.ceil(samples_per_period * 0.9)
@ -187,17 +222,18 @@ with gex.Client(gex.TrxRawUSB()) as client:
gains = t[:, 1]
phases = t[:, 2]
plt.figure()
plt.ylabel('Gain (dB)')
plt.xlabel('Frequency (Hz)')
plt.semilogx(freqs, gains) # Bode magnitude plot
plt.grid()
plt.figure()
plt.ylabel('Phase (deg)')
plt.xlabel('Frequency (Hz)')
plt.semilogx(freqs, phases) # Bode phase plot
plt.grid()
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_ylabel('Gain (dB)')
ax1.semilogx(freqs, gains) # Bode magnitude plot
ax1.grid()
ax2 = fig.add_subplot(212)
ax2.set_ylabel('Phase (deg)')
ax2.set_xlabel('Frequency (Hz)')
ax2.semilogx(freqs, phases) # Bode phase plot
ax2.grid()
plt.show()

Loading…
Cancel
Save