list = dir('*DT1*');
   [b a] = butter(3,[130/625 195/625],'bandpass');
    
for ll = 1:length(list)
   cd(list(ll).name)
   lfp = dir('*lfp');
   xml = dir('*xml');
   SetCurrentSession(xml.name)
   ad = GetLFP(18); ad = ad(:,2);
   for chunk = 1:100000:length(ad)
       freq = scal2frq(140:195,'cgau4',1/1250);
%        c = cwt(ad(chunk:chunk+100000),freq,'cgau4'); 
       w = fastrms(lfp_filt,10); %mean((abs(c.*c))); clear c;
       lfp_filt = (filtfilt(b,a,ad));
       thresh = mean(w)+5*std(w);
       [pks locs] = findpeaks(w,'MINPEAKHEIGHT',thresh);
       allchan = GetLFP('all');
       while locs(1) < 500
           locs(1)=[];
       end
       while locs(end) + 500 > length(w)
          locs(end) = []; 
       end
       for j=1:length(locs)
           for i = 1:32
               plot(i*500+allchan(locs(j)-500:locs(j)+500,i))
               hold on
           end
       plot(20*lfp_filt(locs(j)-500:locs(j)+500),'r');
       hold off
       pause
       end

   end
   cd ..
end