00001 #ifndef Impala_Core_Matrix_VectorQuantize_h
00002 #define Impala_Core_Matrix_VectorQuantize_h
00003
00004 #include "Core/Matrix/MatFunc.h"
00005 #include "Core/Matrix/MatNorm2Dist.h"
00006 #include "Core/Matrix/Mat.h"
00007
00008 namespace Impala
00009 {
00010 namespace Core
00011 {
00012 namespace Matrix
00013 {
00014
00015
00016 Mat* VectorQuantizeX(Mat* a, Mat* codebook)
00017 {
00018 Mat* distances = 0;
00019 #ifdef CUDA
00020 if(Link::Cuda::CudaUsed())
00021 {
00022
00023 return Link::Cuda::MatCodebookAssignHard(a, codebook);
00024 }
00025 else
00026 #endif
00027 {
00028 distances = MatNorm2DistTransposed(a,codebook);
00029 Mat* res = MatCreate<Mat>(MatNrRow(a), 2);
00030 for(int vj = 0; vj < MatNrRow(a); vj++)
00031 {
00032 Real64 bestDistance = 999999999.0;
00033 int bestIndex = 0;
00034 for(int i = 0; i < MatNrRow(codebook); i++)
00035 {
00036 Real64 value = *MatE(distances, vj, i);
00037 if(value < bestDistance)
00038 {
00039 bestDistance = value;
00040 bestIndex = i;
00041 }
00042 }
00043 *MatE(res, vj, 0) = bestIndex;
00044 *MatE(res, vj, 1) = bestDistance;
00045 }
00046 delete distances;
00047 return res;
00048 }
00049 }
00050
00051
00052 Mat* VectorQuantize(Mat* data, Mat* codebook)
00053 {
00054 Timer vqTimer;
00055 int step = 10000;
00056 Mat* res = MatCreate<Mat>(MatNrRow(data), 2);
00057
00058
00059 for(int i = 0; i < MatNrRow(data); i += step)
00060 {
00061 int s = MatNrRow(data) - i;
00062 if(step < s) s = step;
00063
00064 Mat* partial = MatCreate<Mat>(s, MatNrCol(data), MatE(data, i, 0), true);
00065
00066 Mat* temp = VectorQuantizeX(partial, codebook);
00067
00068 for(int j = 0; j < s; j++)
00069 {
00070 *MatE(res, i + j, 0) = *MatE(temp, j, 0);
00071 *MatE(res, i + j, 1) = *MatE(temp, j, 1);
00072 }
00073 delete temp;
00074 delete partial;
00075 }
00076 return res;
00077 }
00078
00079
00080 }
00081 }
00082 }
00083
00084 #endif