Frequency detection using the FFT (aka pitch tracking) With Source - TopicsExpress



          

Frequency detection using the FFT (aka pitch tracking) With Source Code Read enough data to fill the FFT Low-pass the data Apply a window to the data Transform the data using the FFT Find the peak value in the transformed data Compute the peak frequency from from the index of the peak value in the transformed data This is the main processing loop for the tuner, with some stuff left out: while( running ) { // read some data err = Pa_ReadStream( stream, data, FFT_SIZE ); // low-pass for( int j=0; j data[j] = processSecondOrderFilter( data[j], mem1, a, b ); data[j] = processSecondOrderFilter( data[j], mem2, a, b ); } // window applyWindow( window, data, FFT_SIZE ); // do the fft for( int j=0; j datai[j] = 0; applyfft( fft, data, datai, false ); //find the peak float maxVal = -1; int maxIndex = -1; for( int j=0; j float v = data[j] * data[j] + datai[j] * datai[j] ; if( v > maxVal ) { maxVal = v; maxIndex = j; } } float freq = freqTable[maxIndex]; //... } Audio Data inputParameters.device = Pa_GetDefaultInputDevice(); inputParameters.channelCount = 1; inputParameters.sampleFormat = paFloat32; inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ; inputParameters.hostApiSpecificStreamInfo = NULL; #define SAMPLE_RATE (8000) Low-Pass Filtering // Process every sample of your input with this function // (this is not used in our guitar tuner) function float twoPointMovingAverageFilter( float input ) { static float lastInput = 0; float output = ( input + lastInput ) / 2 ; lastInput = input; return output; } Windowing void buildHanWindow( float *window, int size ) { for( int i=0; i window[i] = .5 * ( 1 - cos( 2 * M_PI * i / (size-1.0) ) ); } void applyWindow( float *window, float *data, int size ) { for( int i=0; i data[i] *= window[i] ; } FFT binSize = sampleRate/N ; for( i in 0 to N/2 ) magnitude[i] = sqrt( real[i]*real[i] + cmpx[i]*cmpx[i] );
Posted on: Sat, 22 Mar 2014 18:36:36 +0000

Trending Topics



Recently Viewed Topics




© 2015