Compare commits

..

5 Commits

  1. 24
      UNITS.INI
  2. 121
      main.py

@ -2,9 +2,6 @@
## GEX v1.0.0 on STM32F072-HUB ## GEX v1.0.0 on STM32F072-HUB
## built Jun 15 2018 at 13:45:28 ## built Jun 15 2018 at 13:45:28
# Overwrite this file to change settings.
# Press the LOCK button to save them to Flash.
[UNITS] [UNITS]
# Create units by adding their names next to a type (e.g. DO=A,B), # Create units by adding their names next to a type (e.g. DO=A,B),
# remove the same way. Reload to update the unit sections below. # remove the same way. Reload to update the unit sections below.
@ -34,7 +31,7 @@ TOUCH=
# Simple PWM output # Simple PWM output
PWMDIM= PWMDIM=
# Two-channel analog output with waveforms # Two-channel analog output with waveforms
DAC=dac DAC=
[SPI:spi@2] [SPI:spi@2]
# Peripheral number (SPIx) # Peripheral number (SPIx)
@ -58,7 +55,7 @@ first-bit=MSB
# SS port name # SS port name
port=A port=A
# SS pins (comma separated, supports ranges) # SS pins (comma separated, supports ranges)
pins=3 pins=4
[ADC:adc@4] [ADC:adc@4]
# Enabled channels, comma separated # Enabled channels, comma separated
@ -76,7 +73,7 @@ frequency=1000
# - defines the maximum pre-trigger size (divide by # of channels) # - defines the maximum pre-trigger size (divide by # of channels)
# - captured data is sent in half-buffer chunks # - captured data is sent in half-buffer chunks
# - buffer overrun aborts the data capture # - buffer overrun aborts the data capture
buffer_size=800 buffer_size=512
# Enable continuous sampling with averaging # Enable continuous sampling with averaging
# Caution: This can cause DAC output glitches # Caution: This can cause DAC output glitches
@ -84,17 +81,4 @@ averaging=N
# Exponential averaging coefficient (permil, range 0-1000 ~ 0.000-1.000) # Exponential averaging coefficient (permil, range 0-1000 ~ 0.000-1.000)
# - used formula: y[t]=(1-k)*y[t-1]+k*u[t] # - used formula: y[t]=(1-k)*y[t-1]+k*u[t]
# - not available when a capture is running # - not available when a capture is running
avg_factor=80 avg_factor=800
[DAC:dac@1]
# Enabled channels (1:A4, 2:A5)
ch1_enable=Y
ch2_enable=N
# Enable output buffer
ch1_buff=Y
ch2_buff=Y
# Superimposed noise type (NONE,WHITE,TRIANGLE) and nbr. of bits (1-12)
ch1_noise=NONE
ch1_noise-level=3
ch2_noise=NONE
ch2_noise-level=3

