Skip to content

Instantly share code, notes, and snippets.

@encryptio
Created July 27, 2010 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save encryptio/491687 to your computer and use it in GitHub Desktop.
Save encryptio/491687 to your computer and use it in GitHub Desktop.
sub read_curve_from {
my ($fn) = @_;
my $fh;
if ( $fn eq "-" ) {
$fh = \*STDIN;
} else {
open $fh, "<", $fn or die "Couldn't open $fn for reading: $!";
}
return read_curve_string(do { local $/; <$fh> });
}
sub read_curve_string {
my ($str) = @_;
my $samplerate;
my $curve = [];
for my $line ( grep { defined and length } split /[\n\r,]/, $str ) {
if ( $line =~ /SAMPLERATE=(\d+)/ ) {
$samplerate = $1;
}
next if $line =~ /^[#;]/;
$line =~ s/^\s*//;
$line =~ s/\s*$//;
my ($freq, $power) = split /[=\s]+/, $line;
next unless $freq =~ /^\+?\d*(?:\.\d*)?(?:[eE][-+]?\d+)?$/;
next unless $power =~ /^\+?\d*(?:\.\d*)?(?:[eE][-+]?\d+)?$/;
push @$curve, [$freq, $power];
}
return ($curve, $samplerate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment