What you are computing is not the amplitude, but the wrong mean value. The "wrong" in mean value is because a sound signal is supposed to be variations around 0, which is not possible with your signed byte values. You'll have to find out how your values are actually coded, either with an offset (eg: substract 128 to every value), or as signed values. It's even possible that the values are not linear representations of the data, but rather logarithmic, as in A-law encoding. AFAIK, you can indeed expect one sample per byte. To see if you're decoding your input correctly, you should try to play a sinusoid beside your microphone (I'm sure you can find somewhere on the internet to have the reference A 440), and see if you manage to plot something that looks like a sinusoid centered on 0.

Now, as I've already told you, the mean value is supposed to be 0 and is therefore useless. The power of a signal ("sound level" if you will) is only a function of its amplitude when the signal is periodic, preferably a sinusoid (a continuous beep). Otherwise you want to compute:

my $sum = 0; my $length = @input; # number of samples in the array @input for my $value (@input) # assuming @input are samples that have already + been converted to the correct unit { my $sum += $value*value; # sum of the squares } my $dba = 10 * log10($sum/$length);

Note that the factor before the log10 is now 10 and not 20, because the input is squared. Besides, unless you have some way of calibrating your microphone to be able to tell exactly the power of its input, you'll only be able to use the values relative to each other. Like saying "this sound is 3dB higher than that one" but not "that sound is at 50dB".


In reply to Re: Measuring the sound level (dB(A)) with PERL by Eily
in thread Measuring the sound level (dB(A)) with PERL by John-Robie

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.