c@251: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@251: c@251: /* c@251: QM DSP Library c@251: c@251: Centre for Digital Music, Queen Mary, University of London. c@251: This file copyright 2005 Nicolas Chetry, copyright 2008 QMUL. c@251: All rights reserved. c@251: */ c@251: c@251: #include c@251: #include c@251: c@251: #include "MFCC.h" c@251: #include "dsp/transforms/FFT.h" c@251: #include "base/Window.h" c@251: c@251: MFCC::MFCC(MFCCConfig config) c@251: { c@251: int i,j; c@251: c@251: /* Calculate at startup */ c@251: double *freqs, *lower, *center, *upper, *triangleHeight, *fftFreqs; c@251: c@251: lowestFrequency = 66.6666666; c@251: linearFilters = 13; c@251: linearSpacing = 66.66666666; c@251: logFilters = 27; c@251: logSpacing = 1.0711703; c@251: c@251: /* FFT and analysis window sizes */ c@251: fftSize = config.fftsize; c@251: c@251: totalFilters = linearFilters + logFilters; c@251: c@251: samplingRate = config.FS; c@251: c@251: /* The number of cepstral componenents */ c@251: nceps = config.nceps; c@251: c@251: /* Set if user want C0 */ c@251: WANT_C0 = (config.want_c0 ? 1 : 0); c@251: c@251: /* Allocate space for feature vector */ c@251: if (WANT_C0 == 1) { c@251: ceps = (double*)calloc(nceps+1, sizeof(double)); c@251: } else { c@251: ceps = (double*)calloc(nceps, sizeof(double)); c@251: } c@251: c@251: /* Allocate space for local vectors */ c@251: mfccDCTMatrix = (double**)calloc(nceps+1, sizeof(double*)); c@251: for (i=0;i lower[i]) && (fftFreqs[j] <= center[i])) { c@251: c@251: mfccFilterWeights[i][j] = triangleHeight[i] * c@251: (fftFreqs[j]-lower[i]) / (center[i]-lower[i]); c@251: c@251: } c@251: else c@251: { c@251: mfccFilterWeights[i][j] = 0.0; c@251: } c@251: c@251: if ((fftFreqs[j]>center[i]) && (fftFreqs[j](HammingWindow, fftSize); c@251: c@251: /* Allocate memory for the FFT */ c@251: imagIn = (double*)calloc(fftSize,sizeof(double)); c@251: realOut = (double*)calloc(fftSize,sizeof(double)); c@251: imagOut = (double*)calloc(fftSize,sizeof(double)); c@251: c@251: free(freqs); c@251: free(lower); c@251: free(center); c@251: free(upper); c@251: free(triangleHeight); c@251: free(fftFreqs); c@251: } c@251: c@251: MFCC::~MFCC() c@251: { c@251: int i; c@251: c@251: /* Free the structure */ c@251: for (i=0;icut(inputData); c@251: c@251: /* Calculate the fft on the input frame */ c@251: FFT::process(fftSize, 0, inputData, imagIn, realOut, imagOut); c@251: c@251: for (i = 0; i < fftSize; ++i) { c@251: fftMag[i] = sqrt(realOut[i] * realOut[i] + c@251: imagOut[i] * imagOut[i]); c@251: } c@251: c@251: /* Multiply by mfccFilterWeights */ c@251: for (i=0;i0) c@251: earMag[i] = log10(tmp); c@251: else c@251: earMag[i] = 0.0; c@251: } c@251: c@251: /* c@251: * c@251: * Calculate now the cepstral coefficients c@251: * with or without the DC component c@251: * c@251: */ c@251: c@251: if (WANT_C0==1) { c@251: c@251: for (i=0;i