% *************************************************************************************************
%  File: simulacia_merania.m
%
%  Brief: subor obsahuje algoritmus simulacie merania popisany v teorii
% 
%  Version: 1.0  Revision: 0.0   Name: peter novotny
% 
%  Date: 2011-05_20
%
%  Matlab version: R2009a
% *************************************************************************************************

clear all; close all; clc; 

% **************************************************************************************************
% Namodelovanie interferogramu bez sumu "I" a so sumom "I_noise" spolu s osou "z" ktora predstavuje  
% rozdiel optickych drah v [nm] 
% **************************************************************************************************

% parametre pouziteho svetelneho zdroja - biela LED dioda
lambda_source = 460;    % stredna vlnova dlzka v [nm]
Lc = 2500;              % koherencna dlzka v [nm] 

% parametre modelu simulacie merania
interval = 12000;           % dlzka meraneho intervalu v [nm]
stred_pruzkov = interval/2; % stred pruzkov pre model je presne v strede
step = 2;                   % krok simulovaneho merania v [nm]
z = -interval/2:step:interval/2-step;   %[nm] os "z" pre model

% modely interferogramov I a I_noise
I = exp(-(2*z/Lc).^2).*cos(2*pi*z/lambda_source)*0.5 + 1;
N = numel(I);
posun = 1500; % udava pozadovany posun pruzkov od stredu v nm (6000+1500=7500nm)
I = circshift(I,[ 0 posun/step]); % posunutie stredu pruzkov do polohy 7500nm

% vytvorenie a pridanie sumu so SNR = 16 dB => zosilenie K = 0.15
K = 0.15; % zosilenie odpovedajuce SNR = 16dB
noise = randn(1,N); % vygenerovanie bieleho sumu s normalnym rozlozenim
norm_noise = (noise/max(abs(noise)))/2; % normalizacia sumu na rozsah -0.5 az 0.5
noise = norm_noise*K; % nastavenie pozadovanej hodnoty sumu
I_noise = (I + noise); % pridanie sumu k modelovanemu signalu

% posunutie osi "z" do oblasti kladnych hodnot
z = step:step:interval;

% **************************************************************************************************
% Zistenie stredu pruzkov jednotlivymi algoritmami 
% **************************************************************************************************

I_1D = I_noise; % ulozenie do vektoru I_1D ktory je vstupnym interfergramom v algoritmoch
run weight_center;
fringe_center_wc = fringe_center;
run coherent_correlation;
fringe_center_cc = fringe_center;
run frequency_domain_analysis;
fringe_center_fda = fringe_center;

% vypocet rozdielu stredu skutocnych a algoritmami detekovanych pruzkov delta_z
real_center = stred_pruzkov + posun;
delta_z_wc = real_center - fringe_center_wc;
delta_z_cc = real_center - fringe_center_cc;
delta_z_fda = real_center - fringe_center_fda;
delta_z = [delta_z_wc delta_z_cc delta_z_fda];

disp('Vypocitane rozdiely stredu pruzkov delta_z pre jednotlive algoritmy'); 
delta_z_wc 
delta_z_cc 
delta_z_fda

% **************************************************************************************************
%  Zobrazenie modelovanych signalov interferogramov bez sumu a so sumom
% **************************************************************************************************
figure; 
subplot(2,1,1), plot(z,I), grid on,title('Interferogram bez sumu'); 
ylabel('Intenzita [-] \rightarrow'); xlabel('Rozdiel optickich drah [nm] \rightarrow');

subplot(2,1,2), plot(z,I_noise), grid on, title('Interferogram s pridanym sumom'); 
ylabel('Intenzita [-] \rightarrow'); xlabel('Rozdiel optickich drah [nm] \rightarrow');

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