0. 实验准备
泰克 AWG70001
一台电脑
一根网线
使用网线连接 AWG70001 和电脑,并且配置 IP 在同一网段下
1. 环境要求
vxi11
numpy
struct
matplotlib
没有的库可以使用下面的命令安装
pip install vxi11
pip install numpy
pip install struct
pip install matplotlib
2.Python代码
首先导入库
import numpy as np
from matplotlib import pyplot as plt
import vxi11
import struct
from time import sleep
2.1 打开连接
ip = "192.168.0.1" # 这里设置为 AWG的 IP,AWG 自带 Windows,可以直接查看 IP 地址
session = vxi11.Instrument('TCPIP::{}::INSTR'.format(ip))
session.timeout = 500
session.clear()
idn_str = session.ask("*idn?")
print(idn_str)
能收到类似下面的回复说明连接成功
TEKTRONIX,AWG70001A,B030508,FV:5.3.0128.0
2.2 设置采样率
使用以下的命令设置采样率
sample_rate = 10e9 # 采样率为 10Gs/s
session.write( "CLOCK:SRATE {:e}".format(sample_rate))
2.3 生成并发送自定义波形
下面的代码生成了一个起始频率为 5G,终止频率为 5.1G ,Chirp 时长为 20us 的 FMCW 波形,并且发送给 AWG
# 参数设置
f_start = 5e9 # 起始频率(Hz)
f_end = 5.1e9 # 终止频率(Hz)
T_chirp = 20e-6 # Chirp时间(秒)
Fs = 1e10 # 采样率(Hz)
# 生成时间轴
t = np.arange(0, T_chirp, 1/Fs)
# 生成线性调频信号
slope = (f_end - f_start) / T_chirp
chirp_signal = np.cos(2 * np.pi * (f_start * t + (1/2) * slope * t**2))
trace = 1
idle_val = 0
yscale = 1
xscale = 1
delay = 0e-9
sample_rate = Fs
invert = 0
period = T_chirp
waveformName = "ExtWaveformCh{:d}".format(trace)
xdata = np.array(t)
ydata = np.array(chirp_signal)
session = vxi11.Instrument('TCPIP::{}::INSTR'.format(ip))
session.timeout = 500
session.clear()
idn_str = session.ask("*idn?")
print(idn_str)
local_objects["session"] = session
print("STOP!")
session.write("AWGControl:STOP")
MAX_MEM_SIZE = 262144 # this might not actually be true for the TEK device
MIN_SAMPLE_LEN = 2400
mem_size = MAX_MEM_SIZE
session.write( "CLOCK:SRATE {:e}".format(sample_rate))
print("准备向通道 {:d}发送数据".format(trace))
xdata = xdata*xscale + delay
width = xdata[-1]
ydata = ydata*yscale
plt.figure()
plt.plot(xdata,ydata)
plt.show()
if(invert):
idle_val = -idle_val
ydata = -ydata
n = int(len(xdata))
sample_len = 0
if(period == 0):
sample_len = np.max([MIN_SAMPLE_LEN,n])
else:
sample_len = int(period * sample_rate)
dataList = idle_val*np.ones(sample_len)
n_ = np.min([n,sample_len])
dataList[0:n_] = ydata[0:n_]
#send data
data = bytearray()
waveform_length = sample_len
print("波形长度(样本长度*个数): {:d}".format(waveform_length))
print("样本长度(总时间*采样率): {:d}".format(sample_len))
for i in range(sample_len):
value = dataList[i]
data += bytearray(struct.pack("f", value))
commandString = "WLIST:WAVEFORM:DATA \"{}\",0,{},#{}{}".format(waveformName, waveform_length, len(str(4*waveform_length)), str(4*waveform_length))# + datastring
print(commandString)
len(data)
# Open socket, create waveform, send data, read back, start playing waveform and close socket
session.write("WLIST:WAVEFORM:DELETE \"{}\"".format(waveformName))
session.write("WLIST:WAVEFORM:NEW \"{}\" ,{}".format(waveformName, waveform_length))
session.write_raw( str.encode(commandString) + data)
session.write("SOURCE{:d}:CASSET:WAVEFORM \"{}\"".format(trace,waveformName))
2.4 设置 Run mode
使用以下的命令设置 Run mode
# 可选参数: CONTinuous TRIGgered GATed SEQuence
session.write("AWGCONTROL:RMODE TRIGgered") # 设置为触发模式