function [rws_filt,snr_filt,beta_filt]=filterrws(lidar,jang,jrad,snr_min,vel_max)
[na,nr]=size(lidar.RWS);
rws=zeros(2*jang+na,2*jrad+nr);
rws(jang+1:na+jang,jrad+1:nr+jrad)=lidar.RWS;
snr=zeros(2*jang+na,2*jrad+nr);
snr(jang+1:na+jang,jrad+1:nr+jrad)=lidar.SNR;
beta=zeros(2*jang+na,2*jrad+nr);
beta(jang+1:na+jang,jrad+1:nr+jrad)=lidar.Beta;
r=lidar.ranges;
az=lidar.azimuth(:,1);
rwss=lidar.RWS;
snrs=lidar.SNR;
betas=lidar.Beta;
for ja=1:na
    for jr=1:nr
        %disp([num2str(ja),'   ',num2str(jr)])
        
        %radial wind
        rwst=rws(ja:ja+2*jang,jr:jr+2*jrad);
        [nat,nrt]=size(rwst);
        rr=reshape(rwst,1,nat*nrt);
        mrr=mean(rr);
        srr=std(rr);
        if abs(rwss(ja,jr)-mrr)>3*srr
            rwss(ja,jr)=mrr;
        end

        %snr
        snrt=snr(ja:ja+2*jang,jr:jr+2*jrad);
        [nat,nrt]=size(snrt);
        rr=reshape(snrt,1,nat*nrt);
        mrr=mean(rr);
        srr=std(rr);
        if abs(snrs(ja,jr)-mrr)>3*srr
            snrs(ja,jr)=mrr;
        end

        %radial wind
        betat=beta(ja:ja+2*jang,jr:jr+2*jrad);
        [nat,nrt]=size(betat);
        rr=reshape(betat,1,nat*nrt);
        mrr=mean(rr);
        srr=std(rr);
        if abs(betas(ja,jr)-mrr)>3*srr
            betas(ja,jr)=mrr;
        end


    end
end

ind=find(abs(rwss)>vel_max);
rwss(ind)=NaN;
betas(ind)=NaN;
snrs(ind)=NaN;
ind=find(lidar.SNR<snr_min);
rwss(ind)=NaN;
betas(ind)=NaN;
snrs(ind)=NaN;

rws_filt=rwss;
snr_filt=snrs;
beta_filt=betas;

return