Rigol DS2072 and python - 2 channels, 1 FFT and an X-Y plot on your PC - Page 1
Forum section: electronics / projects

author: poida_pie
author_rank: Regular Contributor
timestamp: July 30, 2013, 05:32:11 am
content: 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 XPsource:Code:import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationimport matplotlib.mlab as mlabimport sysimport fpformatfrom visa import *#init and declare some varstime = np.linspace(0,1,1400)timescale = timeoffset = tn = samprate = vn1 = vn2 = 1.0tunit=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 nowif (len(cdata) <> len(time)):cdata = timereturn cdatadef get_timebase():global timescale,timeoffset,time,sampratevi.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,tunitif (timescale < 1e-6):tn = timescale * 1e9tunit = "ns"elif (timescale < 1e-3):tn = timescale * 1e6tunit = "us"elif (timescale < 1):tn = timescale * 1e3tunit = "ms"else:tn = timescaletunit = "s"def get_vscales():global vscale1,vscale2,vn1, vn2, vunit1, vunit2vi.write(":CHANnel1:SCALe?")vscale1 = float(vi.read())vi.write(":CHANnel2:SCALe?")vscale2 = float(vi.read())if (vscale1 < 1):vn1 = vscale1 * 1e3vunit1 = "mV"else:vn1 = vscale1vunit1 = "V"if (vscale2 < 1):vn2 = vscale2 * 1e3vunit2 = "mV"else:vn2 = vscale2vunit2 = "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 Infovi = 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 functionwblackman = []for i in range(1024):t = (i - 512.0) / 512.0b = 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,line3ani = animation.FuncAnimation(fig, update, interval=100)plt.show()

author: brainwash
author_rank: Frequent Contributor
timestamp: July 30, 2013, 08:27:07 pm
content: http://www.righto.com/2013/07/rigol-oscilloscope-hacks-with-python.html