#!/usr/local/bin/perl # histor.p min=0 max=6 out="h/s10" s10 # histor.p min=0 max=6 out="h/s20" s20 # histor.p min=0 max=6 out="h/s40" s40 # histor.p min=0 max=6 out="h/s60" s60 # histor.p min=0 max=6 out="h/s60" s60 # produces one histogram for every row. # the histogram is labelled by the value of the first element on the row. $min=-1; $max=1; $bins=50; $c=0; $col=-1; $scale=1; $out=""; $l=0; # whether to log transform. # How to override min and max values with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; if ( $col != -1 ) { $c = $col ; } # alternative command line arg if ( !$printall ) { if ($c==0 ) { # $printall=1; } else { $printall=0; $c--; } # } $width=($max-$min)/$bins; printf STDERR "min = %5f, max = %5f, bins = %d. gnuplot\n", $min, $max, $bins; while (<>) { $offset=0.05; $line = 0; $truemax = - 1e200 ; $truemin = 1e200 ; $count = 0.0; $cum=0.0; $sum=0.0; for ( $i = 0 ; $i <= $bins ; $i ++ ) { $countarr[$i] = 0 ; } $line++ ; @fields = split(/[\s\,\t]+/,$_); # was split ( ' ' , $_ ) ; # print "$line :", $#fields , "\n" ; if( $printall ) { $c=1; } # omits the first item on each line if($#fields >= $c) { # do { $x = $fields[$c]; if ( $l > 0 ) { $x = log ( $x ) ; } if ( $x > $truemax ) { $truemax = $x ; } if ( $x < $truemin ) { $truemin = $x ; } # $cum += $x * $x ; $sum += $x ; $count++ ; $i = (($x-$min)/$width) ; if( $i < 0 ) { $i = 0 ; } elsif ( $i > $bins ) { $i = $bins ; } $countarr[$i]++; } while ( $printall && ( $c++ < $#fields) ) ; $label = $fields[0] ; $file = $out.".".$label ; open ( OUT , ">$file" ) ; printf STDERR "replot '$file' u 1:2 w linesp " ; if ( $curvetoo ) { print STDERR ", '$file' u 1:3 w l\n"; } else { print "\n" ; } $xbar = $sum/$count ; $var = ($cum/$count - $xbar*$xbar) ; $sd = sqrt ( $count * $var / ($count-1.0) ) ; $sdz = sqrt( $cum/$count ) ; # sd around mu = zero if ( $scale != 1 ) { $count *= $scale ; } printf OUT "# count = %d, mu = %5f, sd about zero = %5f, sd = %5f, range = %f,%f\n", $count, $xbar, $sdz, $sd , $truemin , $truemax ; for ( $i = 0 ; $i <= $bins ; $i ++ ) { if ( $scale != 1 ) { $countarr[$i] *= $scale ; } $x = $min + ( $i + 0.5 ) * $width ; printf OUT "%f %f %f %f\n",$x,$countarr[$i]+$offset,&gauss($x-$xbar,$sd)*$count*$width,&gauss($x,$sdz)*$count*$width ; } close ( OUT ) ; } elsif ( $c > 0 ) { printf STDERR "Skipping line %d - %d items %d col\n", $line , $#fields+1 , $c+1 ; } } sub gauss { #usage ( $x-$mu , $sd ) local( $x , $sd ) = @_ ; local($stpi) = sqrt(2*3.1415) ; return ( exp ( - $x*$x/(2*$sd*$sd) ) / ( $stpi * $sd ) ) ; }