clear all;
close all;
clc;
%___________________________________________________________________________________

% loading CSV file
File_name = 'log.csv';                %Name for loading data file
Tau_Step = 0.01;                                %Stepping of Tau
DUT_Ref_freq = 10000000;                        %frequency of DUT oscilator
REF_Ref_freq =  9990000;                        %frequency of Reference oscilator

%___________________________________________________________________________________

Ref_freq =abs( DUT_Ref_freq - REF_Ref_freq);         %reference frequency after mixed

data_load = csvread (File_name);                %loading data from file.CSV

dCount = numel(data_load);                      %count of load data

% calculation of Tau array, from minimum tau to max during tau step
Tau_0 = 0.0001;                                 
Tau_Start = 0.0001;
Tau_Stop = (dCount*Tau_0)/2;

Tau = Tau_Start:Tau_Step:Tau_Stop;
Tau = Tau';
Tau_Count = numel(Tau);

   
if rem(Tau_Stop,Tau_Step) ~= 0
    error('Tau musi mit celociselny pocet kroku')
end

% calculate Allan variance     
AVARArray = zeros(Tau_Count, 1);
for i = 1:Tau_Count
    Tau_A = Tau(i);                             %actual tau
    FracFreqArray = CalculateFracFreq(Tau_A, Tau_0, data_load);     %calculating of fractional frequency
    FracFreqArray = FracFreqArray - Ref_freq;                       
    FracFreqArray = FracFreqArray ./ Ref_freq;
    AVARArray(i) =  Calculate_oAVAR(FracFreqArray);
end

% current ploting measurment data
figure(1);
plot(data_load); title('Frequency vs. Time'), xlabel('Time [s]'), ylabel('Frequency [Hz]')


% ploting sigma tau figure
figure(2);
loglog(Tau, AVARArray); title('Sigma vs. Tau'), xlabel('Tau [s]'), ylabel('Sigma [-]'), grid on 

% ploting histogram of measurment data
figure(3);
hist((data_load - Ref_freq),101);  title('Histogram of Measurment frequency'), xlabel('Frequency [Hz]')




