Joshua’s Code

Joshua is responsible for controlling the network analyzer. All of Joshua’s code is being developed in Matlab

This code block is a function to establish a connection with the Fieldfox network analyzer.

%
%Filename: FFConnectFunction
%function = connect(IP)
%
%Date: 2/17/2021
%Designer: Joshua Paine
%Team members: Jesus Gonzalez-Villafuerte & Daniel Greisen
%
%Purpose: Senior Design project, SATFSS 
%Description: Make VISA connection with Fieldfox
%
%Parameter: IPv4 address
%
%Subsequent: To be combined with code to move stages 
%	and code to predict coordinate values
%

function FFConnectFunction(ip)
    arguments
        ip string                       %function input variable
    end
    
    string x;                           %local variables for 
    string y;                           %concatenating command string
    
    instrreset                          %find and clear all objects form
    oldobjs = instrfind;                %workspace
    if (~isempty(oldobjs))
        fclose(oldobjs);
        delete(oldobjs);
    end
    clear oldobjs;
    
    x = 'TCPIP0::';                     
    y = '::inst0::INSTR';
    z = strcat(x,ip,y);                 %command string with input argument
    
    fieldFox = visa('KEYSIGHT', z);             %SCPI commands for 
    set(fieldFox,'InputBufferSize', 640000);    %setup and acquisition of
    set(fieldFox,'OutputBufferSize', 640000);   %VISA connection with 
    fopen(fieldFox);                            %Fieldfox
    fprintf(fieldFox, '*CLS');

    IDNString = query(fieldFox,'*IDN?');        %Identification query to 
    fprintf('Connected to: %s\n',IDNString);    %confirm connection
end

This block of code is a function to setup the measurement on the Fieldfox.

%
%Filename: FFSetupFunction
%function = FFSetupFunction()
%
%Date: 2/19/2021
%Designer: Joshua Paine
%Team members: Jesus Gonzalez-Villafuerte & Daniel Greisen
%
%Purpose: Senior Design project, SATFSS 
%Description: Setup the measurement for the Fieldfox
%Parameter: none at this time
%
%Subsequent: To be combined with code to move stages 
%	and code to predict coordinate values
%	will take parameters of Measurement-Type (Magnitude or Phase)


function FFSetupFunction ()

    instrreset
    oldobjs = instrfind;                            %find and clear all objects 
    if (~isempty(oldobjs))                          %from workspace
        fclose(oldobjs);
        delete(oldobjs);
    end
    clear oldobjs;

                                                    %Setup and open VISA 
                                                    %connection with Fieldfox
                                            
    %fieldFox = visa('KEYSIGHT', 'TCPIP0::10.0.0.123::inst0::INSTR');
    fieldFox = visa('KEYSIGHT', 'TCPIP0::192.168.8.112::inst0::INSTR');
    set(fieldFox,'InputBufferSize', 640000);
    set(fieldFox,'OutputBufferSize', 640000);
    fopen(fieldFox);

    fprintf(fieldFox, 'SYST:PRES');                 %system preset to factory settings
    fprintf(fieldFox, 'INST:RLOC:DIS 1');           %disable remote lock-out
    fprintf(fieldFox, 'INST "NA"');                 %select NA mode
    fprintf(fieldFox, 'SENS:FREQ:STAR 8E9');        %set start frequency
    fprintf(fieldFox, 'SENS:FREQ:STOP 12E9');       %set stop frequency
    fprintf(fieldFox, 'SOUR:POW:ALC HIGH');         %set source power to high
    fprintf(fieldFox, 'SENS:BWID 1E3');             %set IF bandwidth
    fprintf(fieldFox, 'DISP:WIND:SPL D12_34');      %setup display to show all 4 S-Parameters
    fprintf(fieldFox, 'SWE:POIN 1001');             %set sweep resolution

end

This block of code if a function that records the full S-parameters using the Fieldfox and saves them to a USB drive and names the file according to the coordinates of the measurement and the measurement type.

