diff options
Diffstat (limited to 'libs/qm-dsp/dsp/mfcc/MFCC.cpp')
-rw-r--r-- | libs/qm-dsp/dsp/mfcc/MFCC.cpp | 94 |
1 files changed, 47 insertions, 47 deletions
diff --git a/libs/qm-dsp/dsp/mfcc/MFCC.cpp b/libs/qm-dsp/dsp/mfcc/MFCC.cpp index 4c021aa7ba..09b296a0c8 100644 --- a/libs/qm-dsp/dsp/mfcc/MFCC.cpp +++ b/libs/qm-dsp/dsp/mfcc/MFCC.cpp @@ -27,75 +27,75 @@ MFCC::MFCC(MFCCConfig config) /* Calculate at startup */ double *freqs, *lower, *center, *upper, *triangleHeight, *fftFreqs; - + lowestFrequency = 66.6666666; linearFilters = 13; linearSpacing = 66.66666666; logFilters = 27; logSpacing = 1.0711703; - + /* FFT and analysis window sizes */ fftSize = config.fftsize; fft = new FFTReal(fftSize); totalFilters = linearFilters + logFilters; logPower = config.logpower; - + samplingRate = config.FS; - + /* The number of cepstral componenents */ nceps = config.nceps; /* Set if user want C0 */ WANT_C0 = (config.want_c0 ? 1 : 0); - + /* Allocate space for feature vector */ if (WANT_C0 == 1) { ceps = (double*)calloc(nceps+1, sizeof(double)); } else { ceps = (double*)calloc(nceps, sizeof(double)); } - + /* Allocate space for local vectors */ mfccDCTMatrix = (double**)calloc(nceps+1, sizeof(double*)); for (i = 0; i < nceps+1; i++) { - mfccDCTMatrix[i]= (double*)calloc(totalFilters, sizeof(double)); + mfccDCTMatrix[i]= (double*)calloc(totalFilters, sizeof(double)); } mfccFilterWeights = (double**)calloc(totalFilters, sizeof(double*)); for (i = 0; i < totalFilters; i++) { - mfccFilterWeights[i] = (double*)calloc(fftSize, sizeof(double)); + mfccFilterWeights[i] = (double*)calloc(fftSize, sizeof(double)); } - + freqs = (double*)calloc(totalFilters+2,sizeof(double)); - + lower = (double*)calloc(totalFilters,sizeof(double)); center = (double*)calloc(totalFilters,sizeof(double)); upper = (double*)calloc(totalFilters,sizeof(double)); - + triangleHeight = (double*)calloc(totalFilters,sizeof(double)); fftFreqs = (double*)calloc(fftSize,sizeof(double)); - + for (i = 0; i < linearFilters; i++) { freqs[i] = lowestFrequency + ((double)i) * linearSpacing; } - + for (i = linearFilters; i < totalFilters+2; i++) { - freqs[i] = freqs[linearFilters-1] * + freqs[i] = freqs[linearFilters-1] * pow(logSpacing, (double)(i-linearFilters+1)); } - + /* Define lower, center and upper */ memcpy(lower, freqs,totalFilters*sizeof(double)); memcpy(center, &freqs[1],totalFilters*sizeof(double)); memcpy(upper, &freqs[2],totalFilters*sizeof(double)); - + for (i=0;i<totalFilters;i++){ triangleHeight[i] = 2./(upper[i]-lower[i]); } - + for (i=0;i<fftSize;i++){ - fftFreqs[i] = ((double) i / ((double) fftSize ) * + fftFreqs[i] = ((double) i / ((double) fftSize ) * (double) samplingRate); } @@ -103,12 +103,12 @@ MFCC::MFCC(MFCCConfig config) for (i=0;i<totalFilters;i++){ for (j=0;j<fftSize;j++) { - + if ((fftFreqs[j] > lower[i]) && (fftFreqs[j] <= center[i])) { - - mfccFilterWeights[i][j] = triangleHeight[i] * - (fftFreqs[j]-lower[i]) / (center[i]-lower[i]); - + + mfccFilterWeights[i][j] = triangleHeight[i] * + (fftFreqs[j]-lower[i]) / (center[i]-lower[i]); + } else { @@ -118,7 +118,7 @@ MFCC::MFCC(MFCCConfig config) if ((fftFreqs[j]>center[i]) && (fftFreqs[j]<upper[i])) { mfccFilterWeights[i][j] = mfccFilterWeights[i][j] - + triangleHeight[i] * (upper[i]-fftFreqs[j]) + + triangleHeight[i] * (upper[i]-fftFreqs[j]) / (upper[i]-center[i]); } else @@ -130,15 +130,15 @@ MFCC::MFCC(MFCCConfig config) } /* - * We calculate now mfccDCT matrix + * We calculate now mfccDCT matrix * NB: +1 because of the DC component */ const double pi = 3.14159265358979323846264338327950288; - + for (i = 0; i < nceps+1; i++) { for (j = 0; j < totalFilters; j++) { - mfccDCTMatrix[i][j] = (1./sqrt((double) totalFilters / 2.)) + mfccDCTMatrix[i][j] = (1./sqrt((double) totalFilters / 2.)) * cos((double) i * ((double) j + 0.5) / (double) totalFilters * pi); } } @@ -146,7 +146,7 @@ MFCC::MFCC(MFCCConfig config) for (j = 0; j < totalFilters; j++){ mfccDCTMatrix[0][j] = (sqrt(2.)/2.) * mfccDCTMatrix[0][j]; } - + /* The analysis window */ window = new Window<double>(config.window, fftSize); @@ -156,7 +156,7 @@ MFCC::MFCC(MFCCConfig config) earMag = (double*)calloc(totalFilters, sizeof(double)); fftMag = (double*)calloc(fftSize/2, sizeof(double)); - + free(freqs); free(lower); free(center); @@ -168,27 +168,27 @@ MFCC::MFCC(MFCCConfig config) MFCC::~MFCC() { int i; - + /* Free the structure */ for (i = 0; i < nceps+1; i++) { free(mfccDCTMatrix[i]); } free(mfccDCTMatrix); - + for (i = 0; i < totalFilters; i++) { free(mfccFilterWeights[i]); } free(mfccFilterWeights); - + /* Free the feature vector */ free(ceps); - + /* The analysis window */ delete window; free(earMag); free(fftMag); - + /* Free the FFT */ free(realOut); free(imagOut); @@ -198,19 +198,19 @@ MFCC::~MFCC() /* - * - * Extract the MFCC on the input frame - * - */ + * + * Extract the MFCC on the input frame + * + */ int MFCC::process(const double *inframe, double *outceps) { double *inputData = (double *)malloc(fftSize * sizeof(double)); for (int i = 0; i < fftSize; ++i) inputData[i] = inframe[i]; window->cut(inputData); - + /* Calculate the fft on the input frame */ - fft->process(0, inputData, realOut, imagOut); + fft->forward(inputData, realOut, imagOut); free(inputData); @@ -244,14 +244,14 @@ int MFCC::process(const double *real, const double *imag, double *outceps) } /* - * - * Calculate now the cepstral coefficients + * + * Calculate now the cepstral coefficients * with or without the DC component * */ - + if (WANT_C0 == 1) { - + for (i = 0; i < nceps+1; i++) { double tmp = 0.; for (j = 0; j < totalFilters; j++){ @@ -260,8 +260,8 @@ int MFCC::process(const double *real, const double *imag, double *outceps) outceps[i] = tmp; } } - else - { + else + { for (i = 1; i < nceps+1; i++) { double tmp = 0.; for (j = 0; j < totalFilters; j++){ @@ -270,7 +270,7 @@ int MFCC::process(const double *real, const double *imag, double *outceps) outceps[i-1] = tmp; } } - + return nceps; } |