-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDCTJava.java
57 lines (43 loc) · 1.42 KB
/
DCTJava.java
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
package com.example.android_mfcc;
import static java.lang.Math.PI;
import static java.lang.Math.cos;
import static java.lang.Math.sqrt;
public class DCTJava {
/** @brief constructor. it pre-calculates a table.
*
* @param numPoints : number of points in the input.
*/
DCTJava ( int numPoints ) {
mNumPoints = numPoints;
makeDCTTable();
}
/** @brief main function for DCT
*
* @param array_in : time domain samples
* @return : frequency domain samples
*/
float[] transform(float[] array_in) {
float[] array_out = new float[mNumPoints + 1];
for ( int i = 0; i < mNumPoints + 1 ; i++ ) {
float val = 0.0f;
for ( int j = 0; j < mNumPoints; j++ ) {
val += mDCTTable[i][j] * array_in[j];
}
array_out[i] = val;
}
return array_out;
}
private void makeDCTTable() {
mDCTTable = new float[ mNumPoints + 1][ mNumPoints ];
final float C = (float)sqrt( 2.0 / ((double)mNumPoints) );
for ( int i = 0; i <= mNumPoints; i++ ) {
for (int j = 0; j < mNumPoints; j++ ) {
final float di = (float)i ;
final float dj = (float)j + 0.5f ;
mDCTTable[i][j] = C * (float)cos( PI * di * dj / (float)mNumPoints );
}
}
}
private int mNumPoints;
private float[][] mDCTTable;
}