#! /usr/gtm-perl/bin/perl # # use PDL; use PDL::Audio; use Storable qw(nstore); use Getopt::Long; #$signal = gen_oscil 2*44100, (2*440*4/2)/44100; my $winsize = 2048; my $stepsize = 2048; my $wintype = HANNING; my $file; my $outfile = "fft.out"; my $sformat = 0; my %wintypes; my @data; my $normalize = 0; eval { %wintypes = map { $_ => 1} (gen_fft_window(2048, LIST)); }; sub usage() { my $types; $types = join ", ", keys %wintypes; print STDERR < \$winsize, "wintype=s", => \$wintype, "stepsize=i" => \$stepsize, "normalize" => \$normalize, "input=s" => \$file, "output=s" => \$outfile, "storable" => \$sformat, ); usage unless $file; -r $file or die "can't access $file: $!"; $wintype = uc $wintype; $wintypes{$wintype} or die "no such type: $wintype"; my $a = PDL::Audio::raudio($file); my $win = PDL::Audio::gen_fft_window($winsize, $wintype, 2.5); my $max = $a->getdim(0); for(my $i = 0;;$i++) { my $start = $i * $stepsize; my $end = $start + $winsize - 1; last if $end >= $max; my $ex = $a->slice("$start:$end"); my $data = PDL::Audio::spectrum $ex, $normalize, $win; push @data, [crunch list $data]; } if($sformat) { nstore \@data, $outfile; } else { open my $fh, ">", $outfile or die "can't create $outfile: $!"; for(my $s = 0;; $s++) { my $l = $data[$s]; last unless $l; print { $fh } join " ", @$l; print { $fh } "\n"; } }