001/******************************************************************************* 002 * This software is provided as a supplement to the authors' textbooks on digital 003 * image processing published by Springer-Verlag in various languages and editions. 004 * Permission to use and distribute this software is granted under the BSD 2-Clause 005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit http://imagingbook.com for additional details. 008 *******************************************************************************/ 009package imagingbook.pub.dct; 010 011import imagingbook.lib.math.Matrix; 012import imagingbook.lib.settings.PrintPrecision; 013 014public class DctMatrixTest { 015 016 017 /** 018 * Test method. 019 * @param args command arguments 020 */ 021 public static void main(String[] args) { 022 PrintPrecision.set(5); 023 float[][] A = makeDctMatrix(4, 4); 024 System.out.println("A = " + Matrix.toString(A)); 025 System.out.println(); 026 027 float[][] At = Matrix.transpose(A); 028 System.out.println("At = " + Matrix.toString(At)); 029 System.out.println(); 030 031 Dct2d dct = new Dct2d(); 032 float[][] g = { 033 {1,2,3,4}, 034 {7,2,0,9}, 035 {6,5,2,5}, 036 {0,9,8,1}}; 037 038 float[][] g1 = Matrix.duplicate(g); 039 dct.DCT(g1); 040 System.out.println("G1 = " + Matrix.toString(g1)); 041 System.out.println(); 042 043 float[][] g2 = Matrix.duplicate(g); 044 float[][] G2 = Matrix.multiply(A, Matrix.multiply(g2, At)); 045 System.out.println("G2 = " + Matrix.toString(G2)); 046 System.out.println(); 047 048 float[][] g2r = Matrix.multiply(At, Matrix.multiply(G2, A)); 049 System.out.println("g2r = " + Matrix.toString(g2r)); 050 051 float[][] I1 = Matrix.multiply(At, A); 052 System.out.println("I1 = " + Matrix.toString(I1)); 053 054 float[][] I2 = Matrix.multiply(A, At); 055 System.out.println("I2 = " + Matrix.toString(I2)); 056 } 057 058 static float[][] makeDctMatrix(int M, int N) { 059 float[][] A = new float[M][N]; 060 for (int i = 0; i < M; i++) { 061 double c_i = (i == 0) ? 1.0 / Math.sqrt(2) : 1; 062 for (int j = 0; j < N; j++) { 063 A[i][j] = (float) 064// \sqrt{\tfrac{2}{N}} \cdot c_i \cdot 065// \cos\Bigl(\frac{\pi \cdot (2j + 1) \cdot i}{2M}\Bigr) , 066 (Math.sqrt(2.0/N) * c_i * Math.cos(Math.PI * (2*j + 1) * i / (2.0 * M)) ); 067 } 068 } 069 070 return A; 071 } 072 073}