function energy_demo(noise_amount)
%function energy_demo(noise_amount)
%
% ENERGY_DEMO -- A demonstration of the robustness of 
%                correlation detection
%
% Input Parameters:
%   noise_amount -- noise power to use; values between .1 and 2 work well
%
% Output Parameters:
%   none
%

fontsize = 16;

signal = hanning(99);
no_signal = zeros(size(signal));

c = zeros(2,1000);
e = sum(signal.^2);

figure(1);
subplot(222);
cla; xlabel('');
subplot(224);
cla; xlabel('');

for i = 1:10
    n1 = noise_amount*randn(size(signal));
    n2 = noise_amount*randn(size(signal));

    subplot(221);
    plot(n1);
    ylabel('No Signal','FontSize',fontsize);
    c(1,i) = sum(n1.^2);
    lbl = ['Energy: ' num2str(c(1,i))];
    xlabel(lbl,'FontSize',fontsize);
    
    subplot(223);
    plot(signal + n2);
    ylabel('Signal','FontSize',fontsize);
    c(2,i) = sum((signal + n1).^2);
    lbl = ['Energy: ' num2str(c(2,i))];
    xlabel(lbl,'FontSize',fontsize);

    pause(.5);
end

for i=11:1000
    n1 = noise_amount*randn(size(signal));
    n2 = noise_amount*randn(size(signal));
    c(1,i) = sum(n1.^2);
    c(2,i) = sum((signal + n1).^2);
end

th = mean(mean(c'));

subplot(222)
hist(c(1,:),50);
a = axis;
line([th, th],[0,a(end)],'Color',[1 0 0]);
lbl = ['False alarm rate: ' num2str(sum(c(1,:) > th)/length(c))];
xlabel(lbl,'FontSize',fontsize);

subplot(224)
hist(c(2,:),50);
a = axis;
line([th, th],[0,a(end)],'Color',[1 0 0]);
lbl = ['Misse rate: ' num2str(sum(c(2,:) < th)/length(c))];
xlabel(lbl,'FontSize',fontsize);
