5
$\begingroup$

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.

$\endgroup$
11
  • 1
    $\begingroup$ 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? $\endgroup$
    – rm -rf
    Commented Feb 19, 2013 at 23:46
  • $\begingroup$ 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. $\endgroup$
    – Ting Lee
    Commented Feb 19, 2013 at 23:54
  • $\begingroup$ An example will be to make an intensity versus frequency plot of a whole song. Need helps!!! $\endgroup$
    – Ting Lee
    Commented Feb 20, 2013 at 0:03
  • $\begingroup$ Have you imported the audio file into Mathematica? $\endgroup$
    – Joel Klein
    Commented Feb 20, 2013 at 0:06
  • 1
    $\begingroup$ 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? $\endgroup$
    – Joel Klein
    Commented Feb 20, 2013 at 4:29

1 Answer 1

7
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Ting Lee
    Commented Feb 22, 2013 at 16:16
  • $\begingroup$ 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}). $\endgroup$
    – Joel Klein
    Commented Feb 22, 2013 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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