#!/usr/bin/perl use strict; use warnings; my $SMALL=1.e-12; # threshold to decide if 3 points are equally spaced my $HUGE=1.2313e200; my $nsample=10; sub print_help { print STDERR <0) || die "error! running_average requires nsample > 0\n"; next; } if($p eq "-s"){ $skip=1; next; } if($p eq "-h"){# [-h] print help print_help; exit; } print_help; print STDERR "invalid option $p!\n "; exit; } if($#inputfiles<0){ push(@inputfiles,"-"); } while (my $filename = shift(@inputfiles)){ my $xsum=0; my $ysum=0; my $xprev=$HUGE; my $deltaxprev=$HUGE; my $deltax=$HUGE; open(INF, $filename) || die "Cannot open file $filename\n"; while (){ if($_ !~/^\#/ && $_ !~/^\s*$/){ # ignore commented and empty lines s/^\s+//; my @fi=split(/\s+/); my $xnew=shift(@fi)+0.; my $ynew=shift(@fi)+0.; # print "debug: ",$#xx," ",$xx[$#xx]," ",$xnew,"\n"; if($xprev==$HUGE){ $xprev=$xnew; }else{ $deltax=$xnew-$xprev; if($deltaxprev==$HUGE){ $deltaxprev=$deltax; }else{ if(abs($deltax-$deltaxprev)>$SMALL){ die "running_average needs equally spaced data: $deltax $deltaxprev\n"; } } } $xprev=$xnew; push(@xx,$xnew); # remember the x's push(@yy,$ynew); # remember the y's $xsum+=$xnew; $ysum+=$ynew; # print "#QUIII ",$#xx," ",$nsample," ",$xnew," ",$ynew," ",$xsum," ",$ysum,"\n"; if($#xx==$nsample-1){ print $xsum/$nsample," ",$ysum/$nsample,"\n"; if($skip){ @xx=(); @yy=(); $xsum=0; $ysum=0; }else{ my $xold=shift(@xx); my $yold=shift(@yy); $xsum-=$xold; $ysum-=$yold; } } } } close(INF); }