% ECE 248, Kalman and Adaptive Filtering, Fall 2010, UCSB
% written by: Katie Byl, Sep 26, 2010
% edit list: 
%    Oct. 10, 2010 - minor changes, to post on the web
%
% Description:
%
% If we have two different measurements (or guesses) for a
% single value, we can combine their individual PDF's to produce
% a "joint" PDF that weights each of their MEANS by 1/var (for
% each PDF) and reduces the VARIANCE such that:
%   (1/var_tot) = (1/var_1) + (1/var_2)
% Here, we set the two initial Gaussians to be related to their
% combined (joint) PDF via the 3-4-5 triangle ratios.
% (The new PDF has a standard dev. of (3/5)*.4 = (4/5)*.3 = .24.)

figure(6); clf

dx = .001;
x = [-10:dx:10];  % vector for plotting PDF's

y1 = normpdf(x,0,.4);  % A Gaussian normal distribution function
y2 = normpdf(x,2.5,.3);
y3 = y1.*y2;  % This joint probability still needs to be "rescaled" later...
plot(x,y1)
hold on
axis([-2 4 0 2])
grid on
set(gca,'YTick',[0:1/3:2])
plot(x,y2,'r')
title('What is the joint PDF of two Gaussians???')

A = axis;
tt = text(.9*A(1)+.1*A(2),.1*A(3)+.9*A(4),'Hit enter to continue...');
input('Hit enter to continue...')
set(tt,'String',' ')
%plot(x,y1.*y2,'k')
plot(x,y3.*((1/dx)/sum(y3)),'m-')  % rescale, to total probability is 1.
y3a = y3.*((1/dx)/sum(y3));
set(gca,'XTick',[-2 -1 0 1 1.6 2 2.5 3 4])


title('What is the joint PDF?? Just a re-scaled Gaussian...')
A = axis;
set(tt,'String','Hit enter AGAIN to continue...');
input('Hit ENTER to compare to a warped version of the blue curve, 3/5 wide, 5/3 tall...')
set(tt,'String',' ')
fprintf('Note that AMPLITUDES are related via a 3-4-5 triangle ((5/3)^2 = (3/3)^2 + (4/3)^2),\n')
fprintf('and the STANDARD DEVIATIONS are inversely related to amplitude (to preserve unity area).\n')
plot((3/5)*x+1.6,(5/3)*y1,'k--')