/* 
 * Setup filterbank internals
 */
bool ChannelizerBase::init()
{
	int i;

	/*
	 * Filterbank coefficients, fft plan, history, and output sample
	 * rate conversion blocks
	 */
	if (!initFilters()) {
		LOG(ERR) << "Failed to initialize channelizing filter";
		return false;
	}

	mResampler = new Resampler(mP, mQ, mFiltLen, mChanM);
	if (!mResampler->init()) {
		LOG(ERR) << "Failed to initialize resampling filter";
		return false;
	}

	history = (struct cxvec **) malloc(sizeof(struct cxvec *) * mChanM);
	for (i = 0; i < mChanM; i++) {
		history[i] =  cxvec_alloc(mFiltLen, 0, NULL, 0);
		cxvec_reset(history[i]);
	}

	if (!initFFT()) {
		LOG(ERR) << "Failed to initialize FFT";
		return false;
	}

	mapBuffers();

	return true;
}
// _______________________________________________________
void KeyWordDetector::init(void (*keywordHandler)(int), void (*templateHandler)(int)) {
  // Store the handler function
  keywordHandlerFunction = keywordHandler;
  templateHandlerFunction = templateHandler;
  
  // Since the audio board uses different spi pins, we need to change those here
  if (!theBreadBoard) {
    SPI.setMOSI(7);
    SPI.setSCK(14);
  }
  // Init the SD card
  if (DEBUG_KD_MORE) Serial.print("Initializing SD card...");
  pinMode(SD_CS, OUTPUT);
  if (!SD.begin(SD_CS)) {
    if (DEBUG_KD_MORE) Serial.println("initialization failed!");
  } else {
    if (DEBUG_KD_MORE) Serial.println("initialization done.");
  }
  
  
  // Read all available MFCC data from SD card
  for (int i = 0; i < 100; i++) {
    if (SD_readMFCCData(i)) {
      if (DEBUG_KD_MORE) {
        Serial.print("Template size: ");
        Serial.print(templateKeywordFrameCount);
        Serial.println(" frames");
        //printV(templateMFCCs, templateKeywordFrameCount*NUM_MFCCS, true);
      }
      
      // Determine the longest and shortes keyword
      if (templateKeywordFrameCount < shortestTemplateSize)  shortestTemplateSize = templateKeywordFrameCount;
      if (templateKeywordFrameCount > longestTemplateSize)  longestTemplateSize = templateKeywordFrameCount; 
    } else {
      numberOfStoredTemplates = i;
      break;
    }
  }
  
  if (DEBUG_KD) {
    Serial.print("Total number of templates: ");
    Serial.println(numberOfStoredTemplates);
    Serial.print("Longest template: ");
    Serial.println(longestTemplateSize);
    Serial.print("Shortest template: ");
    Serial.println(shortestTemplateSize);
  }
  
  // Calculate the size of the needed FFT
  initFFT();
  // Generate the Mel filter bank
  generateMelFilterBank();
  // Geneerate the window function
  generateWindowFunction();
  // Calculate needed DCT coefficients
  computeDCTCoeff26(dctCoeff);
  
  if (DEBUG_KD) dumpInfo();
}
Esempio n. 3
0
void fftl(int n, double xRe[], double xIm[],
                double yRe[], double yIm[])
{
    int   count;
    initFFT(n);
//    transTableSetup(sofarRadix, actualRadix, remainRadix, &nFactor, n);
    permute(n, nFactor, actualRadix, remainRadix, xRe, xIm, yRe, yIm);

    for (count=1; count<=nFactor; count++){
      twiddleTransf(sofarRadix[count], actualRadix[count], remainRadix[count], 
                    yRe, yIm);
    }
}   /* fft */
Esempio n. 4
0
void ofxFFTBase::setBufferSize(int value) {
    int bufferSizeNew = ofNextPow2(value);
    
    if(bufferSize == bufferSizeNew) {
        return;
    }
    
    bufferSize = bufferSizeNew;
    binSize = (int)(bufferSize * 0.5);
    
    killFFT();
    initFFT();
    initAudioData(fftData, bufferSize);
}
Esempio n. 5
0
CFFT::CFFT(QObject *parent, int size) :
    QObject(parent)
  ,window(NULL)
  ,winfunc(NULL)
  ,bufferBlock(0)
  ,channelSize(FRAME_SIZE)
  ,update(false)
{
    xval = new double[size];
    yval = new double[size];
    initBuffer(size);
    initFFT(size);
    fftsize = size;
    setWindow(CFFT::Blackman, size);
}
// ----------------------------------------------------------------------------
float * ofOpenALSoundPlayer::getSpectrum(int bands){
	initFFT(bands);
	bins.assign(bins.size(),0);
	if(sources.empty()) return &bins[0];

	int signalSize = (bands-1)*2;
	getCurrentBufferSum(signalSize);

	float normalizer = 2. / windowSum;
	runWindow(windowedSignal);
	kiss_fftr(fftCfg, &windowedSignal[0], &cx_out[0]);
	for(int i= 0; i < bands; i++) {
		bins[i] += sqrtf(cx_out[i].r * cx_out[i].r + cx_out[i].i * cx_out[i].i) * normalizer;
	}
	return &bins[0];
}
Esempio n. 7
0
void ofxFFTBase :: setNoOfBands( int value )
{
	int audioNoOfBandsNew;
	audioNoOfBandsNew	= ofNextPow2( value );
	
	if( audioNoOfBands == audioNoOfBandsNew )
		return;
	
	audioNoOfBands		= OFX_FFT_NO_OF_BANDS;
	audioNoOfBandsHalf	= (int)( audioNoOfBands * 0.5 );
    
    killFFT();
	initFFT();
	initAudioData( rawData, getNoOfBands() );
	initAudioData( fftData, getNoOfBands() );
}
FastFourierTransform::FastFourierTransform(){
    initialized = false;
    computeMagnitude = true;
    computePhase = true;
    windowSize = 0;
    windowFunction = RECTANGULAR_WINDOW;
    gFFTBitTable = NULL;
    fftReal = NULL;
    fftImag = NULL;
    tmpReal = NULL;
    tmpImag = NULL;
    magnitude = NULL;
    phase = NULL;
    power = NULL;
    averagePower = 0;
    
    initFFT();
}
Esempio n. 9
0
ofxFFTBase::ofxFFTBase() {
    _fft = NULL;
    buffer = NULL;
    magnitudes = NULL;
    magnitudesDB = NULL;
    phases = NULL;
    window = NULL;
    
    setMaxDecay(0.995);
    setPeakDecay(0.96);
    setThreshold(0.5);
    setMirrorData(false);
    
    renderBorder = 1;
    
    bufferSize = 512; // default.
    binSize = (int)(bufferSize * 0.5);
    
    initFFT();
    initAudioData(fftData, binSize);
}
Esempio n. 10
0
ofxFFTBase ::  ofxFFTBase() 
{
    specData        = NULL;
    fftMagnitude    = NULL;
    fftPhase        = NULL;
    fftPower        = NULL;
	fftFreq         = NULL;

    setMaxDecay( 0.995 );
	setPeakDecay( 0.96 );
	setThreshold( 0.5 );
	setMirrorData( false );
	
	renderBorder = 1;
    
	audioNoOfBands		= OFX_FFT_NO_OF_BANDS;
	audioNoOfBandsHalf	= (int)( audioNoOfBands * 0.5 );
    
    killFFT();
	initFFT();
	initAudioData( rawData, audioNoOfBandsHalf );
	initAudioData( fftData, audioNoOfBandsHalf );
}
Esempio n. 11
0
void CMFCC::init(float SampleF,char* mempool, int &mpidx,long DefFFTLen,long DefFrameLen,long DefSubBandNum, long DefCepstrumNum)
{
	long	M, SubBandNo, i, j;
	float	ms,pi_factor, mfnorm, a, melk, t;
	MPidx = &mpidx;
	m_pMemPool = mempool;
	historyMPidx = mpidx;

	FrameLen	= DefFrameLen;
	SubBandNum	= DefSubBandNum;
	CepstrumNum	= DefCepstrumNum;
	initFFT(m_pMemPool,mpidx,DefFFTLen);
	FFT_LEN = GetFFTAnalyseLen();
	//	std::cout <<"FFT_LEN = "<<FFT_LEN<<std::endl;

	if(((unsigned int)(m_pMemPool + mpidx))%4)
		mpidx += 4 - ((unsigned int)(m_pMemPool + mpidx))%4;

	cosTab = (float*)(m_pMemPool + mpidx);
	mpidx += sizeof(float) * (SubBandNum+1)*(SubBandNum+1);	//DCT变换系数

	hamWin = (float*)(m_pMemPool + mpidx);
	mpidx += sizeof(float) * FrameLen;	//hamming 窗系数

	cepWin = (float*)(m_pMemPool + mpidx);
	mpidx += sizeof(float) * (SubBandNum+1);	

	MelBandBoundary = (float*)(m_pMemPool + mpidx);
	mpidx += sizeof(float) * (SubBandNum+2);

	SubBandWeight = (float*)(m_pMemPool + mpidx);
	mpidx += sizeof(float) * FFT_LEN/2;

	SubBandIndex = (long*)(m_pMemPool + mpidx);
	mpidx += sizeof(long) * FFT_LEN/2;


	SubBandEnergy = (float*)(m_pMemPool + mpidx); 
	mpidx += sizeof(float) * (SubBandNum+2);		//for accumulating the corresponding 

	FFTFrame = (float*)(m_pMemPool + mpidx); 
	mpidx += sizeof(float) * (FFT_LEN);		//for accumulating the corresponding 

	Cepstrum = (float*)(m_pMemPool + mpidx); 
	mpidx += sizeof(float) * (SubBandNum+1);		//for accumulating the corresponding 

	fres = SampleF/(FFT_LEN*700.0f);
	//caculating the mel scale sub-band boundary
	//由于子带0的系数(直流分量)不参与MFCC特征计算,所以子带个数要多一个
	M	 = SubBandNum+1;	
	ms	 = Mel(FFT_LEN/2);		//计算1/2采样率所对应的MEL刻度
	//Note that the sub-band 0 is not used for cepstrum caculating
	for ( SubBandNo = 0; SubBandNo <= M; SubBandNo++ )
	{	//计算每个子带的起始MEL刻度
		MelBandBoundary[SubBandNo] = ( (float)SubBandNo/(float)M )*ms;
	}

	//mapping the FFT frequence component into the corresponding sub-band
	for ( i=0,SubBandNo=1; i< FFT_LEN/2; i++)
	{
		melk = Mel(i);
		while( MelBandBoundary[SubBandNo] < melk ) SubBandNo++;
		SubBandIndex[i] = SubBandNo-1;
	}
	//caculating the weighting coefficients for each FFT frequence components	
	for(i=0; i< FFT_LEN/2; i++)
	{	//以子带的起始MEL频率为中心,计算三角窗加权系数
		SubBandNo = SubBandIndex[i];
		SubBandWeight[i] = (MelBandBoundary[SubBandNo+1]-Mel(i))/(MelBandBoundary[SubBandNo+1]-MelBandBoundary[SubBandNo]);
	}

	pi_factor = (float)( asin(1.0)*2.0/(float)SubBandNum );
	mfnorm	  = (float)sqrt(2.0f/(float)SubBandNum);
	for( i=1; i<= CepstrumNum; i++ )
	{
		t = (float)i*pi_factor;
		for(j=1; j<=SubBandNum; j++)
			cosTab[i*(SubBandNum+1)+j] = (float)cos(t*(j-0.5f))*mfnorm;
	}

	a =(float)( asin(1.0)*4/(FrameLen-1) );
	for(i=0;i<FrameLen;i++)
		hamWin[i] = 0.54f - 0.46f * (float)cos(a*i);

	for(i=1;i<=CepstrumNum;i++)
		cepWin[i-1] = (float)i * (float)exp(-(float)i*2.0/(float)CepstrumNum);
}
bool FastFourierTransform::FFT(int NumSamples,bool InverseTransform,double *RealIn, double *ImagIn, double *RealOut, double *ImagOut){
    int NumBits;                 /* Number of bits needed to store indices */
    int i, j, k, n;
    int BlockSize, BlockEnd;
    
    double angle_numerator = 2.0 * PI;
    double tr, ti;                /* temp real, temp imaginary */
    
    if ( !isPowerOfTwo(NumSamples) ) {
        fprintf(stderr, "%d is not a power of two\n", NumSamples);
        return false;
    }
    
    if (!gFFTBitTable)
        initFFT();
    
    if (InverseTransform)
        angle_numerator = -angle_numerator;
    
    NumBits = numberOfBitsNeeded(NumSamples);
    
    //Simultaneously data copy and bit-reversal ordering into outputs...
    for(i = 0; i < NumSamples; i++) {
        j = fastReverseBits(i, NumBits);
        RealOut[j] = RealIn[i];
        ImagOut[j] = (ImagIn == NULL) ? 0.0 : ImagIn[i];
    }
    
    //Do the FFT
    BlockEnd = 1;
    for (BlockSize = 2; BlockSize <= NumSamples; BlockSize <<= 1) {
        
        double delta_angle = angle_numerator / (double) BlockSize;
        
        double sm2 = sin(-2 * delta_angle);
        double sm1 = sin(-delta_angle);
        double cm2 = cos(-2 * delta_angle);
        double cm1 = cos(-delta_angle);
        double w = 2 * cm1;
        double ar0, ar1, ar2, ai0, ai1, ai2;
        
        for (i = 0; i < NumSamples; i += BlockSize) {
            ar2 = cm2;
            ar1 = cm1;
            
            ai2 = sm2;
            ai1 = sm1;
            
            for (j = i, n = 0; n < BlockEnd; j++, n++) {
                ar0 = w * ar1 - ar2;
                ar2 = ar1;
                ar1 = ar0;
                
                ai0 = w * ai1 - ai2;
                ai2 = ai1;
                ai1 = ai0;
                
                k = j + BlockEnd;
                tr = ar0 * RealOut[k] - ai0 * ImagOut[k];
                ti = ar0 * ImagOut[k] + ai0 * RealOut[k];
                
                RealOut[k] = RealOut[j] - tr;
                ImagOut[k] = ImagOut[j] - ti;
                
                RealOut[j] += tr;
                ImagOut[j] += ti;
            }
        }
        
        BlockEnd = BlockSize;
    }
    
    //Need to normalize the results if we are computing the inverse transform
    if( InverseTransform ){
        double denom = (double) NumSamples;
        
        for(i = 0; i < NumSamples; i++) {
            RealOut[i] /= denom;
            ImagOut[i] /= denom;
        }
    }
    
    return true;
}