0

I'm new to portaudio and i looked at "How to extract frequency..." question but still i would like to have some help. i need to transmit a frequency (using the speakers) and then i would like to check if this frequency was transmitted (catching it in the microphone), of course that i would like to use portaudio for the sending and detecting. also,i have no idea how to use the fft for detection because i saw the record example and they use a SAMPLE data type and with this data type they can find the max amplitude can i use it for finding the frequency?

tnx in advance.

TheNewOne
  • 1,613
  • 11
  • 19
  • 1
    I'm not clear what you are trying to do "transmit a frequency" seems like one thing you want to do. What does that mean? over the net? play through your soundcard? I think you need to write a clearer question. – Bjorn Roche Jul 12 '12 at 18:04
  • sry if it wasnt't clear i meant through the default output.and i know how to use the functions of getting the default output and input but how do i set exact frequency e.g 1000 hz.and then how i use the input (microphone for instance) to say i've just got 1000hz. tnx for the interest and the will the help. – TheNewOne Jul 13 '12 at 09:41

1 Answers1

1

Playing a simple tone is given as sample code in PortAudio.

Although the pitch there is not calculated from a specific number, it's easy to figure out:

amplitude[n] = sin( n * desiredFreq * 2 * pi / samplerate )

As in the PortAudio example code, you may want to consider using a lookup table rather than counting on sin() for each call, both for performance (and accuracy of sin at large numbers, which might be fine, but I'm not sure about).

Detecting what frequency is being played is a question that has been asked here on SO. Here is an answer I gave recently.

The upshot is that there are many ways depending on your skill level, and issues such as background noise. If you are just detecting a simple sin wav and don't expect a lot of noise, you might do well to just count zero crossings after making sure that the signal is above a certain threshold. If you do expect noise, you'll have to get fancier.

Community
  • 1
  • 1
Bjorn Roche
  • 11,279
  • 6
  • 36
  • 58
  • I was counting on this http://stackoverflow.com/questions/3058236/how-to-extract-frequency-information-from-samples-from-portaudio-using-fftw-in-c.i just didn't understand the whole idea, i looked at the example called 'paex_record' and i have no idea how can i use FFT on the sample,and again tnx for the help and +1 for the clarification of transmitting frequency – TheNewOne Jul 13 '12 at 16:38
  • When you record, you get a series of samples, which represent the amplitude over time. Take a contiguous subset of these (say, the first 1024 samples), window it, and perform the FFT as described in that answer. You'll need to take into account mono vs stereo, data format, and a few other things, but this is a complex problem I can't explain everything here. For starters, you don't need to window it, get mono floating point data from PA. That will help. Then move on to the next block of samples (you may want to overlap). – Bjorn Roche Jul 13 '12 at 21:32
  • A bit of detail on generating tones here: http://stackoverflow.com/questions/12612951/multi-audio-tones-to-sound-card-using-portaudio/12632742#12632742 – Bjorn Roche Sep 28 '12 at 17:17
  • Thank you, I have already wrote a slice of code to generate the frequency I need. Cheers and thanks anyway I really appreciate your help. – TheNewOne Sep 29 '12 at 18:09