function [bdata,nbad,nfixed,delmean,delvar,stdr]=... deglitch(bdata,nsd,ndt,iplot); % Identify and remove suspicious points from ADV data % [bdata,nbad,nfixed,delmean,delvar,stdr]=deglitch(bdata,nsd,ndt,iplot); % % Flag and deglitch ADV data. % Try using: % nsd = 2.8; % num std. deviations that defines outliers % ndt = 6; % cutoff frequency % min_corr = 80; % min_amp = 140; % Last revised 5/12/03 on desktop by Chris Sherwood, USGS % number of points n = bdata.sampleperburst; u = bdata.Vx{:}; v = bdata.Vy{:}; w = bdata.Vz{:}; dt = 1 ./ (bdata.samprate); % bad test - sticks out from smoothed time series % low-pass filter (ends not treated well) uf=lpfilt(u,dt,1/(ndt*dt)); vf=lpfilt(v,dt,1/(ndt*dt)); wf=lpfilt(w,dt,1/(ndt*dt)); X = (1:n)'; % other criteria min_amp = 120; min_corr = 65; a = bdata.amp{1}; c = bdata.corr{1}; if(iplot), figure(1) clf vBins = [-100:1:100]; [Nu, vBins]= hist(u(:),vBins); [Nv, vBins]= hist(v(:),vBins); [Nw, vBins]= hist(w(:),vBins); umean = mean(u); ustd = std(u); vmean = mean(v); vstd = std(v); wmean = mean(w); wstd = std(w); subplot(331) bar(vBins,100*Nu./n); axis([-100.5 100.5 0 20]) set(gca,'XTick',[-100:25:100]) ylabel('Percent') hold on; plot([umean,umean],[0 20],'-r') plot([umean+nsd*ustd,umean+nsd*ustd],[0 20],'--r') plot([umean-nsd*ustd,umean-nsd*ustd],[0 20],'--r') subplot(334) bar(vBins,100*Nv./n); axis([-100.5 100.5 0 20]) set(gca,'XTick',[-100:25:100]) hold on plot([vmean,vmean],[0 20],'-r') plot([vmean+nsd*vstd,vmean+nsd*vstd],[0 20],'--r') plot([vmean-nsd*vstd,vmean-nsd*vstd],[0 20],'--r') ylabel('Percent') subplot(337) bar(vBins,100*Nw./n); axis([-50.5 50.5 0 20]) set(gca,'XTick',[-50:10:50]) hold on plot([wmean,wmean],[0 20],'-r') plot([wmean+nsd*wstd,wmean+nsd*wstd],[0 20],'--r') plot([wmean-nsd*wstd,wmean-nsd*wstd],[0 20],'--r') ylabel('Percent') xlabel('Velocity') % Correlation histograms cBins = [0:1:100]; worst_corr = min(min(c)) [Nc1, cBins]= hist(c(:,1),cBins); [Nc2, cBins]= hist(c(:,2),cBins); [Nc3, cBins]= hist(c(:,3),cBins); subplot(332) bar(cBins,100*Nc1./n); axis([.5 100.5 0 20]) set(gca,'XTick',[0:20:100]) ylabel('Percent') hold on plot([min_corr,min_corr],[0 20],'-r') subplot(335) bar(cBins,100*Nc2./n); axis([.5 100.5 0 20]) set(gca,'XTick',[0:20:100]) ylabel('Percent') hold on plot([min_corr,min_corr],[0 20],'-r') subplot(338) bar(cBins,100*Nc3./n); axis([.5 100.5 0 20]) set(gca,'XTick',[0:20:100]) ylabel('Percent') hold on plot([min_corr,min_corr],[0 20],'-r') xlabel('Correlation') % Amplitude histograms aBins = [0:5:250]; worst_amp = min(min(a)) [Na1, aBins]= hist(a(:,1),aBins); [Na2, aBins]= hist(a(:,2),aBins); [Na3, aBins]= hist(a(:,3),aBins); subplot(333) bar(aBins,100*Na1./n); axis([.5 250.5 0 20]) set(gca,'XTick',[0:50:250]) hold on plot([min_amp,min_amp],[0 20],'-r') subplot(336) bar(aBins,100*Na2./n); axis([.5 250.5 0 20]) set(gca,'XTick',[0:50:250]) hold on plot([min_amp,min_amp],[0 20],'-r') subplot(339) bar(aBins,100*Na3./n); axis([.5 250.5 0 20]) set(gca,'XTick',[0:50:250]) hold on plot([min_amp,min_amp],[0 20],'-r') xlabel('Amplitude') drawnow; end nfixed = 0; ur = u; vr = v; wr = w; warning off MATLAB:polyfit:RepeatedPointsOrRescale; uresid = u-uf; vresid = v-vf; wresid = w-wf; stdur=std(uresid); stdvr=std(vresid); stdwr=std(wresid); stdr = [stdur,stdvr,stdwr]; fprintf(1,'std devs of residuals %g %g %g\n',stdr); % Could use other flags to find replacement points, % but I think the low-pass filter works pretty well bad = ( (abs(uresid) > (nsd*stdur)) | ( abs(vresid) > (nsd*stdvr) )); bad_corr = ( (c(:,1)< min_corr)|(c(:,2)(7) & rep(kk)