%
%Filename: FFRecordFunction
%function = FFRecordFunction(Horizontal,Vertical,Type)
%
%Date: 2/19/2021
%Designer: Joshua Paine
%
%Purpose: Senior Design project, SATFSS 
%Description: Setup the measurement for the Fieldfox
%Parameter: X,Z coordinates and Measurement-Type (Magnitude or Phase)
%
%Revision:
%

function FFRecordFunction (Horizontal,Vertical,Type)
    arguments                                       %Setup Arguments
		Horizontal string;
		Vertical string;
        Type string;
    end
    
    string M;                                       %Local variables
    string P;
    
    instrreset                                      %Clear connection
        oldobjs = instrfind;                        %of old objects
        if (~isempty(oldobjs))
            fclose(oldobjs);
            delete(oldobjs);
        end
    clear oldobjs;
    
%Setup VISA connection with Fieldfox 
 %   fieldFox = visa('KEYSIGHT', 'TCPIP0::10.0.0.123::inst0::INSTR');
    fieldFox = visa('KEYSIGHT', 'TCPIP0::192.168.8.112::inst0::INSTR');
    set(fieldFox,'InputBufferSize', 640000);
    set(fieldFox,'OutputBufferSize', 640000);
    fopen(fieldFox);
    fprintf(fieldFox, 'INST:RLOC:DIS 1');           %Disable insturment lock-out
 
%Concatonate an string that is the SCPI command 
%that also has a naming convention for the file
%that indicates the location and type of measurement
	A = 'MMEM:STOR:FDAT "';                         
    B = '-';
	C = '.csv"';
	FName = strcat(A,Horizontal,B,Vertical,Type,C);
	fprintf(FName);
    
% select each trace and set to measure magnitude in dB 
    if Type == "Mag"
        fprintf(fieldFox, 'CALC:PAR1:SEL');
        fprintf(fieldFox, 'CALC:FORM MLOG');
        fprintf(fieldFox, 'CALC:PAR2:SEL');
        fprintf(fieldFox, 'CALC:FORM MLOG');
        fprintf(fieldFox, 'CALC:PAR3:SEL');
        fprintf(fieldFox, 'CALC:FORM MLOG');
        fprintf(fieldFox, 'CALC:PAR4:SEL');
        fprintf(fieldFox, 'CALC:FORM MLOG');
% save data to a USB drive with filename of coordinates of measurement and 
% measurement type       
        pause(10)
        fprintf(fieldFox, 'MMEM:CDIR "[USBDISK]:"');
        pause(10);
        fprintf(fieldFox, FName);
    end

% select each trace and set to measure phase 
    if Type == "Phase"
        fprintf(fieldFox, 'CALC:PAR1:SEL');
        fprintf(fieldFox, 'CALC:FORM PHAS');
        fprintf(fieldFox, 'CALC:PAR2:SEL');
        fprintf(fieldFox, 'CALC:FORM PHAS');
        fprintf(fieldFox, 'CALC:PAR3:SEL');
        fprintf(fieldFox, 'CALC:FORM PHAS');
        fprintf(fieldFox, 'CALC:PAR4:SEL');
        fprintf(fieldFox, 'CALC:FORM PHAS');
% same saving method for phase measurements
        pause(10)
        fprintf(fieldFox, 'MMEM:CDIR "[USBDISK]:"');
        pause(10);
        fprintf(fieldFox, FName);
    end
    fprintf('\n');
end

This code block is a script that takes a full S-parameter measurement of magnitude and plots them together on one graph.

%
%Filename: MyAttemptToPlotAllFourTraces
%Working script in progress
%
%Date: 2/27/2021
%Designer: Joshua Paine
%Team members: Jesus Gonzalez-Villafuerte & Daniel Greisen
%
%Purpose: Senior Design project, SATFSS 
%Description: Connect to Fieldfox, setup measurement, read and plot data
%   from all four S-Parameters
%             
%Subsequent: to be combined with Jesus' code for moving the stages %and Daniels code to compare with the Kriging prediction
%   To be made into a function
%
%
   
