% Stdev filter of data
% requires the number of stdev to be filtered and some input data set
% returns filtered data array 

function [data_filtered,m,v,bad_data]=stdev_filter(data,num,pass);

data=data(:);
m=nanmean(data);
v=variance(data);
stdev=sqrt(variance(data));
bad_data=0;
    for i=1:pass;
        bad=find(abs(data-m)>=num*stdev);
        data(bad)=NaN;
        if (pass==1)
                bad_data=bad;
            else 
                bad_data=cat(1,bad_data,bad);
        end 
        m=nanmean(data); 
        v=variance(data);
        stdev=sqrt(v);
    end
  
  bad_data=bad_data(2:end);
  data_filtered=data;
  