aboutsummaryrefslogtreecommitdiffstats
path: root/libs/maxiMFCC.h
blob: 19dd72e06472a262029e8af9062f35158e656912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 *  maxiMFCC.h
 *  mfccs
 *
 *  Created by Chris on 08/03/2011.
 *  Copyright 2011 Goldsmiths Creative Computing. All rights reserved.
 *
 
 Based on Matthew Yee-King's MFCCMYK java class
 */

#pragma once
#pragma pack(16)

#include "maxiFFT.h"
#include <math.h>
#include <iostream>
#include <cstdlib>
#ifdef __APPLE_CC__
#include <Accelerate/Accelerate.h>
#endif

using namespace std;


// implements this formula:
// mel = 2595 log10(Hz/700 + 1)
inline double hzToMel(double hz){
	return 2595.0 * (log10(hz/700.0 + 1.0));
}

// implements this formula
// Hz = 700 (10^(mel/2595) - 1)
inline double melToHz(double mel){
	return 700.0 * (pow(10, mel/2595.0) - 1.0);
}

template <class T>
class maxiMFCCAnalyser {
public:
	T *melBands;
	maxiMFCCAnalyser():melFilters(NULL),dctMatrix(NULL), melBands(NULL){};
	~maxiMFCCAnalyser() {
		if (melFilters) {
			delete[] melFilters;
			delete[] melBands;
			delete[] dctMatrix;
#ifdef __APPLE_CC__
			delete doubleSpec;
#endif
		}
	}
	
	void setup(unsigned int numBins, unsigned int numFilters, unsigned int numCoeffs, double minFreq, double maxFreq, unsigned int sampleRate) 
	{
		this->numFilters = numFilters;
		this->numCoeffs = numCoeffs;
		this->minFreq = minFreq;
		this->maxFreq = maxFreq;
		this->sampleRate = sampleRate;
		this->numBins = numBins;
		melFilters = NULL;
		melBands = (T*) malloc(sizeof(T) * numFilters);
#ifdef __APPLE_CC__
		doubleSpec = (T*)malloc(sizeof(T) * numBins);
#endif
		//create new matrix
		dctMatrix = (T*)malloc(sizeof(T) * numCoeffs * numFilters);
		calcMelFilterBank(sampleRate, numBins);
		createDCTCoeffs();
	}
	void mfcc(float* powerSpectrum, T *mfccs) {
		melFilterAndLogSquare(powerSpectrum);
		dct(mfccs);
	}
	
private:
	unsigned int numFilters, numCoeffs;
	double minFreq, maxFreq;
	unsigned int sampleRate;
	T *melFilters;
	unsigned int numBins;
	T *dctMatrix;
#ifdef __APPLE_CC__
	T *doubleSpec;
#endif
	
#ifdef __APPLE_CC__
	void dct(T *mfccs); //define later
#else
	void dct(T *mfccs) {
		for(int i=0; i < numCoeffs; i++) {
			mfccs[i] = 0.0;
		}
		for(int i=0; i < numCoeffs; i++ ) {
			for(int j=0; j < numFilters; j++) {
				int idx = i + (j * numCoeffs);
				mfccs[i] += (dctMatrix[idx] * melBands[j]);
			}
		}
		for(int i=0; i < numCoeffs; i++) {
			mfccs[i] /= numCoeffs;
		}
	}
#endif
	
	void melFilterAndLogSquare(float* powerSpectrum);
	void melFilterAndLogSq_Part2(float *powerSpectrum);

	
	void calcMelFilterBank(double sampleRate, int numBins) {

		double mel, dMel, maxMel, minMel, nyquist, binFreq, start, end, thisF, nextF, prevF;
		int numValidBins;
		
		// ignore bins over nyquist
		numValidBins = numBins;
		
		nyquist = sampleRate/2;
		if (maxFreq > nyquist) {
			maxFreq = nyquist;
		}
		
		maxMel = hzToMel(maxFreq);
		minMel = hzToMel(minFreq);
		
		dMel = (maxMel - minMel) / (numFilters + 2 - 1);
		
		T *filtPos = (T*) malloc(sizeof(double) * (numFilters + 2));
		
		// first generate an array of start and end freqs for each triangle
		mel = minMel;
		for (int i=0;i<numFilters + 2;i++) {
			// start of the triangle
			filtPos[i] = melToHz(mel);
			//		std::cout << "[" << i << "] MFCC: centre is at " <<filtPos[i]<<"hz "<<mel<<" mels" << endl;
			mel += dMel;
		}
		// now generate the coefficients for the mag spectrum
		melFilters = (T*) malloc(sizeof(T) * numFilters * numValidBins);
		
		for (int filter = 1; filter < numFilters; filter++) {
			for (int bin=0;bin<numValidBins;bin++) {
				// frequency this bin represents
				binFreq = (T) sampleRate / (T) numValidBins * (T) bin;
				thisF = filtPos[filter];
				nextF = filtPos[filter+1];
				prevF = filtPos[filter-1];
				int idx = filter + (bin * numFilters);
				if (binFreq > nextF || binFreq < prevF) {
					// outside this filter
					melFilters[idx] = 0;
					//cout << "MFCCMYK: filter at " <<thisF << " bin at " <<binFreq <<" coeff " <<melFilters[filter][bin] << endl;
				}
				else {
					T height = 2.0 / (nextF - prevF);
					
					if (binFreq < thisF) {
						// up
						start = prevF;
						end = thisF;
						melFilters[idx] = (binFreq - start) * (height / (thisF - start));	  
					}
					else {
						// down
						start = thisF;
						end = nextF;
						melFilters[idx] = height + ((binFreq - thisF) * (-height /(nextF - thisF)));	  
					}
					//				cout << "MFCCMYK: filter at " <<thisF << " bin at " <<binFreq <<" coeff " <<melFilters[filter][bin] << endl;
					//cout << "MFCCMYK: filter at " <<thisF << " bin at " <<binFreq <<" coeff " <<melFilters[idx] << endl;
				}
			}
		}
	}
	void createDCTCoeffs() {
		T k = 3.14159265358979323846/numFilters;
		T w1 = 1.0/(sqrt(numFilters));
		T w2 = sqrt(2.0/numFilters);
		
		
		//generate dct matrix
		for(int i = 0; i < numCoeffs; i++)
		{
			for(int j = 0; j < numFilters; j++)
			{
				int idx = i + (j * numCoeffs);
				if(i == 0)
					dctMatrix[idx]= w1 * cos(k * (i+1) * (j + 0.5));
				else
					dctMatrix[idx] = w2 * cos(k * (i+1) * (j + 0.5));
			}
		}
		
		
	}
		
	
	
};



typedef maxiMFCCAnalyser<double> maxiMFCC;
//typedef maxiMFCCAnalyser<float> maxiFloatMFCC;