@ -7,7 +7,13 @@ from matplotlib import pyplot as plt
import gex import gex
import time import time
use_native_dac = True # 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: class ADG:
def __init__(self, client:gex.Client): def __init__(self, client:gex.Client):
@ -42,7 +48,8 @@ class ADG:
if freq is not None: if freq is not None:
self.set_frequency(freq) 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. # Delay between adjusting input and starting the measurement.
@ -54,77 +61,88 @@ with gex.Client(gex.TrxRawUSB()) as client:
# db shift compensation (spread through the frequency sweep to adjust for different slopes) # db shift compensation (spread through the frequency sweep to adjust for different slopes)
allowed_shift_compensation = -4.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 False: if demo == 'HP':
# highpass filter example (corner 340 Hz) # highpass filter example (corner 340 Hz)
settling_time_s = (4700*100e-9)*10 settling_time_s = (4700*100e-9)*10
max_allowed_shift_db = 5 max_allowed_shift_db = 5.6
allowed_shift_compensation = -4.5 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 True: if demo == 'LP2':
# lowpass filter example (corner 340 Hz) # lowpass filter example (corner 340 Hz)
settling_time_s = (4700*100e-9)*10 settling_time_s = (4700*100e-9)*10
max_allowed_shift_db = 1 max_allowed_shift_db = .5
allowed_shift_compensation = 1.2 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
f_0 = 10
f_1 = 5000
f_step = 50
# Retry on failure # Retry on failure
retry_count = 5 retry_count = 5
retry_delay_s = settling_time_s*10 retry_delay_s = settling_time_s*2
# Initial sample granularity # Initial sample granularity
samples_per_period = 60 samples_per_period = 60
capture_periods = 10 capture_periods = 10
# Parameters for automatic params adjustment # Parameters for automatic params adjustment
max_allowed_sample_rate = 36000 max_allowed_sample_rate = 65000
max_allowed_nr_periods = 80 max_allowed_nr_periods = 100
min_samples_per_period = 4 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') adc = gex.ADC(client, 'adc')
dac = None gen = ADG(client)
gen = None gen.initialize()
if use_native_dac:
print('Using native GEX DAC')
dac = gex.DAC(client, 'dac')
dac.waveform(1, 'SINE')
else:
print('Using AD9833 via SPI')
gen = ADG(client)
gen.initialize()
table = [] table = []
last_db = None 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
if use_native_dac: #dac.set_frequency(1, f)
dac.set_frequency(1, f) gen.set_frequency(f)
else:
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 # Adjust measurement parameters
while True: while True:
desiredf = f*samples_per_period desiredf = f*samples_per_period
if desiredf > max_allowed_sample_rate: if desiredf > max_allowed_sample_rate:
desiredf = max_allowed_sample_rate
oldspp = samples_per_period oldspp = samples_per_period
samples_per_period = math.ceil(samples_per_period * 0.9) samples_per_period = math.ceil(samples_per_period * 0.9)
@ -165,7 +183,6 @@ with gex.Client(gex.TrxRawUSB()) as client:
y1 = np.max(t[:,0]) - np.min(t[:,0]) y1 = np.max(t[:,0]) - np.min(t[:,0])
y2 = np.max(t[:,1]) - np.min(t[:,1]) y2 = np.max(t[:,1]) - np.min(t[:,1])
print("\x1b[90mU %f, Y %f\x1b[0m" % (y1, y2))
gain_raw = y2/y1 gain_raw = y2/y1
gain_db = 20*math.log10(gain_raw) gain_db = 20*math.log10(gain_raw)
@ -197,10 +214,7 @@ with gex.Client(gex.TrxRawUSB()) as client:
if not suc: if not suc:
last_db = last_db_in_fail last_db = last_db_in_fail
if use_native_dac: gen.wfm_dc()
dac.dc(1, 2000)
else:
gen.wfm_dc()
t = np.reshape(np.array(table), [int(len(table) / 3), 3]) t = np.reshape(np.array(table), [int(len(table) / 3), 3])
@ -208,17 +222,18 @@ with gex.Client(gex.TrxRawUSB()) as client:
gains = t[:, 1] gains = t[:, 1]
phases = t[:, 2] phases = t[:, 2]
plt.figure() fig = plt.figure()
plt.ylabel('Gain (dB)') ax1 = fig.add_subplot(211)
plt.xlabel('Frequency (Hz)') ax1.set_ylabel('Gain (dB)')
plt.semilogx(freqs, gains) # Bode magnitude plot ax1.semilogx(freqs, gains) # Bode magnitude plot
plt.grid() ax1.grid()
plt.figure() ax2 = fig.add_subplot(212)
plt.ylabel('Phase (deg)') ax2.set_ylabel('Phase (deg)')
plt.xlabel('Frequency (Hz)') ax2.set_xlabel('Frequency (Hz)')
plt.semilogx(freqs, phases) # Bode phase plot ax2.semilogx(freqs, phases) # Bode phase plot
plt.grid() ax2.grid()
plt.show() plt.show()

Loading…
Cancel
Save