% Reads the Agilent PNA files 
% Examples
clear all
close all

% Constants
Z0=50; %Ohm

% Select the Agilent s2p data file
%filename='traccia2.s2p';
filename='S11_modo0.prn';
%filename='traccia.s1p';

if strfind(filename,'.prn'),
    out=DatareadPrn(filename);
    
    % Data for the plot
    stimulus=out.stimulus;
    plotData=out.data;
    plotXlabel=out.stimulusLabel;
    plotTitle=out.measurement;
    plotYlabel=out.dataLabel;

elseif strfind(filename,'.s1p'),
    out=DatareadS1p(filename);
    
    % Data for the plot
    stimulus=out.stimulus;
    plotData=out.data;
    plotXlabel=out.stimulusLabel;
    plotTitle=out.measurement;
    plotYlabel='';            
            
elseif strfind(filename,'.s2p'),
    out=DatareadS2p(filename);
    S11data=out.S11;
    S22data=out.S22;

    % Example of possible data manipulation
    % Compute the impedance from reflection coefficient
    ImpedanceIn=Z0.*(1+S11data)./(1-S11data);
    ImpedanceOut=Z0.*(1+S22data)./(1-S22data);
    
    % Data for the plot
    stimulus=out.stimulus;
    plotData=ImpedanceIn;
    plotXlabel=out.stimulusLabel;
    plotTitle='Impedance from reflection meas.';
    plotYlabel='\Omega';

end

% Test plot: follow this comand to make the plots
fig=figure;
% User defined settings 
% ----
set(gca,'FontSize',22,'LineWidth',2);
grid(gca,'on');
box(gca,'on');
hold(gca,'all');
% ----

if strfind(filename,'.prn'),    % plotData is already formatted
    plot(stimulus,plotData,'Linewidth',2);
else                            % plotData is not already formatted
    plot(stimulus,abs(plotData),'Linewidth',2);
end 
xlabel(plotXlabel);
ylabel(plotYlabel);
title(plotTitle);
