%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%                                                                   %%%%
%%%%    Program to process and plot Network Analyzer data              %%%%
%%%%                                                                   %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Reading, opening and transfering Network Analyzer data
fid=fopen('na_data.txt', 'r');
data_na=fscanf(fid, '%g, %g');    
status=fclose(fid);  

% Retrieving file size
[m,n] = size(data_na);

% Sorting data
    for j=1:1:m;                      
        re_im(j,:)=data_na(1+(j-1)*n:1:j*n);
    end  
   
% Setting Network Analyzer frequency range    
freq = (1e9:10e6:3e9)';

% Getting the data's magnitude
data = re_im(:,1).^2 + re_im(:,2).^2 ;
data = sqrt(data);

% Converting to dB's
data_db = 10 * log(data);

% Plotting Reflection Coefficient (S11) vs Frequency 
plot(freq,data_db)