The module itself is fairly easy to use, but I suspect your main difficulty will be capturing and decoding the audio stream. If it's an au file, easy, but some broadcast bitstream is more difficult. That would involve capturing the sample from whatever buffer it is collected in. Without knowing the form your data takes, I'll assume something simple ;-)

The sox utility would be helpful for translating the various audio formats to the one you write for. Since you are interested in dominant frequencies, you can probably get away with a low fidelity monaural format with signed amplitudes, as opposed to PCM or some logarithmic encoding. Given a string containing 16-bit signed amplitude data read from a file or piped from sox, unpack will decode to an array. The Math::FFT object can be constructed with a reference to that array:

use Math::FFT my @samples; { local $/=\2048; # about 1/5 or 1/10 of a second at 11025 or 22050 sps # pick longer samples to be able to see lower frequencies binmode; @samples = <>; } # lets look at about 10 or 20 seconds in # should check if the sample exists in production code my $auft = Math::FFT->new([unpack 's*', $samples[100]]); my $power_spectrum = $auft->spctrm( [window => 'hamm'] ); my $dominant = (sort {$power_spectrum->[$a] <=> $power_spectrum->[$b]} + 0..$#power_spectrum)[-1];
...and so on. Warning, untested code, and it will certainly need rewriting to fit your needs. The window parameter is used to filter out spurious high and low frequency components, I suggest looking at the power spectrum instead of the transformed amplitudes because the dominant peaks are more distinct.

PDL::FFT, PDL::FFTW, Math::Pari are among other possibilities.

After Compline,
Zaxo


In reply to Re: Using Math::FFT by Zaxo
in thread Using Math::FFT by katzuma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.