%this is a test ti see if a filtered or unfiltered signal would serve
%better for startle detection



Fs=31;
cd /u/fenn/SignalTEST
load testgsr
s=testgsr;
lgsr=length(s);
lgsr2=lgsr/2;
t=(1:lgsr)/Fs;

[b,a]=ellip(4,0.1,40,4*2/Fs);
[H,w]=freqz(b,a,lgsr);
sf=filter(b,a,s);

sf_prime=diff(sf);
sf_vker=filter(1,[-1 8 0 -8 1],sf);

%There is ringing in the signal so the first 35 points are excluded

l35=length(s_prime);

sf_prime35=sf_prime(35:l35);
sf_vker35=sf_vker(35:l35);
endit=l35-35;

upcross_sf=[];
upcross_vker=[];


%the following indicates that there is a significant startle in the area



thresh=0.025;


vector=sf_prime35;

overthresh=find(vector>thresh);


for i=2:endit
  if ((vector(i)>thresh)&(vector(i-1)<=thresh))     

    %this means that the current value is high 
    %and that the one before was not.

    upcross_sf=[upcross_sf i];
  end;
end;

%%%%%%%%%%%%%
%see if this smoothes edges
%%%%%

vector=sf_vker35;

overthresh=find(vector>thresh);

for i=2:endit
  if ((vector(i)>thresh)&(vector(i-1)<=thresh))     

    %this means that the current value is high 
    %and that the one before was not.

    upcross_vker=[upcross_vker i];
  end;
end;





cd /u/fenn/matlab/StartleDetection



upcross_sf=upcross_sf+35;
upcross_vker=upcross_vker+35;


%check the results

figure(1)

plot(sf)
hold on


for i=1:length(upcross_sf)
  plot(upcross_sf(i), s(upcross_sf(i)),'go')
end;



hold off

figure(2)

plot(sf)
hold on


for i=1:length(upcross_vker)
  plot(upcross_vker(i), s(upcross_vker(i)),'go')
end;



hold off






% elf=find(elf>31)    



% plot(268+35,s(268+35),'rx')
% plot(overthresh(137)+35,s(overthresh(137)+35),'rx')
% plot(overthresh(164)+35,s(overthresh(164)+35),'rx')
% plot(overthresh(165)+35,s(overthresh(165)+35),'rx')
%  plot(overthresh(136)+35,s(overthresh(136)+35),'bx')
% plot(overthresh(10)+35,s(overthresh(10)+35),'bx')  
% plot(overthresh(118)+35,s(overthresh(118)+35),'bx')
 
% elf=diff(overthresh)   


