#!/usr/bin/perl # use List::Util; sub print_help { print STDERR <1 or die "better have at lest 2 steps!!\n"; next; } die "error: \"$p\" is an unknown option\n"; } if($filename[0] eq "-" || $#filename < 0 ){ $file=$tmpfile; `cat > $file`; }else{ $file=$filename[0]; } $_=`head -1 $file`; s/^\s+//; my @temp = split(/\s+/); $tinfile=shift(@temp)+0.; $tbef=$tinfile; $fbef=shift(@temp)+0.; $_=`tail -1 $file`; s/^\s+//; @temp = split(/\s+/); $tfinfile=shift(@temp)+0.; $lines=`awk '/^#/{c++}END{print NR-c}' $file`+0; $lines>2 || die "average_variance_of_slope: No way to work with ",$lines," lines: terminating.\n"; # check input and set defaults: if ($tin eq "unset" || $tin<$tinfile){ $tin=$tinfile; } if ($tfin eq "unset" || $tfin>$tfinfile){ $tfin=$tfinfile; } if($step eq "unset" || $step<2){ $step=List::Util::min 30,$lines-1; } if($noise>0){ print STDERR "average_variance_of_slope: file=",$file," tin=",$tin," tfin=",$tfin," nstep=",$step," ndata=",$lines,"\n"; } $deltat=1.*($tfin-$tin)/$step; #print "QUAA ",$deltat," ",$tfin," ",$tin," ",$step,"\n"; open(INPUT, $file) || die "Cannot open $file\n"; $s1=0; $s2=0; $count=0; for($i=0;$i<$step;$i++){ $ti=$tin+$i*$deltat; $tf=$tin+($i+1)*$deltat; LINE: while(){ s/^\s*//; # remove leading spaces if(!/^#/){ # ignore commented lines @li = split('\s+'); $t=$li[0]+0.; $f=$li[1]+0.; if($t<=$ti){ $tbef=$t; $fbef=$f; } last LINE if $t>$tf; $taft=$t; $faft=$f; # print "QUUU ",$t," ",$tbef," ",$taft,"\n"; } } if($taft>$tbef){ $val=($faft-$fbef)/($taft-$tbef); # print "QUII ",$deltat," ",$tbef," ",$taft," ",$fbef," ",$faft," ",$val,"\n"; $s1+=$val; $s2+=$val*$val; $tbef=$taft; $fbef=$faft; $taft=$t; $faft=$f; $count++; } # print "QUXX ",$count," ",$i," ",$ti," ",$tf," ",$t,"\n"; } $mean=$s1/List::Util::max 1,$count; $diff=List::Util::max 0,$count*$s2 - $s1*$s1; $std=sqrt($diff)/$count; $sem=$std/sqrt($count); # standard error on the mean # sem is an estimator of the standard deviation on the final mean, # not on a measurament of an individual signal segment!!! print $mean,"\t",$sem,"\n"; if (-e $tmpfile){ `rm $tmpfile`; }