function
    
%Setup Fieldfox to prepare for measurement
    fprintf(fieldFox,'INST:SEL ''NA''');                    %Mode selection
    fprintf(fieldFox,'INIT:CONT 0\n');                      %set to hold mode
    fprintf(fieldFox,'FREQ:STAR 8E9;STOP 12E9\n');          %set start and stop freqs
    fprintf(fieldFox,'SWE:POIN 101\n');                     %set resolution
    fprintf(fieldFox,'CALC:PAR1:SEL\n');                    %select trace 1 to read
    fprintf(fieldFox,'*OPC?\n');
    done = fscanf(fieldFox,'%1d');
    
% setup and measure trace1 S11
    fprintf(fieldFox,'INIT;*OPC?\n');                       %initiate trace 
    trigComplete = fscanf(fieldFox,'%1d');                  %format data for 
    fprintf(fieldFox, 'FORM:DATA REAL,32\n');               %human viewing
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');                 %read the data
    myS11Data = binblockread(fieldFox,'float');             %assign data to var
    hangLineFeed = fread(fieldFox,1);    
        
% setup amd measure frequency span
    fprintf(fieldFox, 'FORM:DATA REAL,64\n');               %format the data
    fprintf(fieldFox,'SENS:FREQ:DATA?\n');                  %comma seperated array
    myBinStimulusData = binblockread(fieldFox,'double');    %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% setup and measure trace2 S21  
    fprintf(fieldFox,'CALC:PAR2:SEL\n');                    %select trace 2 to read
    fprintf(fieldFox, 'FORM:DATA REAL,32\n');               %format data
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');                 %read the trace data
    myS21Data = binblockread(fieldFox,'float');             %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% setup and measure trace3 S12     
    fprintf(fieldFox,'CALC:PAR3:SEL\n');                    %select trace 3 to read
    fprintf(fieldFox, 'FORM:DATA REAL,32\n');               %format the data
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');                 %read the data
    myS12Data = binblockread(fieldFox,'float');             %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% setup and measure trace4 S22     
    fprintf(fieldFox,'CALC:PAR4:SEL\n');                    %select trace 4 to read
    fprintf(fieldFox, 'FORM:DATA REAL,32\n');               %format the data
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');                 %read the data
    myS22Data = binblockread(fieldFox,'float');             %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
    display myBinData;
    display myBinStimulusData;
    display myBinS21Data;

    xMHz = myBinStimulusData/1E9;                           %format data for plotting
    clear title xlabel ylabel;                               

%Setup polot for all four traces
    plot(xMHz, myS11Data)
    hold on
    plot(xMHz, myS21Data)
    hold on
    plot(xMHz, myS12Data)
    hold on
    plot(xMHz, myS22Data)
    hold off
%Label plot    
    title('Full S-Parameters')
    xlabel('Frequency (MHz)')
    ylabel('Log Mag (dB)')
    legend('S11', 'S21', 'S12', 'S22')

    fprintf(fieldFox, 'SYST:ERR?')
    finalErrCheck = fscanf(fieldFox, '%c');

This code block is a function that takes as an argument the fieldFox object and returns 8 vectors of the full S-parameters both magnitude and phase. this function is designed to be called only after a connection is made and a measurement is setup.

%
% Filename: ReadSParameters(fieldfox)
% Working script in progress
% Date: 4/9/2021
% Designer: Joshua Paine
% Team members: Jesus Gonzalez-Villafuerte & Daniel Greisen
%
% Purpose: Senior Design project, SATFSS 
% Description: To be called in the main script to read the S-parameters 
%   and formet the data to be readable 
% Returns the variables of 8 [101x8] vectors the full S-parameters
%   magnitude and phase
% Function is called after a FFConnectFunction and FFSetupFunction are
%   called
% The funcion is passed the fieldFox object as an  
%             
% Subsequent: to be combined with Jesus' code for moving the stages and
%   Daniels code to compare with the Kriging prediction
%
%
   
