  function InsertTest( T, p1, pfa, snrdb);
% function InsertTest( T, p1, pfa, snrdb);
% Some examples of indexing and plotting
% H0 x=w
% H1 x= A+w with Prob p1
% Typical Call is  
% InsertTest(300,.3,.01,20)

% Uses Logical Subscripting

% Setup
sqrt2 = sqrt(2);
th = sqrt2*erfcinv(2*pfa);
snr = 10^(snrdb/10);A=sqrt(snr);

%generate signals
t = [1:T]';%col time index
x = randn(T,1);% get w iid N(0,1) noise

Sindx = (rand(T,1) < p1);% Bern(p1) indicating H1 
% LOGICAL variable
L1 = sum(Sindx);% Number of occurences
L1 


% Here are two different models for comparison
x(Sindx) = x(Sindx) + A*sqrt(-log(rand(L1,1)) );% This is models random A
%x(Sindx) = x(Sindx) + A*ones(L1,1);% This is models fixed A

t1 = t(Sindx);t0 = setdiff(t,t1);x1 = x(t1);x0 = x(t0);

%Plotting
%subplot(211),plot( t, x, 'b*');grid;
%subplot(212),plot( t, x.^2, 'b*');grid;% basic plots
figure(1)
subplot(211),plot( t0,x0,'bo',t1,x1,'ro');grid
hold on
h1 = plot( t, th*ones(T,1),'m');
set(h1,'LineWidth',2);
hold off
Title('Amplitude Plot')

subplot(212),semilogy( t0,x0.^2,'bo',t1,x1.^2,'ro');grid
hold on
h2 = semilogy( t, (th^2)*ones(T,1),'m');
set(h2,'LineWidth',2);
hold off
title('Semilog Power Plot')
subplot(111)
figure(1)