%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(testgsr);
lgsr2=lgsr/2;
t=(1:lgsr)/Fs;

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

s_prime=diff(s);
sf_prime=diff(sf);

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

l35=length(s_prime);
s_prime35=s_prime(35:l35);
sf_prime35=sf_prime(35:l35);

endit=l35-35;

upcross_s=[];
upcross_sf=[];


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

vector=s_prime35;

thresh=0.025;

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_s=[upcross_s i];
  end;
end;


vector=sf_prime35;

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;





cd /u/fenn/matlab/StartleDetection


upcross_s=upcross_s+35;
upcross_sf=upcross_sf+35;


%check the results

figure(1)

plot(testgsr)
hold on

for i=1:length(upcross_s)
  plot(upcross_s(i), testgsr(upcross_s(i)),'rx')
end;

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



hold off








