SHOGUN
v2.0.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2011 Siddharth Kherada 00008 * Copyright (C) 2007-2011 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/lib/common.h> 00012 #include <shogun/kernel/WaveletKernel.h> 00013 #include <shogun/features/DenseFeatures.h> 00014 00015 using namespace shogun; 00016 00017 CWaveletKernel::CWaveletKernel() : CDotKernel(), Wdilation(0.0), Wtranslation(0.0) 00018 { 00019 init(); 00020 } 00021 00022 CWaveletKernel::CWaveletKernel(int32_t size, float64_t a, float64_t c) 00023 : CDotKernel(size), Wdilation(a), Wtranslation(c) 00024 { 00025 init(); 00026 } 00027 00028 CWaveletKernel::CWaveletKernel( 00029 CDotFeatures* l, CDotFeatures* r, int32_t size, float64_t a, float64_t c) 00030 : CDotKernel(size), Wdilation(a), Wtranslation(c) 00031 { 00032 init(); 00033 init(l,r); 00034 } 00035 00036 CWaveletKernel::~CWaveletKernel() 00037 { 00038 cleanup(); 00039 } 00040 00041 void CWaveletKernel::cleanup() 00042 { 00043 } 00044 00045 bool CWaveletKernel::init(CFeatures* l, CFeatures* r) 00046 { 00047 CDotKernel::init(l, r); 00048 return init_normalizer(); 00049 } 00050 00051 void CWaveletKernel::init() 00052 { 00053 SG_ADD(&Wdilation, "Wdilation", "Dilation coefficient", MS_AVAILABLE); 00054 SG_ADD(&Wtranslation, "Wtranslaton", "Translation coefficient", MS_AVAILABLE); 00055 } 00056 00057 float64_t CWaveletKernel::compute(int32_t idx_a, int32_t idx_b) 00058 { 00059 int32_t alen, blen; 00060 bool afree, bfree; 00061 00062 float64_t* avec= 00063 ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00064 float64_t* bvec= 00065 ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); 00066 ASSERT(alen==blen); 00067 00068 float64_t result=1; 00069 00070 for (int32_t i=0; i<alen; i++) 00071 { 00072 if (Wtranslation !=0) 00073 { 00074 float64_t h1=(avec[i]-Wdilation)/Wtranslation; 00075 float64_t h2=(bvec[i]-Wdilation)/Wtranslation; 00076 float64_t res1=MotherWavelet(h1); 00077 float64_t res2=MotherWavelet(h2); 00078 result=result*res1*res2; 00079 } 00080 } 00081 00082 ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00083 ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00084 00085 return result; 00086 }