Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Does anyone know how to get the total intensity of a certain frequency range in an audio file?

Let's say an audio file has a frequency range from 0Hz to 20KHz, and I want to extract the total intensity of every 100Hz, then I can have the intensity of 0 to 100Hz, 100 to 200Hz....etc.

share|improve this question
1  
What have you tried? Do you have a minimal example? Is your question mainly about the signal processing part of it or Mathematica part of it? –  rm -rf Feb 19 '13 at 23:46
    
I am very new to mathematica, I try to get the data from the spectrogram but I haven't figured out how to do it and I can't find any references or examples of it. –  Ting Lee Feb 19 '13 at 23:54
    
An example will be to make an intensity versus frequency plot of a whole song. Need helps!!! –  Ting Lee Feb 20 '13 at 0:03
    
Have you imported the audio file into Mathematica? –  Joel Klein Feb 20 '13 at 0:06
1  
Did you read the documentation page on Fourier? It's the discrete Fourier transform. I sympathize with this, because while I took courses that covered Fourier analysis, I never took a course with discrete Fourier, so when I went to use this function, I was tearing my hair out to interpret it. I have a notebook I wrote for myself on the subject, but it's never been reviewed by someone who actually knows what they're doing. This is a Q&A web site for Mathematica, so we need to stick to that. We could take it up in a chat room, maybe others could join? –  Joel Klein Feb 20 '13 at 4:29
show 6 more comments

1 Answer

up vote 5 down vote accepted

This answer addresses some of your question, which is actually fairly broad and I think touches as much on the underlying concepts as on how to use Mathematica to achieve the stated goal.

This will answer:

  1. How to import a wav file as samples
  2. How to get the discrete Fourier transform normalized to give expected magnitudes
  3. How to get a range of frequencies
  4. How to get the magnitudes at those frequencies

Step 1: Import the wav file as samples:

signal = First@Import["D:\\audio\\Airwolf.wav", "Data"]

Here I use First to get just one stereo channel. Use "Elements" instead of "Data" above to reveal other options to pass as the 2nd argument to Import to find (for example) how many channels there are and what the sampling rate is.

You can test your techniques by using a sampled sine, rather than a wav file, where you control the magnitude, frequency, and phase. This generates 1 second of a 440Hz sine with magnitude 1 and zero phase shift:

Table[Sin[2 Pi t 440], {t, 0, 1, 1./10000}]

Step 2: Use the Fourier function to get the discrete Fourier transform:

dft=Fourier[signal, FourierParameters -> {-1, 1}]

I use FourierParameters -> {-1,1} to get magnitudes that you expect from the symbolic Fourier transform. For example, if signal was sampled from a 440Hz sine wave with magnitude k: $$f(t) = k * sin(2 \pi 440 t)$$ then the symbolic Fourier transform will give a Dirac delta at +/-440Hz with magnitude k/2, and FourierParameters->{-1,1} will give the same magnitudes. Read the docs for more info. The default option value normalizes the magnitudes differently.

You can plot the audible portion of this:

ListLinePlot[Part[dft, 1 ;; 20000], PlotRange -> Full]

enter image description here

Step 3: Get a range of frequencies from 101 to 200 Hz:

range = Part[dft, 101;;200]

Step 4: Get their magnitudes with the Abs function:

mag = Abs[range]

You can plot the magnitudes for this range:

ListLinePlot[Transpose[{Range[101, 200], mag}], PlotRange -> Full]

enter image description here

share|improve this answer
    
Thank you Joel, I really appreciate your response. May I know the length of of your wav file in this example? Because what I am trying to do is to analyse the frequency distribution of a whole song, and the Fourier takes ages to do it. And I am also concerned with another problem that should I divide my audio file into several small pieces and do the Fourier of each and then sum them up? will it provides a more accurate result than to Fourier a whole song? –  Ting Lee Feb 22 '13 at 16:16
    
Did you read my most recent comments above under the question? Fourier on a list of 7.2 million samples (180 seconds * 40,000 samples/second) runs in 3 seconds on my machine. So if it is taking ages on your machine, something is wrong. Be methodical. Take the Head, Length, and Dimensions of your signal and make sure they're what you expect (Dimensions should have only 1 thing in it, like {7200000}). –  Joel Klein Feb 22 '13 at 23:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.