function [S11M, S21M, S12M, S22M, S11P, S21P, S12P, S22P] = ReadSParameters (fieldFox)
    
% set to hold mode and wait 
    fprintf(fieldFox,'INIT:CONT 0\n');                      
    query(fieldFox,'*OPC?\n');      
    done = fscanf(fieldFox,'%1d');
    
% select each trace individually and set to measure magnitude in dB    
    fprintf(fieldFox, 'CALC:PAR1:SEL');
    fprintf(fieldFox, 'CALC:FORM MLOG');
    fprintf(fieldFox, 'CALC:PAR2:SEL');
    fprintf(fieldFox, 'CALC:FORM MLOG');
    fprintf(fieldFox, 'CALC:PAR3:SEL');
    fprintf(fieldFox, 'CALC:FORM MLOG');
    fprintf(fieldFox, 'CALC:PAR4:SEL');
    fprintf(fieldFox, 'CALC:FORM MLOG');
    
% setup and measure trace1 S11
    fprintf(fieldFox,'INIT;*OPC?\n');                   %initiate trace 
    trigComplete = fscanf(fieldFox,'%1d');              %format data for 
    fprintf(fieldFox, 'FORM:DATA REAL,32\n');           %human viewing
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');             %read the data
    S11M = binblockread(fieldFox,'float');              %assign data to var
    hangLineFeed = fread(fieldFox,1);    
           
% setup and measure trace2 S21  
    fprintf(fieldFox,'CALC:PAR2:SEL\n');                %select trace 2 to read
    fprintf(fieldFox, 'FORM:DATA REAL,32\n');           %format data
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');             %read the trace data
    S21M = binblockread(fieldFox,'float');              %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% setup and measure trace3 S12     
    fprintf(fieldFox,'CALC:PAR3:SEL\n');                %select trace 3 to read
    fprintf(fieldFox, 'FORM:DATA REAL,32\n');           %format the data
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');             %read the data
    S12M = binblockread(fieldFox,'float');              %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% setup and measure trace4 S22     
    fprintf(fieldFox,'CALC:PAR4:SEL\n');                %select trace 4 to read
    fprintf(fieldFox, 'FORM:DATA REAL,32\n');           %format the data
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');             %read the data
    S22M = binblockread(fieldFox,'float');              %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% select each trace individually and set to measure phase  
    fprintf(fieldFox, 'CALC:PAR1:SEL');
    fprintf(fieldFox, 'CALC:FORM PHAS');
    fprintf(fieldFox, 'CALC:PAR2:SEL');
    fprintf(fieldFox, 'CALC:FORM PHAS');
    fprintf(fieldFox, 'CALC:PAR3:SEL');
    fprintf(fieldFox, 'CALC:FORM PHAS');
    fprintf(fieldFox, 'CALC:PAR4:SEL');
    fprintf(fieldFox, 'CALC:FORM PHAS');
    
% setup and measure trace1 S11
    fprintf(fieldFox,'CALC:PAR1:SEL\n');
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');             %read the data
    S11P = binblockread(fieldFox,'float');              %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% setup and measure trace2 S21
    fprintf(fieldFox,'CALC:PAR1:SEL\n');
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');             %read the data
    S21P = binblockread(fieldFox,'float');              %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% setup and measure trace3 S12    
    fprintf(fieldFox,'CALC:PAR1:SEL\n');
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');             %read the data
    S12P = binblockread(fieldFox,'float');              %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
% setup and measure trace4 S22    
    fprintf(fieldFox,'CALC:PAR1:SEL\n');
    fprintf(fieldFox,'CALC:DATA:FDATA?\n');             %read the data
    S22P = binblockread(fieldFox,'float');              %assign data to var
    hangLineFeed = fread(fieldFox,1);
    
    fprintf(fieldFox,'INIT:CONT 1\n');                  %remove hold state
end