Search code examples
vb.netaudioaudio-analysis

Mathematical analysis of a sound sample (as an array of numbers)


I need to find the frequency of a sample, stored (in vb) as an array of byte. Sample is a sine wave, known frequency, so I can check), but the numbers are a bit odd, and my maths-foo is weak. Full range of values 0-255. 99% of numbers are in range 235 to 245, but there are some outliers down to 0 and 1, and up to 255 in the remaining 1%. How do I normalise this to remove outliers, (calculating the 235-245 interval as it may change with different samples), and how do I then calculate zero-crossings to get the frequency? Apologies if this description is rubbish!


Solution

  • The FFT is probably the best answer, but if you really want to do it by your method, try this:

    To normalize, first make a histogram to count how many occurrances of each value from 0 to 255. Then throw out X percent of the values from each end with something like:

    for (i=lower=0;i< N*(X/100); lower++)
      i+=count[lower];
    //repeat in other direction for upper
    

    Now normalize with

    A[i] = 255*(A[i]-lower)/(upper-lower)-128
    

    Throw away results outside the -128..127 range.

    Now you can count zero crossings. To make sure you are not fooled by noise, you might want to keep track of the slope over the last several points, and only count crossings when the average slope is going the right way.