combine Rigol DS2072, DG1022 and python for a nice freq response graph - Page 1
Forum section: electronics / projects

author: poida_pie
author_rank: Regular Contributor
timestamp: August 12, 2013, 09:54:28 am
content: Not a lot of python code is needed to produce the attached.This is a sweep from 100 to 100,000 Hz at 5 Vpp sin wave into a RC low pass filter.(1.5nf 19.8K Ohms) You can see the -3 db point is about where it should be (approx 5,100 Hz)This is pretty simple stuff.See the below code. Runs under Win XP, Python 2.7, numpy, matplotlib, pyVisa modules are required.Code:import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationimport matplotlib.mlab as mlabimport sysimport fpformatfrom visa import *import time#init and declare some varsglobal vs,vppnsamps = 100# no. of freq samplesflow = 100# low freq of sweepfhigh = 1e5# high freq of sweepvsig = 5.0# signal outpout voltage, Vppvpp = 0.0vs = 0.0log10 = np.log(10)freq = np.logspace(np.log(flow)/log10, np.log(fhigh)/log10, nsamps, base=10.0)# freq sweep from 1e2 Hz to 1e5 Hz or whateverdata = np.linspace(0, 1.5, nsamps)#just declare and fill the Vpp data arrayvi = instrument("USB0::0x1AB1::0x04B0::DS2A152000988::INSTR")# my serial for DS2072, may need your serial to workvi.write("*IDN?")# not needed but it proves both machines are plugged in andprint vi.read()# connected via USB and switched on.# vi is the object that communicates to the DS2072# visg communicates to the DG1022 sig genvisg = instrument("USB0::0x09c4::0x0400::DG1D144303960::INSTR")# my serial for DG1022 (maybe need your serial)visg.write("*IDN?")print visg.read()# set the scope to be ch1, 20Mhz bw, 14k points acquire, no average, trigger on ch1 0 volts, etc.# this code assumes simple prior setup conditionsvs = vsig/5.0vi.write(":CHANnel1:SCALe " + str(vs))time.sleep(0.3)vi.write(":CHANnel1:OFFSet 0.0")time.sleep(0.3)vi.write(":TRIGger:MODE EDGE")time.sleep(0.3)vi.write(":TRIGger:EDGe:SOURce CHANnel1")time.sleep(0.3)vi.write(":TRIGger:EDGe:LEVel 0.0")time.sleep(0.3)vi.write(":TIMebase:MAIN:OFFSet 0.0")time.sleep(0.3)vi.write(":ACQuire:MDEPth 14000")time.sleep(0.3)vi.write(":CHANnel1:BWLimit 20M")time.sleep(0.3)vi.write(":ACQuire:TYPE NORMal")time.sleep(0.3)#init the sig genvisg.write("VOLT:UNIT VPP")time.sleep(0.1)visg.write("APPL:SIN 1000," + str(vsig) + ",0.0")# a sine wave, 0.0 v offset, vsig volts p/p, 1000 Hztime.sleep(0.1)visg.write("PHAS 0")time.sleep(0.1)visg.write("OUTP ON")time.sleep(0.1)#set ch1 on for the DS2072 and set a reasonable timebase calculated from the start freq of the sweepvi.write(":MEASure:SOURce CHAN1")time.sleep(0.3)tb = 0.1 / freqvi.write("TIMebase:MAIN:SCALe " + str(tb))time.sleep(0.3)# setup the graph systemfig = plt.figure()fig.set_tight_layout(True)ax = fig.add_subplot(111)# for each freq from flow to fhigh, set sig gen to output vsig volts p/p sin out chan1# set timebase to be reasonable, Ch1 V scale something reasonable# get volts p/p and put into the array data[]i=0for f in freq:# set DSO timebase to give about 1 wave per div sort of approximately nearlyif (f*tb > 0.5):tb = 0.1 / fvi.write("TIMebase:MAIN:SCALe " + str(tb))time.sleep(0.3)# set DG to this freqvisg.write("APPL:SIN " + str(f) + "," + str(vsig) + ",0.0")time.sleep(0.2)#get Vpp from DS2072vi.write(":MEASure:VPP?")time.sleep(0.2)r = vi.read()vpp = float(r)data = vpp/vsig# rescale ch1 if needed to make the best of signal to noise of DSOif (vpp < (vs*6.0) ):vs = vpp / 7.5vi.write(":CHANnel1:SCALe " + str(vs))time.sleep(0.3)i = i+1# release the sig gen from remote controlvisg.write("SYSTem:LOCal")#convert to db and plotd2 = 10.0 * np.log(data)line, = ax.plot(freq,d2)# add nice stuff to the graphax.set_ylim(-30.0,10.0)ax.set_xscale("log")ax.set_xlim(freq,freq[nsamps-1])ax.set_xlabel("Frequency")ax.set_ylabel("db (0db = " + str(vsig) + "V)")ax.grid();plt.show()

author: bingo600
author_rank: Super Contributor
timestamp: August 12, 2013, 08:46:11 pm
content: Nice example/Bingo