Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

audio - Determining the magnitude of a certain frequency on the iPhone

I'm wondering what's the easiest/best way to determine the magnitude of a given frequency in a sound.

It's my understanding that a FFT function will return the magnitudes of all frequencies in a signal. I'm wondering if there is any shortcut I could use if I'm only concerned about a specific frequency.

I'll be using the iPhone mic to record the audio. My guess is that I'll be using the Audio Queue Services for recording since I don't need to record the audio to a file. I'm using SDK 4.0, so I can use any of the functions defined in the Accelerate framework (e.g. FFT functions) if needed.

Update: I updated the question to be more clear as per Conrad's suggestion.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you only need to test for one frequency, you can just calculate the corresponding point of the DFT. The DFT algorithm is O(N^2), but the FFT algorithm reuses intermediate results to achieve O(NlogN) for computation of the DFT. However, if you want only one frequency sample, you can just calculate one output sample of the DFT and achieve O(N) performance.

This can be done by looking at the equation for the DFT on the wikipedia page (I'm not even going to try to type it here) and just calculate Xk for a single k corresponding to the frequency of interest. k is just the indexing on the output of the DFT.

Mapping k (indexes of the DFT output) into real frequencies (Hz) depends on two things:

  • Sampling frequency (for example, 44100 Hz for CD Audio)
  • FFT size

Real frequencies are mapped to k as follows:

F = k*Fs/N  for k = 0 ... N/2-1 ((N-1)/2 for odd N)

or

k = F*N/Fs  for F = 0Hz ... Fs/2-Fs/N

where F is the frequency in Hz, N is the FFT size, and Fs is the sampling frequency (Hz). Some things to note:

  • k is an integer, so not all frequencies will map to an integer k. Find the closest k
  • If you need more frequency resolution, increase N.
  • Signals sampled at Fs are only able to accurately represent frequencies up to, but not including Fs/2 (Nyquist rate). This is why I showed that the mapping from k to Hz is only good for half the output samples. I will not go into what the second half represents (it will actually be a mirror image of the first half for a real input signal)
  • The output of the DFT/FFT is complex. You most likely want to take the magnitude of this.
  • If you need to compute even a few DFT outputs, it may be better to just use the FFT function available and get all the output samples instead of calculating just the output samples you need using the DFT. The reason is that most FFT algorithms are heavily optimized so even though you may be theoretically doing less work, it may take longer than the FFT. You would probably just have to benchmark this to see which approach is better.

I've left out quite a few other details for simplicity's sake that shouldn't matter for your application


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...