Daniels Code

Daniel is responsible for the kriging machine learning algorithm. All of his code is being written in Matlab.

This code block sets up the algorithm for predicting the values of a small matrix.

% Standardized Automated Testing of Frequency Selective Surfaces
% SAT FSS
%
% Machine Learning Algorithm 
% Kriging Model
%
% Joshua Paine, Jesus Gonzalez, Daniel Greisen 

test = [4,4,4,4,4,4,4,4,4
        3,3,3,3,3,3,3,4,4
        2,2,2,2,2,3,3,4,4
        2,2,2,2,2,3,3,4,4
        1,1,1,2,2,3,3,4,4
        1,1,1,2,2,3,3,4,4
        0,1,1,2,2,3,3,4,4]; % m x n = 7 x 9
    
ds = [1 1 1 2 2 3 4 4 5 6 6 7 7 7; 3 4 5 3 5 4 3 5 4 3 5 3 4 5]; % Design Sights, m = 2 & n = 14
lends = length(ds); % length of design site
mv = [2; 2; 3; 2; 3; 2; 2; 3; 3; 3; 3; 4; 4; 4]; % Measured Values
ts = [2 3 3 4 5 5 6; 4 3 5 4 3 5 4]; %test site
lents = length(ts); % length of test site

% Constants 
a = 0;
theta = 0.3; % usually between 0 to 1
omega = 1; %due to normalization

% Calculate distances between design sites
sij = zeros(lends);
for ix = 1:lends
    for iy = 1:lends
        sij(ix,iy) = (ds(1,ix)-ds(1,iy))^2 + (ds(2,ix)-ds(2,iy))^2;
    end
end

% calculate distnaces between test sites and design sites
sn = zeros(lents,lends);
for ix = 1:lents
    for iy = 1:lends
        sn(ix,iy) = (ts(1,ix) - ds(1,iy))^2 + (ts(2,ix) - ds(2,iy))^2;
    end
end

% transpose sn to get distnance values for each test site lined up in
% columns
sn = sn';

cij = (omega^2 - a) * exp(-theta*sij.^2); % covariance of design sites
co = (omega^2 - a) * exp(-theta*sn.^2); % covariance between test site and design sites

% extend row and column of 1s to cij and fix last element to zero
cij(end+1, :) = 1;
cij(:, end+1) = 1;
[m n] = size(cij);
cij(m,n) = 0;
cijin = inv(cij);

% extend row of value one to co
co(end+1, :) = 1;

% Calculate weights for each design site
w = cijin * co;

% Get size of weight vector, should be n + 1
[c d] = size(w);

% Calculate estimated value
Y = w(1:c-1)' .* mv;


Here is a code block that is designed to plot the data that is collected and predicted from the Kriging Model.

