Averaging data

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I need to take a set of numbers (anywhere from 1,000 numbers to 10,000
numbers) and graph them with a bar chart. The bar chart can not be
anywhere more than 550 pixels.

Does anyone know of a way to figure out how to best average the data
with the result of an array of 550 entries? I have searched CPAN
without success, not to mention spending an embarrassing amount of time
trying different nested for loop routines only to come "almost close"
to what I am trying to accomplish, surely quite inefficiently.

Any help or suggestions would be greatly appreciated.

This is what I have so far... it skips every third entry of the array
or so... It also prints a lot of output so I can see what is going on.



undef @numbers;

close FILE;

foreach $number(@numbers)
{$dave++;if ($dave<10){print "$number ";}chomp $number;}



print "row count: $rowcount\n";
$factor= sprintf("%.2f", $factor);


print "\nint:$int\n";
print "point:$point\n\n";



#z is final total row count
#w adds temp values by $int
#x corresponds to values in @numbers and should be incremented by one
each time


       print "w: $w\n";
       if ($w==0 && $z>0 && $z%2==1)
         print "$x: $numbers[$x]\n";
         print "add+=$c2*$numbers[$last]\n";

       elsif ($w==0 && $z>0 && $z%2==0)
         print "$x: $numbers[$x]\n";
         print "add+=$c1*$numbers[$last]\n";
      if ($z==0 && $w==0)

    if ($add ne "")
       print "add+=$numbers[$x]\n";
       print "add+=$numbers[$x]\n";

     print "temp+=$add\n";
     undef $add;

print "   z[$z]:  $temp\n";
undef $temp;
#if ($x>25)

print "-------------------\n\n\n\n";


print "final lines: $finallines\n";

foreach $value(@final)
  {$v++;print "$v: $value\n";}

Re: Averaging data

Hi, my first suggestion is:

use strict

My second one is:

check out pdl.perl.org


Re: Averaging data


I did not see anything within PDL that looks like it would immediately
solve my problem. I will keep looking. Ploting it to actual size and
resizing is a curious idea I will also investigate.

Anyone else have any ideas?


Re: Averaging data

Quoted text here. Click to load it

Draw the real thing, and scale it to 550 pixels. Test with and without
anti-aliasing :-)

John                   Small Perl scripts: http://johnbokma.com/perl/
               Perl programmer available:     http://castleamber.com/
                                        I ploink googlegroups.com :-)

Re: Averaging data

Quoted text here. Click to load it

[program snipped]

I made no attempt to understand your program. I would calculate a "bin"
size of the ratio of original data points to your desired "averaged"
data points as a floating-point number. I would then divide the
original data point x-axis values into intervals of this size and
average together all points that fall into the same bin.

Here is an example program that uses 5 bins (instead of 550) and takes
the number of unaveraged points from the command line, with value[i] =
i assumed:

use strict;
use warnings;

my $n = shift;
die("Please enter number of values to average")
  unless $n;

my $m = 5;  # number of bins -- 550 in the real program

$m = $n if( $n <= $m ); # of original pts is less than # of bins

my $delta = $n/$m;
print "bin size = $delta\n";
my( $i, $j, $f ) = (0)x3;
my( @totals, @counts );
while( $j < $m ) {
  $f += $delta;
  while( $i < $f ) {
    $totals[$j] += $i;

# compute averages
print "Bin    Total  Count  Average\n";
my @averages;
for my $j (0..$#totals) {
  if( $counts[$j] > 0 ) {
    $averages[$j] = $totals[$j]/$counts[$j];
    printf "%3d.  %5d  %5d  %8.2f\n", $j,
      $totals[$j], $counts[$j], $averages[$j];
    print "No data in bin $j!\n";

 Posted Via Usenet.com Premium Usenet Newsgroup Services

Re: Averaging data

Quoted text here. Click to load it

use List::Util qw(sum);
my $bunch= @data / 550;
$bunch=1 if $bunch<1;

my @avg;
for (my ($start,$end)=(0,$bunch); $start<@data; $start=$end+1,
$end+=$bunch) {
  $end=$#data if $#data-$end < 1; # protect end of array from roundoff.
  my @data2=@data[$start..$end];
  push @avg , ((sum @data2)/@data2);
  $start = $end+1;


-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB

Site Timeline