Code covered by the BSD License  

Highlights from
Two Port S-Parameter Measurement

Two Port S-Parameter Measurement



20 May 2009 (Updated )

Perform a 2-port s-parameter measurement using the Instrument Control Toolbox.

function [frequencies sParameters] = acquireTwoPortSParameters
%% Baseband Equivalent Modeling of a Transmission Line

% In this example, you model an RF transmission line stimulated by a pulse
% and plot the baseband-equivalent model that the blockset uses to simulate
% the transmission line in the time domain. This example helps you 
% understand how to best apply the baseband-equivalent modeling paradigm of
% performing time-domain simulation using a limited band of frequency data.
% This demo uses SCPI commands to interface with a PNA Series Network
% Analyzer (from Agilent). It is assumed that:
% 1. Calibration of the PNA has been completed by the user.
% 2. The Agilent IO libraries have been installed. 
% Author: Siddhartha Shankar

%% Demo Requirements
% This demonstration requires the following products:
%    * MATLAB       
%    * Instrument Control Toolbox
% The Following products are required to utilize the optional sections of
% this demo:
%   * Simulink
%   * RF Toolbox
%   * RF Blockset
%% Additional Notes
% * This M-File was last used and tested with MATLAB 7.6 (R2008a)
% * Consider using the MATLAB Report Generator to create a report from this
%   M-File

if ~isempty(oldObjects)
    clear oldObjects;
resourceStruct = instrhwinfo('visa','agilent'); %#ok<NASGU>
% A generalized way to create a visa interface object:
% eval(['visaObj = ',res.ObjectConstructorName{1}]);
visaObj = visa('agilent','GPIB0::16::INSTR');
% NOTE: Change GPIB0::16::INSTR appropriately for your PNA configuration

%% Configure interface object
% Set a sufficiently large input buffer size to store the S-Parameter data
set(visaObj, 'InputBufferSize', 20000);
% Set large timeout in the event of long s-parameter measurement
set(visaObj, 'Timeout', 30);

%% Connect to interface object, visaObj.

%% Configure parameter to be acquired and initiate 
% numOfPoints = 201; % Hard-code value if required
numOfPoints = query(visaObj, 'SENS:SWE:POIN?','%s\n','%d');

% Visual Confirmation of S11 in DB format. 
fprintf(visaObj, 'CALC:PAR:SEL "CH1_S11_1"'); 
data = localFetchData(visaObj,'S11');
% Format of returned data is as follows:
% Row 1 - Frequency: f1 f2 f3 ... fn
% Row 2 - Smn Magnitude: MX1 MX2 MX3 ... MXn (m = 1 to 2)
% Row 3 - Smn Angle: AX1 AX2 AX3 ... AXn (n = 1 to 2)

% Read frequency data back from returned data
plotHandle = plot(frequencies,S11mag);
title(plotHandle,'S11 mag');

%% GET S11
s11Data = localFetchData(visaObj,'S11');
s11 = complex(s11Data(2,:),s11Data(3,:));
%% GET S12
s21Data = localFetchData(visaObj,'S21');
s21 = complex(s21Data(2,:),s21Data(3,:));
%% GET S13
s12Data = localFetchData(visaObj,'S12');
s12 = complex(s12Data(2,:),s12Data(3,:));
%% GET S14
s22Data = localFetchData(visaObj,'S22');
s22 = complex(s22Data(2,:),s22Data(3,:));

%% Reshape and combine the 4 S-parameters to create a 3D matrix. 
% Once in this form, the data can be used by the "S-Parameters Amplifier" 
% block in simulink
S11 = reshape(s11,1,1,numOfPoints);
S21 = reshape(s21,1,1,numOfPoints);
S12 = reshape(s12,1,1,numOfPoints);
S22 = reshape(s22,1,1,numOfPoints);
sParameters = [S11 S12; S21 S22]; 
%% Additional parameters for simulink model, unused in this demo
% sampleTime = 2.5000e-010; 
% centerFrequency = 20.005e9;
% filterLength = 64;
%% Open simulink model from the MATLAB Command line and configure blocks
%  NOTE: This is a non-essential part of the M-File. Once the data is
%  acquired in the above lines of code, it can be processed as required in

% open_system('TxLineModel.mdl')
% set_param('TxLineModel/S-Parameters Amplifier','NetParamData','sParameters',...
% 'NetParamFreq','frequencies','SourceFreq','User-Specified','Freq','frequencies');
%% Close connection, delete visa object
clear visaObj;

function localWaitForSystemReady(visaObj)
opcStatus = 0;
    opcStatus = query(visaObj, '*OPC?','%s\n','%d'); 

function localConfigurePNA(fileFormat)
%% Preset system
% OPC? = All Operations Complete? +1 for Yes

%% Set S2P File Format. 
fprintf(visaObj, sprintf('MMEM:STOR:TRAC:FORM:SNP %s',fileFormat)); 
% MA - Linear Magnitude / degrees
% DB - Log Magnitude / degrees
% RI - Real / Imaginary
% AUTO - data is output in currently selected trace form

%% Set byte order to swapped (little-endian) format
% FORMat:BORDer <char>
fprintf(visaObj, 'FORM:BORD SWAP');
% NORMal - Use when your controller is anything other than an IBM compatible computers
% SWAPped - for IBM compatible computers

%% Set data type to real 64 bit binary block
% FORMat[:DATA] <char>, 64 for more significant digits and precision
fprintf(visaObj, 'FORM REAL,64');
% REAL,32 - (default value for REAL) Best for transferring large amounts of measurement data.
% REAL,64 - Slower but has more significant digits than REAL,32. Use REAL,64 if you have a computer that doesn't support REAL,32.
% ASCii,0 - The easiest to implement, but very slow. Use if small amounts of data to transfer.

function data = localFetchData(visaObj,sParameter)
% Set up the trace corresponding to PARAMETER on the PNA and return DATA,
% a matrix of 2-port S-Parameters in S2P format with specified PRECISION.
% COUNT is the number of values read and MESSAGE tells us if the read
% operation was unsuccessful for some reason.
fprintf(visaObj,sprintf('CALC:PAR:MOD %s',sParameter));
fprintf(visaObj, 'CALC:DATA:SNP? 2');

% Read the data back using binblock format
[rawData] = binblockread(visaObj, 'double');
data = reshape(rawData, [(length(rawData)/9),9]);
data = data';

Contact us