%%% Arquivo: exemplo_11_2.m
%%% Autor: G. A. Borges
%%% Objetivo: fusão sensorial usando o filtro de Kalman.
echo off; clear all; close all; format long;

addpath('..\support');

FlagImprimir = 0;

N = 1000;
x = linspace(-9,9,N)';

var1 = 1.0; mean1 = 2.0;
f1 = 1/sqrt(2*pi*var1)*exp(-(x-mean1).^2/(2*var1));

var2 = 2.0; mean2 = 3.0;
f2 = 1/sqrt(2*pi*var2)*exp(-(x-mean2).^2/(2*var2));

var3 = var1*var2/(var2+var2); % Fusão.
mean3 = mean1*var2/(var1+var2) + mean2*var1/(var1+var2);
f3 = 1/sqrt(2*pi*var3)*exp(-(x-mean3).^2/(2*var3));

%%%%%%%%%%%%%%% probability density function:
figure; 
ax = [-9 9 -0.03 1/sqrt(2*pi*var3)+0.1];

h1 = plot(x,f1,'r-','linewidth',1); hold on; set(gca,'DrawMode','fast');
h2 = plot(x,f2,'b-','linewidth',1);
h3 = plot(x,f3,'g-','linewidth',1);

[m,n] = min(abs(x-mean1));
h = line([mean1, mean1],[0, f1(n)]); set(h,'Color','r','LineStyle',':');
h = line([mean1, 0],[f1(n), f1(n)]); set(h,'Color','r','LineStyle',':');
text(0.55415617128463,0.22319157523709,'p_{a}','HorizontalAlignment','center','VerticalAlignment','bottom');

[m,n] = min(abs(x-mean2));
h = line([mean2, mean2],[0, f2(n)]); set(h,'Color','b','LineStyle',':');
h = line([mean2, 0],[f2(n), f2(n)]); set(h,'Color','b','LineStyle',':');
text(4.63476070528967,0.18323222429974,'p_{b}','HorizontalAlignment','center','VerticalAlignment','bottom');

[m,n] = min(abs(x-mean3));
h = line([mean3, mean3],[0, f3(n)]); set(h,'Color','g','LineStyle',':');
h = line([mean3, 0],[f3(n), f3(n)]); set(h,'Color','g','LineStyle',':');
text(2.92191435768262,0.53287654500149,'p_{c}','HorizontalAlignment','center','VerticalAlignment','bottom');

axis(ax + [-1.0 1.0 -0.05 0.05]);
set(gca,'PlotBoxAspectRatio',[1 0.4 1]);
arrow([ax(1) 0],[ax(2) 0],7,'LineStyle','-','Color','k','BaseAngle',50,'TipAngle',15);
arrow([0 ax(3)],[0 ax(4)],7,'LineStyle','-','Color','k','BaseAngle',50,'TipAngle',15);

set(gcf,'Color',[1 1 1]);
set(gca,'FontSize',8);


if FlagImprimir
%   disp('Posicione as legendas e pressione alguma tecla para imprimir');
%   pause;
   imprimirtudo('figexemplo_11_2',1);
end
