Averaging data

Averaging data

am 28.09.2005 06:47:20 von dszostek

Hi,

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.

Thanks,
Dave
----------------------
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.


#!/usr/bin/perl

$maxrows=550;

undef @numbers;
$ps="";

open(FILE,"numbers.txt");
@numbers=;
close FILE;

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

$rowcount=@numbers;

$factor=$rowcount/$maxrows;

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

($int,$point)=split(/\./,$factor);

$point=$point/100;
print "\nint:$int\n";
print "point:$point\n\n";

$w=0;
$x=0;
$y=0;

$c1=$point;
$c2=1-$point;

#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

for($z=0;$z<$maxrows;$z++)
{

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

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

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

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

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

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

$finallines=@final;

print "final lines: $finallines\n";

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

Re: Averaging data

am 28.09.2005 08:22:56 von John Bokma

"Dave" wrote:

> Hi,
>
> 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.

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

am 28.09.2005 11:49:33 von metaperl

Hi, my first suggestion is:

use strict

My second one is:

check out pdl.perl.org

Best,

Re: Averaging data

am 28.09.2005 17:46:32 von dszostek

Hi,

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?

Thanks,
Dave

Re: Averaging data

am 29.09.2005 01:40:37 von Jim Gibson

In article <1127882840.240543.242490@f14g2000cwb.googlegroups.com>,
Dave wrote:

> Hi,
>
> 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.
>
> Thanks,
> Dave
> ----------------------
> 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.

[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:


#!/usr/local/bin/perl
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;
$counts[$j]++;
$i++;
}
$j++;
}

# 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];
}else{
print "No data in bin $j!\n";
}
}

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Re: Averaging data

am 29.09.2005 16:36:26 von xhoster

"Dave" wrote:
> Hi,
>
> 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.

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;
};

Xho

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