AnsweredAssumed Answered

Programming the PNA in Matlab

Question asked by odanzy Employee on Nov 27, 2006
Latest reply on Jul 3, 2014 by manuelpulido
Just in case anyone is interested.  Below is the code for the PNA to read the contents of the a Touchstone file from the PNA using the "CALC:DATA:SnP?" query using Matlab.  It then puts each value returned into the column of a matrix.  Matlab's Instrument Control Toolbox is required for this code to run.

%Set VISA Address String for Instrument
visaString = 'GPIB1::16::0::INSTR';

% Create a VISA-GPIB object.
vi = instrfind('Type', 'visa-gpib', 'RsrcName', visaString, 'Tag', '');

% Create the VISA-GPIB object if it does not exist
% otherwise use the object that was found.
if isempty(vi)
    vi = visa('AGILENT', visaString);
else
    fclose(vi);
    vi = vi(1);
end

% Configure instrument object, vi.
set(vi, 'InputBufferSize', 20000);
set(vi, 'Timeout', 30);

% Connect to instrument object, vi.
fopen(vi);

% Communicating with instrument object, vi.
opc_comp = query(vi, 'SYST:PRES; *OPC?', '%s\n' ,'%s');
fprintf(vi, 'CALC:PAR:SEL "CH1_S11_1"');

% Query selected measurement name
meas = query(vi, 'CALC:PAR:SEL?', '%s\n' ,'%s');

% Set byte order to swapped (little-endian) format
fprintf(vi, 'FORM:BORD SWAP');

% Set data type to real 64 bit binary block
fprintf(vi, 'FORM REAL,64');

% Read S2P data back from PNA.  A S2P file will return number of points * 9
% data points back.
fprintf(vi, 'CALC:DATA:SNP? 2');
[data, count, msg] = binblockread(vi, 'double');

% Flush the buffer
clrdevice(vi);

% Disconnect gpib object.
fclose(vi);

% Reshape data so it is split into columns
data_r=reshape(data, [(length(data)/9),9]);
data_r=data_r';

% Read frequency data back from returned data
freqs=data_r(1,:);

% This assumes that the return format is in log mag, angle pairs in the S2P
% file
S11mag=data_r(2,:);

plot(freqs,S11mag);
title('S11 mag');
xlabel('Frequency');
ylabel('dB');

% Clean up all objects.
delete(vi);

Outcomes