% **************************************************************************************************
%  File: coherent_correlation.m
% 
%  Brief: subor obsahuje algoritmus koherentnej korelacie pre detekciu stredu 
%         interferencnych pruzkov
% 
%  Version: 1.0  Revision: 0.0   Name: peter novotny
% 
%  Date: 2011-01-15
%
%  Matlab version: R2009a
% **************************************************************************************************

% clear all; close all; clc;

% **************************************************************************************************
%  Vytvorenie modelu signalu pre korelaciu - ma rovnaku dlzku(pocet vzoriek) ako originaly signal
%  I_model_corr - je model signalu vytvoreny z parametrov zdroja svetla
%  model_lambda_source -  stredna vlnova dlzka zdroja svetla v [nm] 
%  model_Lc - koherencna dlzka modelu v [nm] 
%  model_int - dlzka intervalu modelu v [nm]
%  model_step - krok merania v modely [nm]
%  model_z - osa z odpovedajuca modelovanemu rozdielu optickych drah
% **************************************************************************************************

model_lambda_source = 460;          % [nm]     
model_Lc = 2500;                    % [nm]         
model_interval = model_Lc + 3000;   % [nm]  
model_step = step;                  % [nm]  
model_z = -model_interval/2:model_step:model_interval/2-model_step; 
model_I = exp(-(2*model_z/model_Lc).^2).*cos(2*pi*model_z/model_lambda_source);
model_N = numel(model_I);

% model treba predlzit na dlzku realneho signalu a vycentrovat
model_I(end+1:N) = 0; % doplnenie nulami
model_posun = round(N/2-model_N/2); % posun potrebny na vystredenie modelu
model_I = circshift(model_I,[0 model_posun]); % vycentrovanie modelu

% **************************************************************************************************
%  Vypocet stredu pruzkov pouzitim korelacie
%  Po korelacii nameraneho a modelovaneho signalu sa najde miesto maximalnej zhody signalov a
%  dopocita sa poloha odpovedajuca tomuto miestu v nameranom signale co odpoveda miestu s OPD=0.
%  correlation - vektor vyslednej korelacie 
%  model_corr_center - stred modelovaneho signalu(OPD = 0) potrebny pre vypocet polohy OPD=0 
%                      v nameranom signale.
% **************************************************************************************************

correlation = xcorr((I_1D), (model_I));
% figure, plot(correlation); grid; title('korelacia');

% zistenie maxima korelacnej funkcie
[max_corr_value,max_corr_position] = max(correlation);

% znalost stredu pruzkov v modely
model_corr_center = round(N/2);

% vypocet stredu pruzkov v realnom signale
% najpv sa zisti index, kotremu sa nasledne priradi odpovedajuca hodnota
corr_fringe_center = round(max_corr_position - model_corr_center);
fringe_center = z(corr_fringe_center);

% **************************************************************************************************
%  Zobrazenie 
% **************************************************************************************************

% figure;
% subplot(3,1,1), plot(z,I_1D), title('Namodelovany interferogram so sumom (realny)'), grid;
% xlabel('Rozdiel dlzky optickych drah [um]'), ylabel('Intenzita [-]'),
% subplot(3,1,2), plot(z,model_I), title('Model interferogramu pre korelaciu'), grid
% xlabel('Rozdiel dlzky optickych drah [um]'), ylabel('Intenzita [-]'),
% z1 = [z (z(1:end-1)+z(end))]; % os pre korelaciu (vzajomne posunutie)
% subplot(3,1,3), plot(z1,correlation), title('Vysledok vzajomnej korelacie experimentalneho a modelovaneho interferogramu'), grid
% xlabel('Vzajomne posunutie signalov k [um]'), ylabel('Korelacny koeficient[-]'),

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