function op = plotData(op, Y, dX, nsite, nsample, tX)



    % get desired frequency from user
    freq = input('What frequency would like to examine? (GHz):  ');

    % generate vector of actual freqeuncy steps FielFox is taking
    f = zeros(100,1);
    a = 8;
    for i = 1:length(f)
        f(i,1) = a;
        a = a + 0.0396;
    end

    % determine if selected frequency is in vector of fieldfox frequencies 
    if freq == 8
        idx = round((1/0.0396)*(freq - 8) + 1);
    else 
        idx = round((1/0.0396)*(freq - 8));
    end
    val = find(f==freq);
    if isempty(val)
        fprintf('This frequency is not specified in range of FieldFox\n');
        fprintf('Selecting next closest value: ')
        z = f(idx,1);
        fprintf('%.3f GHz\n',z);
    else 
        fprintf('Gathering data for selected Freqeuncy...\n');
    end

    % record the index where freqeuency in question is stored
    index = find(f==z);

    % get user input for which S parameter and component they would like to
    % see. Variable "col" will be set which reflects the S parameter and either
    % mag or phase of that parameter.
    dec = 1;
    while (dec)
        Sparam = input('Which S-Parameter would you like to examine? (S11 = 1, S12 = 2, S21 = 3, S22 = 4): ');
        in = input('Would you like to see magnitude or phase of this S-Parameter? (mag = 1, pha = 2): ');
        if (Sparam == 1 && in == 1)
                col = 1;
                fprintf('Generating plot for S11 Mag\n');
                dec = 0;
                s = 'S11 ';
                t = 'Mag (dB)';
        else if (Sparam == 1 && in == 2)
                col = 5;
                fprintf('Generating plot for S11 Phase\n');
                dec = 0;
                s = 'S11 ';
                t = 'Phase (degrees)';
        else if (Sparam == 2 && in == 1)
                col = 2;
                fprintf('Generating plot for S12 Mag\n');
                dec = 0;
                s = 'S12 ';
                t = 'Mag (dB)';
        else if (Sparam == 2 && in == 2)
                col = 6;
                fprintf('Generating plot for S12 Phase\n');
                dec = 0;
                s = 'S12 ';
                t = 'Phase (degrees)';
        else if (Sparam == 3 && in == 1)
                col = 3;
                fprintf('Generating plot for S21 Mag\n');
                dec = 0;
                s = 'S21 ';
                t = 'Mag (dB)';
        else if (Sparam == 3 && in == 2)
                col = 7;
                fprintf('Generating plot for S21 Phase\n');
                dec = 0;
                s = 'S21 ';
                t = 'Phase (degrees)';
        else if (Sparam == 4 && in == 1)
                col = 4;
                fprintf('Generating plot for S22 Mag\n');
                dec = 0;
                s = 'S22 ';
                t = 'Mag (dB)';
        else if (Sparam == 4 && in == 2)
                col = 8;
                fprintf('Generating plot for S22 Phase\n');
                dec = 0;
                s = 'S22 ';
                t = 'Phase (degrees)';
            end
            end
            end
            end
            end
            end
            end
        end
    end
    
    idx = 1;
    for i = 1:10
        for j = 1:10
            Mp(idx,1) = Y{i,j}(index,col);
            idx = idx + 1;
        end
    end
    % generate plot of estimated values
    for i = 1:nsample % run for each point in tX
        M(i,1) = Y{tX(i,1),tX(i,2)}(index,col); % 5 x 1, building measured value vector
    end
    
    figure
    x = reshape(dX(:,1), nsite, nsite);
    y = reshape(dX(:,2), nsite, nsite);
    Y_est = reshape(Mp, size(x));
    mesh(x,y,Y_est')
    hold on
    plot3(tX(:,2),tX(:,1),M,'.k', 'MarkerSize',10)
    title(['Estimated surface at ' num2str(z) 'GHz with measurement sites: Y_{est}'])
    g = [s t];
    zlabel(g);
    
    
    op = input('To generate another plot, please press 1: ');
    
end

Here is a code block that was used in testing to confirm that primary components of the main code would work together. This began to introduce user input to drive the operation of the main code.

clear

% Set site dimensions and number of test samples
nsite = input('Enter desired dimensions for test site: ');
nsample = input('Enter desired number of test points: ');

% generate grid
dX = gridsamp([1 1;nsite nsite],nsite);

% generate sample matrix...test sites to be measured
tX = floor(lhsu([1 1],[nsite nsite],nsample));
tX = unique(tX,'rows');
nsample = length(tX(:,1));

% JESUS AND JOSH CODE TAKE OVER

% BELOW CODE IS FOR TESTING PURPOSES

% create cell array where mag and phas data will be stored
Y = cell(nsite,nsite);
cp = [0 0];

% store random test data into cell array (acting as measured values)
for im = 1:nsample
    A = im*ones(101,8); % random data for testing purposes
    dx = tX(im,1) - cp(1,1);
    dy = tX(im,2) - cp(1,2);
    Y{cp(1,1)+dx,cp(1,2)+dy} = A;
    cp(1,1) = cp(1,1) + dx;
    cp(1,2) = cp(1,2) + dy;
end

% create vector of measured values for specific frequency
M = zeros(nsample,1); % 5 x 1



for h = 1:8
    for j = 1:101
        for i = 1:nsample % run for each point in tX
            M(i,1) = Y{tX(i,1),tX(i,2)}(j,h); % 5 x 1, building measured value vector
        end
        
        %define constants
        %push data through Kriging Algorithm
        theta = [10 10];
        lob = [0.1 0.1];
        upb = [20 20];
        [dmodel perf] = dacefit(tX, M, @regpoly0, @corrgauss, theta, lob, upb);
        [Mp MSE] = predictor(dX, dmodel);
        
        idx = 1;
        for k = 1:nsite
            for g = 1:nsite
                Y{k,g}(j,h) = Mp(idx,1); 
                idx = idx + 1;
            end
        end
        
    end
end

fprintf('Estimated data calcualtions complete!\n');
op = input('To generate a plot with specific data please press 1: ');

while (op)
    op = plotData(op, Y, dX, nsite, nsample, tX);
end

This is a function called go_home. It run very first when the main script is run in the event that the RX antenna is somewhere out in the measurement field. This function moves the X-stage left until it stops on the horizontal proximity switch. Then it moves the Z-stage down until it stops on the vertical proximity switch. This is the (0,0) or home position and where all new measurements begin.

function go_home
%   SAT FSS
%
%   This Function sends the linear stages to the (0,0) point. This point is
%   represented by the leftmost and bottommost point the linear stages 
%   may travel.
%
%   Author: Daniel Greisen
%
%   31 March 2021
%
%   Team Members: Joshua Paine & Jesus Gonzalez

d = 65000;
    stages_function(d,4)
    pause(5);
    stages_function(d,2)
    

end