Audio input FFT


#1

Hi guys,

I’m new to OF and I’m trying to send data thought a jack between an iPad and a computer. I’m actually working on the computer side part and trying to get the higher frequency I get on input.

I worked with this example : Audio input FFT example

But actually, I don’t really get how I can go from the magnitude to the frequency. Does anyone could help me ?

Thx


#2

I find my solution if there is some people are interested in. Here is my code:

void ofApp::draw(){
    static int index=0;
    float avg_power = 0.0f;

    if(index < 80)
        index += 1;
    else
        index = 0;

    /* do the FFT	*/
    myfft.powerSpectrum(0,(int)BUFFER_SIZE/2, left,BUFFER_SIZE,&magnitude[0],&phase[0],&power[0],&avg_power);

    /* start from 1 because mag[0] = DC component */
    /* and discard the upper half of the buffer */
    for(int j=1; j < BUFFER_SIZE/2; j++) {
        freq[index][j] = magnitude[j];
    }


    /* draw the FFT */
    for (int i = 1; i < (int)(BUFFER_SIZE/2); i++){
        ofDrawBitmapStringHighlight(ofToString(magnitude[i]), ofVec3f(10,10+15*i));
    }

    float maxValue = getMaxValue(magnitude);
    float attachedFreq = maxValue * (44100/BUFFER_SIZE);

    ofDrawBitmapStringHighlight(ofToString(maxValue), ofVec3f(100,25));
    ofDrawBitmapStringHighlight(ofToString(attachedFreq), ofVec3f(100,40));
}
//--------------------------------------------------------------
float ofApp::getMaxValue(float *magnitude){
    std::vector<float> v {magnitude, magnitude + BUFFER_SIZE/2};
    auto result = std::minmax_element(v.begin(), v.end());
    std::cout << "max element at: " << (result.second - v.begin()) << '\n';
    return (result.second - v.begin());
}