Electronics > Projects, Designs, and Technical Stuff
Rigol DS2072 and python - 2 channels, 1 FFT and an X-Y plot on your PC
(1/1)
poida_pie:
I am having a good time farting about with python and the Rigol.
See attached image. The FFT is hard coded to use channel 1. Arbitrary scales on all 3 graphs for now.
This updates about 3-4 times a second on my PC (2.4GHz core2 duo)
to do this you need:
python 2.7, matplotlib, pyvisa, and numpy. I am running this under Windows XP
source:
--- Code: ---import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.mlab as mlab
import sys
import fpformat
from visa import *
#init and declare some vars
time = np.linspace(0,1,1400)
timescale = timeoffset = tn = samprate = vn1 = vn2 = 1.0
tunit=vunit1=vunit2=" "
def get_channel(channel):
vi.write(":WAV:FORM BYTE")
vi.write(":WAV:SOURce CHANnel" + channel)
vi.write(":WAV:MODE NORMal")
vi.write(":WAV:DATA?")
ddata = vi.read()
cdata = np.frombuffer(ddata[12:], 'B')
# sometimes not enough data comes after WAV:DATA? command, so ignore it and copy
# exact number of any value bytes into data list - use time (1400 bytes)
# no error thrown in plot code now
if (len(cdata) <> len(time)):
cdata = time
return cdata
def get_timebase():
global timescale,timeoffset,time,samprate
vi.write(":ACQ:SRATE?")
samprate = vi.read()
vi.write(":TIMebase:MAIN:OFFSET?")
timeoffset = float(vi.read())
vi.write(":TIMebase:MAIN:SCALE?")
timescale = float(vi.read())
def get_timeunits():
global timescale,tn,tunit
if (timescale < 1e-6):
tn = timescale * 1e9
tunit = "ns"
elif (timescale < 1e-3):
tn = timescale * 1e6
tunit = "us"
elif (timescale < 1):
tn = timescale * 1e3
tunit = "ms"
else:
tn = timescale
tunit = "s"
def get_vscales():
global vscale1,vscale2,vn1, vn2, vunit1, vunit2
vi.write(":CHANnel1:SCALe?")
vscale1 = float(vi.read())
vi.write(":CHANnel2:SCALe?")
vscale2 = float(vi.read())
if (vscale1 < 1):
vn1 = vscale1 * 1e3
vunit1 = "mV"
else:
vn1 = vscale1
vunit1 = "V"
if (vscale2 < 1):
vn2 = vscale2 * 1e3
vunit2 = "mV"
else:
vn2 = vscale2
vunit2 = "V"
# use your serial to create the connect string here, not mine. My serial no. won't work for you
# replace the serial number with yours found by UTILITY/System Info
vi = instrument("USB0::0x1AB1::0x04B0::DS2A152000988::INSTR")
vi.write("*IDN?")
print vi.read()
data = get_channel("1")
data2 = get_channel("2")
get_timebase()
time = np.linspace(timeoffset - 7 * timescale, timeoffset + 7 * timescale, num=len(data))
fftpow = np.linspace(0,1,513)
fig = plt.figure()
ax = fig.add_subplot(221)
ax3 = fig.add_subplot(222)
ax4 = fig.add_subplot(212)
ax2 = ax.twiny()
line, = ax.plot(data,color='y')
line2, = ax2.plot(data,color='b')
line3, = ax3.plot(data,data2,color='r')
line4, = ax4.plot(fftpow,fftpow,color='y')
plt.ylim(0,255)
plt.xlim(0,1400)
plt.axis('off')
plt.vlines(np.arange(0,1400,100),0,255,color='0.3',linestyles='dotted')
plt.hlines(np.arange(27,250,25),0,1400,color='0.3',linestyles='dotted')
ax.set_yticklabels("",visible=False)
ax.set_xticklabels("",visible=False)
ax3.set_yticklabels("",visible=False)
ax3.set_xticklabels("",visible=False)
ax3.set_xlim(-96,352)
ax3.set_ylim(0,255)
ax4.set_xlim(0,1)
ax4.set_ylim(-5,5)
#use the Blackman windowing function
wblackman = []
for i in range(1024):
t = (i - 512.0) / 512.0
b = 0.42 + 0.5 * np.cos(np.pi * t) + 0.08 * np.cos(2.0 * np.pi * t)
wblackman.append(b)
def update(data):
data = get_channel("1")
data2 = get_channel("2")
get_timebase()
get_timeunits()
get_vscales()
line.set_ydata(data)
line2.set_ydata(data2)
line3.set_xdata(data)
line3.set_ydata(data2)
plt.title("Ch1 " + str(vn1) + " " + vunit1 + " Ch2 " + str(vn2) + " " + vunit2 + " "+ str(tn) + " " + tunit)
wdata = wblackman * data[0:1024]
fftres = np.fft.rfft(wdata,1024)
fftpow = np.log(np.absolute(fftres)/1024.0)/np.log(10.0)
line4.set_ydata(fftpow)
return line,line2,line3
ani = animation.FuncAnimation(fig, update, interval=100)
plt.show()
--- End code ---
brainwash:
http://www.righto.com/2013/07/rigol-oscilloscope-hacks-with-python.html
Navigation
[0] Message Index
Go to full version