Monday, June 18, 2012

Sending Binary-Block Waveform Data to an AWG using MATLAB

In this post we will look at how to send waveform points as a binary-block to an arbitrary waveform generator (AWG) using MATLAB. The reason for sending a waveform as binary data versus ASCII data is simple, the binary data is much smaller compared to the equivalent ASCII data. This cuts down on remote IO latency between the computer and AWG for faster performance. Also there is typically a limit on the size of a waveform that can be sent to an AWG remotely in ASCII so for large waveforms you may have to use binary data. Before continuing there are two previous GPETE blog posts that I would recommend to you related to this topic. The first is a post that covers how to connect to an instrument remotely using MATLAB, to go to this post click here. The second is a post that explains how you can get waveform data from a PC or a scope to a modern AWG without using a remote connection, to go to this post click here.

The MATLAB function that makes the binary transfer of waveform data points easy to do is the binblockwrite(). This function writes binary-block (binblock) data to an instrument. To learn more about the binblockwrite() function check out the MATLAB help page on the method by clicking here or type “help binblockwrite” into the MATLAB command line. Note that the binblockwrite function only be used if you have the Instrument Control Toolbox for MATLAB, which is needed anyway to control instruments remotely with MATLAB. For the binblockwrite function to work properly you need to know the endian format of your computer and the AWG. The term endian or endianness refers to the ordering of individually addressable sub-components within the representation of a larger data item (for more info click here). If the endian format of your computer does not match that of the AWG you will need to swap the endian format on the instrument using the proper command before you call binblockwrite.

Let’s lot at an example using MATLAB to send waveform points to Agilent’s 33522A function / arbitrary waveform generator using SCPI. In the below MATLAB example waveform data was converted to a binary-block and sent to the 33522A using a remote USB connection. The SCPI language used in the MATLAB code was for Agilent’s 33521A / 33522A function / arbitrary waveform generators. If you are using a different AWG chances are the command language will be different. The comments in green are describing what is happening in the code.

%opens and creates a visa session for communication with function generator
fgen = visa('AGILENT','USB0::0x0957::0x2C07::MY50000780::0::INSTR');
set (fgen,'OutputBufferSize',2000000);
fopen(fgen);

%Query Idendity string and report
fprintf (fgen, '*IDN?');
idn = fscanf (fgen);
fprintf (idn)
fprintf ('\n\n')

%Clear and reset instrument
fprintf (fgen, '*RST');
fprintf (fgen, '*CLS');

%Clear volatile memory
fprintf(fgen,'SOURce1:DATA:VOLatile:CLEar');

%create waveform
i = 1:1:100000;        % Set rise time (100000 points) */
z = (i-1)/100000;
   
fprintf(fgen, 'FORM:BORD SWAP'); %swap the endian forma
binblockwrite(fgen, z, 'float32', 'SOURce1:DATA:ARBitrary testarb, '); %send the data using binary to the instrument

fprintf(fgen, '*WAI'); %wait for operation to finish
fprintf('Download Complete\n\n')

%Set desired configuration of 33522A
fprintf(fgen,'SOURce1:FUNCtion:ARBitrary testarb'); % set current arb waveform to defined arb pulse
fprintf(fgen,'SOURce1:FUNCtion ARB'); % turn on arb function
fprintf(fgen,'SOURCE1:VOLT 2'); % set max waveform amplitude to 2 Vpp
fprintf(fgen,'SOURCE1:VOLT:OFFSET 0'); % set offset to 0 V
fprintf(fgen,'OUTPUT1:LOAD 50'); % set output load to 50 ohms
fprintf(fgen,'SOURCE1:FUNCtion:ARB:SRATe 100e6'); % set sample rate
 
%Enable Output
fprintf(fgen,'OUTPUT1 ON'); % turn on channel 1 output

% Read Error
fprintf(fgen, 'SYST:ERR?');
errorstr = fscanf (fgen);

% error checking
if strncmp (errorstr, '+0,"No error"',13)
   errorcheck = 'Arbitrary waveform generated without any error\n';
   fprintf (errorcheck)
else
   errorcheck = ['Error reported: ', errorstr];
   fprintf (errorcheck)
end

%Save Arb to USB stick, titled test arb
 %fprintf(fgen, 'MMEM:STOR:DATA "USB:\TEST ARB"');

%closes the visa session with the function generator
fclose(fgen);

The majority of the above code is setting up the connection, configuring the 33522A settings, and error checking (to download the 33522A’s programming guide that specifies its commands click here). The main two lines we are interested in for handling binary data are highlighted. The first highlighted line sends a command to the 33522A to swap its endian format to match the format of the incoming data. The second highlighted line is the binblockwrite function, which will convert and send the waveform as a binary-block to the 33522A. The four arguments used in the binblockwrite function are as follows:
  1. “fgen” is the object that refers to the 33522A.
  2. “z” is the array holding the waveform data.
  3. “float32” states the precision. This allows the function to set the number of bits written for each value and the interpretation of the bits. Besides floating-point values, character and integer formats can be used.
  4. “SOURce1:DATA:ARBitrary testarb, “ is the header that will be prefixed to the binary data. In this example it is the beginning of the command to send a binary waveform to the 33522A.
Note that the binary data is in the binary-block format defined by IEEE and the binblockwrite function automatically configures and prefixes the IEEE header to the binary data (the header states the length of the binary data).

The above example MATLAB code creates a simple ramp waveform named "testarb" with a amplitude of 2 Vpp and a frequency of 1 KHz (100 MS/s / 100 KS = 1 KHz). The example MATLAB file was run and the resulting waveform can be seen in the below scope screen shot.


In this post we looked at how to send waveform points to an AWG as a binary-block of data using MATLAB. MATLAB has a function called "binblockwrite" that makes sending binary data to an instrument easy. Before sending the binary data, you want to ensure the endian format of the data matches the AWG's endian read format. Feel free to copy the MATLAB code example used in the post or send me an email and I can send you the .m file. If you have any questions feel free to email me and if you have any personal incites to add use the "Comments" section below.

For more information on the 33522A function / arbitrary waveform generator click here

3 comments: