function [] = webdata()
%close all
data=load('webdata.txt');

%we know it is 10 seconds between each sample
l=length(data);
t=[1:l]*10; %time in seconds
t=t/3600;  %time in hours

%zero mean
data=data./max(data);
data=data-mean(data);


fs=1/10;


%start filtering
filt_data=data;

%lp averaging filter
if 1==1
  k2=6*5; %smooth to 5 minute
  b=ones(1,k2);
  a=k2;
  filt_data = filtfilt(b,a,filt_data);
end

%IIR hp filter
if 1==1
  %cutoff is .001 * half sample rate
  w=.001;
  cutoff=w*fs/2;  %cutoff in hz
  disp(sprintf('cutoff in seconds %4.4f',1/cutoff));
  [b,a]=butter(5,w,'high');   %very nice!
  filt_data=filtfilt(b,a,filt_data);
  %figure
  %freqz(b,a,512,fs);
end

%hp filter but too crude
%if 1==0
%  f=fft(filt_data);
%  k=10;
%  f(1:k)=0;
%  f(end-k+1:end)=0;
%  filt_data=abs(ifft(f));
%end



figure 
plot(t,data)
xlabel('time')
ylabel('hits')

pause



figure
subplot(211)
plot(t,data,'r')
hold on
plot(t,filt_data,'b')
xlabel('hours')
ylabel('load')


subplot(212)
%scale filt_data to plot nicely
plotfilt=filt_data./max(filt_data) *5;
plotfilt=plotfilt-max(plotfilt); 
plot(t,plotfilt,'r')
hold on

%Edge Detection using different thresholds
%triple sig to get around minimum size of filter in image proc tollbox
%if this works we can implement our own 1d edge detector

if 1==1
  fd=[filt_data,filt_data,filt_data];
  %note that we still need the filtering 
  %this does not work: fd=[data,data,data];
  th=linspace(.1,.8,10);
  for i = 1:length(th)
    can=edge(fd,'canny',th(i));
    hits=find(can(:,2));
    plot(t(hits),ones(size(hits))+i,'b.');
    hold on
  end
  xlabel('edge detector output with different thresholds (each row a threshold)')
end


