# Frequency calculation using fourier transform [closed]

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 BlassAug 1 '12 at 1:29

Questions 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.

By the way, this has nothing (as far as I know) to do with Frechet manifolds. I'm going to retag. – David Speyer Jul 31 '12 at 14:48
I really don't see what this question has to do with Math Overflow (it is a purely engineering question), so am voting to close. – Igor Rivin Jul 31 '12 at 20:14
Although you ask about Fourier methods (and that's how I would have thought of doing it) I just read this beautiful alternative approach by Daniel Lichtblau mathematica.stackexchange.com/a/5873/1200 – David Speyer Aug 14 '12 at 21:03

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^{-(s-t)^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.

-
Interesting answer. But why do these electronica genres have to be termed 'obscure'? And then I think Moby is quite well-known, and famously has an 1000bpm track. – user9072 Jul 31 '12 at 15:09
@quid: I believe that "obscure" = "not frequently listened to by David Speyer". This is actually not a bad definition, since @David is probably not feeling confident discussing them, not being an expert. – Igor Rivin Aug 1 '12 at 2:40