annotate examples/simpletest/simpletest.c @ 158:069b2f719dd4

Merge pull request #13 from felixonmars/master fix for automake 1.13
author Jamie Bullock <jamie@jamiebullock.com>
date Fri, 15 Mar 2013 05:46:57 -0700
parents 32b78d95f7fd
children 19e5ef005bdb
rev   line source
jamie@141 1 /*
jamie@141 2 * Copyright (C) 2012 Jamie Bullock
jamie@86 3 *
jamie@141 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
jamie@141 5 * of this software and associated documentation files (the "Software"), to
jamie@141 6 * deal in the Software without restriction, including without limitation the
jamie@141 7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
jamie@141 8 * sell copies of the Software, and to permit persons to whom the Software is
jamie@141 9 * furnished to do so, subject to the following conditions:
jamie@86 10 *
jamie@141 11 * The above copyright notice and this permission notice shall be included in
jamie@141 12 * all copies or substantial portions of the Software.
jamie@86 13 *
jamie@141 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jamie@141 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jamie@141 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jamie@141 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jamie@141 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
jamie@141 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
jamie@141 20 * IN THE SOFTWARE.
jamie@141 21 *
jamie@86 22 */
jamie@29 23
jamie@29 24 #include "xtract/libxtract.h"
jamie@29 25 #include <stdio.h>
jamie@135 26 #include <stdlib.h>
jamie@29 27
jamie@135 28 #define BLOCKSIZE 1024
jamie@135 29 #define SAMPLERATE 44100
jamie@135 30 #define PERIOD 100
jamie@135 31 #define MFCC_FREQ_BANDS 13
jamie@135 32 #define MFCC_FREQ_MIN 20
jamie@135 33 #define MFCC_FREQ_MAX 20000
jamie@29 34
jamie@135 35 int main(void)
jamie@135 36 {
jamie@135 37
jamie@146 38 double mean = 0.f;
jamie@146 39 double input[BLOCKSIZE];
jamie@146 40 double spectrum[BLOCKSIZE];
jamie@146 41 double mfccs[MFCC_FREQ_BANDS * sizeof(double)];
jamie@146 42 double argd[4];
jamie@135 43 int n;
jamie@135 44 xtract_mel_filter mel_filters;
jamie@120 45
jamie@135 46 /* fill the input array with a sawtooth wave */
jamie@135 47 for(n = 0; n < BLOCKSIZE; ++n)
jamie@135 48 {
jamie@146 49 input[n] = ((n % PERIOD) / (double)PERIOD) - .5;
jamie@135 50 }
jamie@135 51
jamie@135 52 /* get the mean of the input */
jamie@156 53 xtract[XTRACT_MEAN](input, BLOCKSIZE, NULL, &mean);
jamie@135 54 printf("\nInput mean = %.2f\n\n", mean);
jamie@135 55
jamie@135 56 /* get the spectrum */
jamie@146 57 argd[0] = SAMPLERATE / (double)BLOCKSIZE;
jamie@146 58 argd[1] = XTRACT_MAGNITUDE_SPECTRUM;
jamie@146 59 argd[2] = 0.f; /* No DC component */
jamie@146 60 argd[3] = 0.f; /* No Normalisation */
jamie@29 61
jamie@135 62 xtract_init_fft(BLOCKSIZE, XTRACT_SPECTRUM);
jamie@156 63 xtract[XTRACT_SPECTRUM](input, BLOCKSIZE, &argd[0], spectrum);
jamie@120 64
jamie@135 65 /* print the spectral bins */
jamie@135 66 printf("\nSpectral bins:\n");
jamie@135 67 for(n = 0; n < (BLOCKSIZE >> 1); ++n){
jamie@135 68 printf("freq: %.1f\tamp: %.6f\n", spectrum[n + (BLOCKSIZE >> 1)], spectrum[n]);
jamie@120 69 }
jamie@120 70 printf("\n");
jamie@120 71
jamie@135 72 /* compute the MFCCs */
jamie@135 73 mel_filters.n_filters = MFCC_FREQ_BANDS;
jamie@155 74 mel_filters.filters = (double **)malloc(MFCC_FREQ_BANDS * sizeof(double *));
jamie@135 75 for(n = 0; n < MFCC_FREQ_BANDS; ++n)
jamie@135 76 {
jamie@155 77 mel_filters.filters[n] = (double *)malloc(BLOCKSIZE * sizeof(double));
jamie@135 78 }
jamie@135 79
jamie@135 80 xtract_init_mfcc(BLOCKSIZE >> 1, SAMPLERATE >> 1, XTRACT_EQUAL_GAIN, MFCC_FREQ_MIN, MFCC_FREQ_MAX, mel_filters.n_filters, mel_filters.filters);
jamie@135 81 xtract_mfcc(spectrum, BLOCKSIZE >> 1, &mel_filters, mfccs);
jamie@135 82
jamie@135 83 /* print the MFCCs */
jamie@135 84 printf("MFCCs:\n");
jamie@135 85 for(n = 0; n < MFCC_FREQ_BANDS; ++n)
jamie@135 86 {
jamie@135 87 printf("band: %d\t", n);
jamie@135 88 if(n < 10) {
jamie@135 89 printf("\t");
jamie@135 90 }
jamie@135 91 printf("coeff: %f\n", mfccs[n]);
jamie@135 92 }
jamie@135 93
jamie@135 94 /* cleanup */
jamie@135 95 for(n = 0; n < MFCC_FREQ_BANDS; ++n)
jamie@135 96 {
jamie@135 97 free(mel_filters.filters[n]);
jamie@135 98 }
jamie@135 99 free(mel_filters.filters);
jamie@135 100
jamie@29 101 return 0;
jamie@135 102
jamie@29 103 }