Subject: | signal size 65536 |
# Following code results in a spectrum with
# negative values only on my machine:
#
# $ perl -v
# This is perl, v5.8.5 built for i386-linux-thread-multi
#
# $ uname -a
# Linux 2.6.9-34.0.1.ELsmp #1 SMP Wed May 17 17:05:24 EDT 2006
# (RedHat 4)
# ------------------------------------------------------------
#!/usr/bin/perl -w
use Math::FFT;
my $PI = 3.1415926539;
# my $N = 32768; <-- That works fine
my $N = 2 * 32768; # <-- That produces negative spectrum values
my $fs = 500;
my @dat = ();
for( my $k=0; $k<$N; $k++ )
{
$dat[ $k ] = sin( 2*$PI*$k/$fs)
+ sin( 3*$PI*$k/$fs)
+ sin( 4*$PI*$k/$fs);
}
my $fft = new Math::FFT(\@dat);
my $spectrum = $fft->spctrm;
open SPECTRUM, ">tmp.dat";
for (my $k=0; $k<$N/2+1; $k++)
{
my $x = $k/$N*$fs;
my $y = $spectrum->[$k];
print SPECTRUM $x." ".$y."\n";
}
close SPECTRUM;