Tektronix Technical Forums are maintained by community involvement. Feel free to post questions or respond to questions by other members. Should you require a time-sensitive answer, please contact your local Tektronix support center here.

AWG70k .mat file extractor

Questions in this forum area are community supported. Tektronix does not regularly monitor or guarantee responses in this area.
Post Reply

AWG70k .mat file extractor

Post by tekzilla » September 8th, 2014, 7:41 am

There are two bits of code here. The first generates a .mat file that can be directly imported to the AWG. The second imports that .mat file into Matlab, extracts the waveform data and sampling parameters, and sends that data directly to the AWG.

Code: Select all

%% Define Waveform Data
NumPoints = 2400;       %Waveform length
t = (0:1:NumPoints-1)';  %Define t vector
waveform = single(sin(2*pi*1/NumPoints*t)); %Create single sinewave

%% Save Waveform
Waveform_Name_1 = 'SINE';       %Name Waveform
Waveform_Data_1 = waveform;     %Assign waveform data
Waveform_Sampling_Rate_1 = 2.4e9;    %You can specify sample rate in S/s
Waveform_Amplitude_1 = 0.300;          %and amplitude in V
save('SingleCycleSine', '*_1', '-v7.3');    %Save all variables ending in _1 to .mat file

Code: Select all

% Date: 9/8/14
% Tested on a 64-bit Windows 7 computer
% using NI-VISA 5.4 and Matlab 2014a
% with Matlab Instrument Control Toolbox
% Tested on AWG70001A with Firmware 3.0.0366
% Program: .mat File Import and Transfer
% This program imports a .mat file into Matlab, extracts the data used
% by the AWG, and sends the waveform to the AWG. This program also sets
% the sample rate and amplitude of the AWG if specified in the .mat file.
% This program assumes that the variable naming convention listed in the 
% AWG70000 user manual on page 67 is followed and that the data type
% for Waveform_Data_1 is single. 
% http://www.tek.com/node/1002756-manual/awg70001a-and-awg70002a-1

%% Configure VISA resource and identify instrument
visa_brand = 'ni';                              %set VISA brand
visa_address = 'TCPIP::';    %send instrument ID from VISA
buffer = 100000;                                %set buffer length
awg = visa(visa_brand, visa_address, 'InputBuffer', buffer,'OutputBuffer', buffer); %configure instrument as a VISA resource
fopen(awg);                     %open the instrument as a VISA resource
awg.timeout = 5;                %set instrument timeout

%% Identify and reset instrument
instID = query(awg,'*idn?\n');  %identify instrument
disp(instID);                   %print instrument ID
fwrite(awg,'*rst');             %default the instrument

%% Open .mat file from local computer
% Since Waveform_Data_1, Waveform_Name_1, Waveform_Sampling_Rate_1, and
% Waveform_Amplitude_1 are all contained in the example .mat file, we can 
% simply send those variables to the AWG directly instead of creating 
% our own variables.
load('SingleCycleSine.mat');            %the .mat file needs to be in the current folder that Matlab is using
wfm_bytes = length(Waveform_Data_1)*4;  %calculate the number of bytes in the waveform, a single is 4 bytes per point

%% Generate commands
% The fwrite command used to send commands to the AWG can only handle a
% single array, so we need to generate single strings for each command we
% will be sending.
delete_waveform = sprintf('wlist:waveform:delete "%s"', Waveform_Name_1);   %delete any previous instances of the waveform we're sending
create_waveform = sprintf('wlist:waveform:data "%s",%s', Waveform_Name_1, wfm_header);  %generate first part of the command to send the waveform data
wfm_header = sprintf('#%d%d',length(num2str(wfm_bytes)), wfm_bytes);                    %generate an IEEE 488.2 binblock header
new_waveform = sprintf('wlist:waveform:new "%s", %d', Waveform_Name_1, length(Waveform_Data_1));    %create new waveform in AWG waveform list

%% Send commands
fwrite(awg, delete_waveform);   %send delete waveform command
fwrite(awg, new_waveform);      %send new waveform command

awg.EOIMode = 'off';                    %turn off automatic assertion of EOI at end of writes so we can send single command with multiple writes
fwrite(awg, create_waveform);           %send header of create waveform command
awg.EOIMode = 'on';                     %turn EOI mode back on
fwrite(awg, Waveform_Data_1,'single');  %send data for the create waveform command

if exist('Waveform_Sampling_Rate_1')    %check if the .mat file includes sample rate
    sample_rate = sprintf('clock:srate %f', Waveform_Sampling_Rate_1);  %set clock rate
    fwrite(awg, sample_rate);           %send sample rate command
if exist('Waveform_Amplitude_1')        %check if the .mat file includes amplitude
    amplitude = sprintf('source1:voltage:amplitude %f', Waveform_Amplitude_1);  %set amplitude
    fwrite(awg, amplitude);             %send amplitude command
assign_waveform = sprintf('source1:waveform "%s"', Waveform_Name_1);%assign waveform to channel
output_on = sprintf('output1:state on');                            %turn output on
run = sprintf('awgcontrol:run:immediate');                          %run AWG

fwrite(awg, assign_waveform);   %send assign waveform command
fwrite(awg, output_on);         %send output command
fwrite(awg, run);               %send run command

%% Close instrument
clear instrument;

Post Reply

Return to “Older and Obsolete Signal Sources”

Who is online

Users browsing this forum: No registered users and 2 guests