Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving for Amplitude and Frequency in WAV files

I've also asked this here on the Sound Design forum, but the question is heavy computer science/math so it might actually belong on this forum:

So I'm able to successfully find all the information about a WAV file except the amplitude and the frequency (hertz) of the big sin function by reading the binaries in the file (which are unfortunately exactly what I'm looking for). Just to verify what I'm talking about, the file generates one wave only with the equation:

F(s) = A * sin(T * s)

Where s is the current sample, A is the amplitude and T is the period. Now the equation for the T (period) is:

T = (2π * Hz) /(α * ω)

Where Hz is frequency in Hertz, α is Samples per second, and ω is the amount of channels.

Now I know that to solve for amplitude, I could simply find the value of F(s) where

s = (π/2)/T

Because then the value of the sine function would be 1, and the final value would be equivalent to A. The problem is that to divide by T, I have to know the Hertz (or Hz).

Is there any way that I can read a WAV file to discover the Hertz from the data, assuming the file only contains a single wave.

like image 647
Nas Kas Avatar asked Nov 08 '22 20:11

Nas Kas


1 Answers

Just to get some terms clarified, the property you're looking for is frequency, and the unit of frequency is Hertz (once per second). By convention, the typical A note has a frequency of 440 Hz.

You got the function wrong, actually. That sine wave in reality has the form F(s) = A * sin(2*pi*s/T + c) - you don't know when it started so you get a constant c in there. Also, you need to divide by T, not multiply.

Getting the amplitude is actually fairly easy. That sine wave has a series op peaks and valleys. Find each peak (higher than both neighbors) and each valley (lower), calculate the average peak and average valley, and the amplitude is TWICE the difference between the two. Pretty easy. The period T can be estimated by counting the average distance from peak to peak, and from valley to valley.

There's one bit where you need to be careful. If there is a slight bit of noise, you may get a slight dent near a peak. Instead of 14 17 18 17 14 you may get 14 17 16 17 14. That 16 isn't a valley. Once you've got a good estimate for the real peaks and valleys, throw out all the distorted peaks.

like image 154
MSalters Avatar answered Nov 14 '22 23:11

MSalters