How to calculate the frequency of an audio file using Fourier Transform
closed as off topic by Will Sawin, Igor Rivin, Noah Stein, Yemon Choi, Andreas Blass Aug 1 '12 at 1:29Questions on MathOverflow are expected to relate to research level mathematics within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question. 


I suspect this is going to get closed because it is asked in a very unsophisticated way, and because a full answer would be a book. But I think that questions about this kind of applied issue are worthwhile and I'd like to be helpful. As you'll see below, there are a lot of issues here which aren't mathematical. I am very far from an expert but: Do you actually have a file which sounds like a single tone, or do you have something that sounds more like a piece of music want to determine how the tone is changing in time? I'll start with the first case, but then talk about the second. First of all, I'm going to assume that your file is equivalent to a sequence of real numbers, giving the amplitude $a(t)$ of your signal at various times. This may be very far from true; MP3's, for example, use a very complicated encoding system. Of the standard audio formats, the one where this is closest to true is WAV, which uses LPCM; see the linked Wikipedia article for more information on how LPCM works. The Fourier transform, of course, is $$F(\omega) = \int_{t} a(t) \cos(\omega t) dt.$$ This function will have peaks at the overtones of your sound file, with the largest peak at the dominant tone. So, roughly, you want to compute $F(\omega)$ and maximize it. Roughly speaking, you want to approximate the integral by a Riemman sum and then search for the value of $\omega$ which makes it largest. If you for some reason are writing these routines yourself, get a book on numerical methods and read up on (a) numerical integration and (b) numerical optimization in one dimension. If this is not a project for a class, a much better idea is to find someone who has already implemented these for you. I don't have enough experience to recommend a particular package and that isn't on topic for the site anyway. Now, what if you actually have a file whose tone changes over time, and you want to plot the changing tone? Then you need to know about Fourier windowing. This is, again, a big subject. I'll tell you one approach (Gaussian windowing). Choose a time interval $\delta$ which is much shorter than the time frame that the notes change and much longer than period of the actual sound waves. There's plenty of room here  the fastest music (not counting some obscure electronica genres) runs at something like 350 beats per minute, or 6 hz; the lowest notes on a piano are around 30 hz. Set $$F_{\delta}(s, \omega) = \int a(t) \cos(\omega t) e^{(st)^2/\delta^2} dt.$$ Then, at time $s$, the function $F_{\delta}(s, \omega)$ will have peaks at the values of $\omega$ which are most dominant at time $s$. By the way, I learned a lot of this in the course of editing these puzzles. I'll also add a very practical suggestion: There is a lot of open source sound editing software out there; I've heard good things about Audacity. Some of it might already do this for you, or be easily hacked to do so. Questions about the internals of these programs are, of course, not on topic at MO. 

