plectrum

Plectrum: instrument tuner for Android
Log | Files | Refs | README | LICENSE

DCT.java (3903B)


      1 /*
      2 *      _______                       _____   _____ _____  
      3 *     |__   __|                     |  __ \ / ____|  __ \ 
      4 *        | | __ _ _ __ ___  ___  ___| |  | | (___ | |__) |
      5 *        | |/ _` | '__/ __|/ _ \/ __| |  | |\___ \|  ___/ 
      6 *        | | (_| | |  \__ \ (_) \__ \ |__| |____) | |     
      7 *        |_|\__,_|_|  |___/\___/|___/_____/|_____/|_|     
      8 *                                                         
      9 * -------------------------------------------------------------
     10 *
     11 * TarsosDSP is developed by Joren Six at IPEM, University Ghent
     12 *  
     13 * -------------------------------------------------------------
     14 *
     15 *  Info: http://0110.be/tag/TarsosDSP
     16 *  Github: https://github.com/JorenSix/TarsosDSP
     17 *  Releases: http://0110.be/releases/TarsosDSP/
     18 *  
     19 *  TarsosDSP includes modified source code by various authors,
     20 *  for credits and info, see README.
     21 * 
     22 */
     23 
     24 package be.tarsos.dsp.mfcc;
     25 
     26 import java.io.*;
     27 import java.util.StringTokenizer;
     28 
     29 public class DCT {
     30 
     31 	int f[][];
     32 	int g[][];
     33 	int inv[][];
     34 
     35 	static public void main(String args[]) {
     36 
     37 		int fm[][] = new int[8][8];
     38 
     39 		if ( args.length != 1 ) {
     40 			System.out.println("usage: java DCT <matrix-filename>");
     41 			return;
     42 		}
     43 
     44 		File f = new File(args[0]);
     45 		if ( !f.canRead() ) {
     46 			System.out.println("Error! can't open "+args[0]+" for reading");
     47 			return;
     48 		}
     49 		try {
     50 			@SuppressWarnings("resource")
     51 			BufferedReader br = new BufferedReader(new FileReader(f));
     52 			for ( int i = 0; i < 8; i++ ) {
     53 				String line = br.readLine();
     54 				StringTokenizer tok = new StringTokenizer(line,", ");
     55 				if ( tok.countTokens() != 8 ) {
     56 					System.out.println("Error! File format error: 8 tokens required!");
     57 					throw new IOException("Error");
     58 				}
     59 				for ( int j = 0; j < 8; j++ ) {
     60 					String numstr = tok.nextToken();
     61 					int num = Integer.parseInt(numstr);
     62 					fm[i][j] = num;
     63 				}
     64 			}
     65 			br.close();
     66 		}
     67 		catch ( FileNotFoundException e ) {
     68 			System.out.println("Error! can't create FileReader for "+args[0]);
     69 			return;
     70 		}
     71 		catch ( IOException e ) {
     72 			System.out.println("Error! during read of "+args[0]);
     73 			return;
     74 		}
     75 		catch ( NumberFormatException e ) {
     76 			System.out.println("Error! NumberFormatExecption");
     77 			return;
     78 		}
     79 
     80 		DCT dct = new DCT(fm);
     81 		dct.transform();
     82 		dct.printout();
     83 		dct.inverse();
     84 		dct.printoutinv();
     85 	}
     86 
     87 	public DCT(int f[][]) {
     88 		this.f = f;
     89 	}
     90 
     91 	public void transform() {
     92 		g = new int[8][8];
     93 
     94 		for ( int i = 0; i < 8; i++ ) {
     95 			for ( int j = 0; j < 8; j++ ) {
     96 				double ge = 0.0;
     97 				for ( int x = 0; x < 8; x++ ) {
     98 					for ( int y = 0; y < 8; y++ ) {
     99 						double cg1 = (2.0*(double)x+1.0)*(double)i*Math.PI/16.0;
    100 						double cg2 = (2.0*(double)y+1.0)*(double)j*Math.PI/16.0;
    101 
    102 						ge += ((double)f[x][y]) * Math.cos(cg1) * Math.cos(cg2);
    103 
    104 					}
    105 				}						
    106 				double ci = ((i==0)?1.0/Math.sqrt(2.0):1.0);
    107 				double cj = ((j==0)?1.0/Math.sqrt(2.0):1.0);
    108 				ge *= ci * cj * 0.25;
    109 				g[i][j] = (int)Math.round(ge);
    110 			}
    111 		}
    112 	}
    113 
    114 
    115 	public void inverse() {
    116 		inv = new int[8][8];
    117 
    118 		for ( int x = 0; x < 8; x++ ) {
    119 			for ( int y = 0; y < 8; y++ ) {
    120 				double ge = 0.0;
    121 				for ( int i = 0; i < 8; i++ ) {
    122 					double cg1 = (2.0*(double)x + 1.0)*(double)i*Math.PI/16.0;
    123 					double ci = ((i==0)?1.0/Math.sqrt(2.0):1.0);
    124 					for ( int j = 0; j < 8; j++ ) {
    125 						double cg2 = (2.0*(double)y + 1.0)*(double)j*Math.PI/16.0;
    126 						double cj = ((j==0)?1.0/Math.sqrt(2.0):1.0);
    127 						double cij4 = ci*cj*0.25;
    128 						ge += cij4 * Math.cos(cg1) * Math.cos(cg2) * (double)g[i][j];
    129 					}
    130 				}
    131 				inv[x][y] = (int)Math.round(ge);
    132 			}
    133 		}
    134 	}	
    135 					
    136 	public void printout() {
    137 		for ( int i = 0; i < 8; i++ ) {
    138 			System.out.print("\n");
    139 			for ( int k = 0; k < 8; k++ ) {
    140 				System.out.print(g[i][k]+" ");
    141 			}
    142 		}
    143 	}
    144 
    145 	public void printoutinv() {
    146 		for ( int i = 0; i < 8; i++ ) {
    147 			System.out.print("\n");
    148 			for ( int k = 0; k < 8; k++ ) {
    149 				System.out.print(inv[i][k]+" ");
    150 			}
    151 		}
    152 	}	
    153 }
    154 
    155