% *************************************************************************************************
%  File: frequency_domain_analysis.m
%
%  Brief: subor obsahuje algoritmus analyzi vo frekvencnej oblasti pre detekciu stredu 
%         interferencnych pruzkov
% 
%  Version: 1.0  Revision: 0.0   Name: peter novotny
% 
%  Date: 2011-01_22
%
%  Matlab version: R2009a
% *************************************************************************************************

% **************************************************************************************************
% Nastavenie parametrov pre prevod do frekvencnej oblasti 
% **************************************************************************************************

lambda_sampling = step; % vzorkovacia vzdialenost[nm]
k_sampling = 2*pi/lambda_sampling; % vzorkovacia priestorova frekvencia

% **************************************************************************************************
% Fourierova transformacia a analyza v spektralnej oblasti
% **************************************************************************************************

% ziskanie zloziek signalu (spektrum priesorovych frekvencii)
I_space_freq_domain_all = fft(I_1D); 
% vyber kladnych frekvencii
I_space_freq_domain = I_space_freq_domain_all(1:round(N/2));
% /(N/2) je normalizacia aby sedela amplituda 
I_space_freq_domain = I_space_freq_domain/(N/2); 
% odstranenie jednosmernej zlozky
I_space_freq_domain(1) = 0; 

% Nastavenie rozsahu priestorovych frekvencii
k = (k_sampling/2)*(linspace(0,1, round(N/2)));
% figure; stem(k, abs(I_space_freq_domain)), grid;

% vyber oblasti frekvencii podstatnej pre analyzu (vacsinou staci okolie 2-3 bodov)
[max_ampl_value,max_ampl_pos] = max(abs(I_space_freq_domain));
% pocet hodnot frekvencii v okoli maxima branych do vypoctu smernice 
okolie = 3; 
I_detail = I_space_freq_domain(max_ampl_pos - okolie: max_ampl_pos + okolie);
k_detail = k(max_ampl_pos - okolie: max_ampl_pos + okolie);

% figure; stem(k_detail, abs(I_detail)), grid;
% xlabel('k [rad/nm]'); ylabel('Amplituda [-]');

%  zistenie zavislosti zmeny faze na vlnovom cisle k
fi = unwrap(angle((I_space_freq_domain)));
% figure; stem(k(:),fi(:),'b'); grid; hold; 
% xlabel('k [rad/nm]'); ylabel('fi [rad]');

% vyber oblasti faze podstatnej pre analyzu 
fi_detail = fi(max_ampl_pos - okolie: max_ampl_pos + okolie);
% figure; plot(k_detail, fi_detail,'g','Linewidth',2); grid on; hold;
% stem(k_detail, fi_detail,'b');

% linearizacia zavislosti faze na vlnovom cisle(frekvencii) metodou najmensich stvorcov
coeff = polyfit(k_detail, fi_detail, 1); 
fi_lsqr = polyval(coeff, k_detail);
% plot(k_detail,fi_lsqr,'r','Linewidth',2); xlabel('k [rad/nm]'); ylabel('faza [rad]');

% smernica odpovedajuca rozdielu optickych drah je rovna smernici aproximovanej priamky
p = coeff(1);  
q = coeff(2);

% smernica moze byt kladna alebo zaporna podla toho v ktorej casti signalu sa nachadyaju pruzky
if p < 0
    fringe_center =  abs(p);
else
    fringe_center =  z(end) - p;
end

% % **************************************************************************************************
% %  END OF FILE: FDA_pre_modelovane_data.m
% % **************************************************************************************************