You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
19 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file homkermap.c
  2. ** @brief Homogeneous kernel map - Definition
  3. ** @author Andrea Vedaldi
  4. **/
  5. /*
  6. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  7. Copyright (C) 2013 Andrea Vedaldi.
  8. All rights reserved.
  9. This file is part of the VLFeat library and is made available under
  10. the terms of the BSD license (see the COPYING file).
  11. */
  12. /** @file homkermap.h
  13. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  14. @page homkermap Homogeneous kernel map
  15. @author Andrea Vedaldi
  16. @tableofcontents
  17. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  18. @ref homkermap.h implements the homogeneous kernel maps introduced in
  19. @cite{vedaldi10efficient},@cite{vedaldi12efficient}. Such maps are
  20. efficient linear representations of popular kernels such as the
  21. intersection, $\chi^2$, and Jensen-Shannon ones.
  22. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  23. @section homkermap-starting Getting started
  24. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  25. The homogeneous kernel map is implemented as an object of type
  26. ::VlHomogeneousKernelMap. To use thois object, first create an
  27. instance by using ::vl_homogeneouskernelmap_new, then use
  28. ::vl_homogeneouskernelmap_evaluate_d or
  29. ::vl_homogeneouskernelmap_evaluate_f (depdening on whether the data is
  30. @c double or @c float) to compute the feature map $ \Psi(x)
  31. $. When done, dispose of the object by calling
  32. ::vl_homogeneouskernelmap_delete.
  33. @code
  34. double gamma = 1.0 ;
  35. int order = 1 ;
  36. double period = -1 ; // use default
  37. double psi [3] ;
  38. vl_size psiStride = 1 ;
  39. double x = 0.5 ;
  40. VlHomogeneousKernelMap * hom = vl_homogeneouskernelmap_new(
  41. VlHomogeneousKernelChi2, gamma, order, period,
  42. VlHomogeneousKernelMapWindowRectangular) ;
  43. vl_homogeneouskernelmap_evaluate_d(hom, psi, psiStride, x) ;
  44. vl_homogeneouskernelmap_delete(x) ;
  45. @endcode
  46. The constructor ::vl_homogeneouskernelmap_new takes the kernel type @c
  47. kernel (see ::VlHomogeneousKernelType), the homogeneity order @c gamma
  48. (use one for the standard $1$-homogeneous kernels), the approximation
  49. order @c order (usually order one is enough), the period @a period
  50. (use a negative value to use the default period), and a window type @c
  51. window (use ::VlHomogeneousKernelMapWindowRectangular if unsure). The
  52. approximation order trades off the quality and dimensionality of the
  53. approximation. The resulting feature map $ \Psi(x) $, computed by
  54. ::vl_homogeneouskernelmap_evaluate_d or
  55. ::vl_homogeneouskernelmap_evaluate_f , is <code>2*order+1</code>
  56. dimensional.
  57. The code pre-computes the map $ \Psi(x) $ for efficient
  58. evaluation. The table spans values of $ x $ in the range
  59. $[2^{-20}, 2^{8}) $. In particular, values smaller than $
  60. 2^{-20} $ are treated as zeroes (which results in a null feature).
  61. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  62. @section homkermap-fundamentals Fundamentals
  63. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  64. The <em>homogeneous kernel map</em> is a finite dimensional linear
  65. approximation of homogeneous kernels, including the intersection,
  66. $\chi^2$, and Jensen-Shannon kernels. These kernels are frequently
  67. used in computer vision applications because they are particular
  68. suited to data in the format of histograms, which includes many common
  69. visual descriptors.
  70. Let $x,y \in \mathbb{R}_+$ be non-negative scalars and let $k(x,y) \in
  71. \mathbb{R}$ be an homogeneous kernel such as the $\chi^2$ and or the
  72. intersection ones:
  73. @f[
  74. k_{\mathrm{inters}}(x,y) = \min\{x, y\},
  75. \quad
  76. k_{\chi^2}(x,y) = 2 \frac{(x - y)^2}{x+y}.
  77. @f]
  78. For vectorial data $ \mathbf{x},\mathbf{y} \in \mathbb{R}_+^d $, the
  79. homogeneous kernels is defined as an <em>additive combination</em> of
  80. scalar kernels $K(\mathbf{x},\mathbf{y}) = \sum_{i=1}^d k(x_i,y_i)$.
  81. The <em>homogeneous kernel map</em> of order $n$ is a vector function
  82. $\Psi(x) \in \mathbb{R}^{2n+1}$ such that, for any choice of $x, y \in
  83. \mathbb{R}_+$, the following approximation holds:
  84. @f[
  85. k(x,y) \approx \langle \Psi(x), \Psi(y) \rangle.
  86. @f]
  87. Given the feature map for the scalar case, the corresponding feature
  88. map $\Psi(\mathbf{x})$ for the vectorial case is obtained by stacking
  89. $[\Psi(x_1), \dots, \Psi(x_n)]$. Note that the stacked feature
  90. $\Psi(\mathbf{x})$ has dimension $d(2n+1)$.
  91. Using linear analysis tools (e.g. a linear support vector machine)
  92. on top of dataset that has been encoded by the homogeneous kernel map
  93. is therefore approximately equivalent to using a method based
  94. on the corresponding non-linear kernel.
  95. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  96. @subsection homkermap-overview-negative Extension to the negative reals
  97. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  98. Any positive (semi-)definite kernel $k(x,y)$ defined on the
  99. non-negative reals $x,y \in \mathbb{R}_+$ can be extended to the
  100. entire real line by using the definition:
  101. @f[
  102. k_\pm(x,y) = \operatorname{sign}(x) \operatorname{sign}(y) k(|x|,|y|).
  103. @f]
  104. The homogeneous kernel map implements this extension by defining
  105. $\Psi_\pm(x) = \operatorname{sign}(x) \Psi(|x|)$. Note that other
  106. extensions are possible, such as
  107. @f[
  108. k_\pm(x,y) = H(xy) \operatorname{sign}(y) k(|x|,|y|)
  109. @f]
  110. where $H$ is the Heaviside function, but may result in higher
  111. dimensional feature maps.
  112. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  113. @subsection homkermap-overview-homogeneity Homogeneity degree
  114. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  115. Any (1-)homogeneous kernel $k_1(x,y)$ can be extended to a so called
  116. $\gamma$-homgeneous kernel $k_\gamma(x,y)$ by the definition
  117. @f[
  118. k_\gamma(x,y) = (xy)^{\frac{\gamma}{2}} \frac{k_1(x,y)}{\sqrt{xy}}
  119. @f]
  120. Smaller values of $\gamma$ enhance the kernel non-linearity and are
  121. sometimes beneficial in applications (see
  122. @cite{vedaldi10efficient},@cite{vedaldi12efficient} for details).
  123. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  124. @subsection homkermap-overview-window Windowing and period
  125. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  126. This section discusses aspects of the homogeneous kernel map which are
  127. more technical and may be skipped. The homogeneous kernel map
  128. approximation is based on periodizing the kernel; given the kernel
  129. signature
  130. @f[
  131. \mathcal{K}(\lambda) = k(e^{\frac{\lambda}{2}}, e^{-\frac{\lambda}{2}})
  132. @f]
  133. the homogeneous kernel map is a feature map for the windowed and
  134. periodized kernel whose signature is given by
  135. @f[
  136. \hat{\mathcal{K}}(\lambda)
  137. =
  138. \sum_{i=-\infty}^{+\infty} \mathcal{K}(\lambda + k \Lambda) W(\lambda + k \Lambda)
  139. @f]
  140. where $W(\lambda)$ is a windowing function and $\Lambda$ is the
  141. period. This implementation of the homogeneous kernel map supports the
  142. use of a <em>uniform window</em> ($ W(\lambda) = 1 $) or of a
  143. <em>rectangular window</em> ($ W(\lambda) =
  144. \operatorname{rect}(\lambda/\Lambda) $). Note that $ \lambda =
  145. \log(y/x) $ is equal to the logarithmic ratio of the arguments of the
  146. kernel. Empirically, the rectangular window seems to have a slight
  147. edge in applications.
  148. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  149. @section homkermap-details Implementation details
  150. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  151. This implementation uses the expressions given in
  152. @cite{vedaldi10efficient},@cite{vedaldi11efficient} to compute in
  153. closed form the maps $\Psi(x)$ for the supported kernel types. For
  154. efficiency reasons, it precomputes $\Psi(x)$ for a large range of
  155. values of the argument when the homogeneous kernel map object is
  156. created.
  157. The internal table stores $\Psi(x) \in \mathbb{R}^{2n+1}$ by sampling
  158. $x\geq 0$. This uses the internal decomposition of IEEE floating point
  159. representations (@c float and @c double) in mantissa and exponent:
  160. <pre>
  161. x = mantissa * (2**exponent),
  162. minExponent <= exponent <= maxExponent,
  163. 1 <= matnissa < 2.
  164. </pre>
  165. Each octave is further sampled in @c numSubdivisions sublevels.
  166. When the map $\Psi(x)$ is evaluated, @c x is decomposed again into
  167. exponent and mantissa to index the table. The output is obtained by
  168. bilinear interpolation from the appropriate table entries.
  169. **/
  170. /* ---------------------------------------------------------------- */
  171. #ifndef VL_HOMKERMAP_INSTANTIATING
  172. /* ---------------------------------------------------------------- */
  173. #include "homkermap.h"
  174. #include "mathop.h"
  175. #include <math.h>
  176. struct _VlHomogeneousKernelMap
  177. {
  178. VlHomogeneousKernelType kernelType ;
  179. double gamma ;
  180. VlHomogeneousKernelMapWindowType windowType ;
  181. vl_size order ;
  182. double period ;
  183. vl_size numSubdivisions ;
  184. double subdivision ;
  185. vl_index minExponent ;
  186. vl_index maxExponent ;
  187. double * table ;
  188. } ;
  189. /** @internal @brief Sample the kernel specturm
  190. ** @param self homogeneous kernel map.
  191. ** @param omega sampling frequency.
  192. ** @return the spectrum sampled at @a omega.
  193. **/
  194. VL_INLINE double
  195. vl_homogeneouskernelmap_get_spectrum (VlHomogeneousKernelMap const * self, double omega)
  196. {
  197. assert (self) ;
  198. switch (self->kernelType) {
  199. case VlHomogeneousKernelIntersection:
  200. return (2.0 / VL_PI) / (1 + 4 * omega*omega) ;
  201. case VlHomogeneousKernelChi2:
  202. return 2.0 / (exp(VL_PI * omega) + exp(-VL_PI * omega)) ;
  203. case VlHomogeneousKernelJS:
  204. return (2.0 / log(4.0)) *
  205. 2.0 / (exp(VL_PI * omega) + exp(-VL_PI * omega)) /
  206. (1 + 4 * omega*omega) ;
  207. default:
  208. abort() ;
  209. }
  210. return 0.0;
  211. }
  212. /* helper */
  213. VL_INLINE double sinc(double x)
  214. {
  215. if (x == 0.0) return 1.0 ;
  216. return sin(x) / x ;
  217. }
  218. /** @internal @brief Sample the smoothed kernel spectrum
  219. ** @param self homogeneous kernel map.
  220. ** @param omega sampling frequency.
  221. ** @return the spectrum sampled at @a omega after smoothing.
  222. **/
  223. VL_INLINE double
  224. vl_homogeneouskernelmap_get_smooth_spectrum (VlHomogeneousKernelMap const * self, double omega)
  225. {
  226. double kappa_hat = 0 ;
  227. double omegap ;
  228. double epsilon = 1e-2 ;
  229. double const omegaRange = 2.0 / (self->period * epsilon) ;
  230. double const domega = 2 * omegaRange / (2 * 1024.0 + 1) ;
  231. assert (self) ;
  232. switch (self->windowType) {
  233. case VlHomogeneousKernelMapWindowUniform:
  234. kappa_hat = vl_homogeneouskernelmap_get_spectrum(self, omega) ;
  235. break ;
  236. case VlHomogeneousKernelMapWindowRectangular:
  237. for (omegap = - omegaRange ; omegap <= omegaRange ; omegap += domega) {
  238. double win = sinc((self->period/2.0) * omegap) ;
  239. win *= (self->period/(2.0*VL_PI)) ;
  240. kappa_hat += win * vl_homogeneouskernelmap_get_spectrum(self, omegap + omega) ;
  241. }
  242. kappa_hat *= domega ;
  243. /* project on the postivie orthant (see PAMI) */
  244. kappa_hat = VL_MAX(kappa_hat, 0.0) ;
  245. break ;
  246. default:
  247. abort() ;
  248. }
  249. return kappa_hat ;
  250. }
  251. /* ---------------------------------------------------------------- */
  252. /* Constructors and destructors */
  253. /* ---------------------------------------------------------------- */
  254. /** @brief Create a new homgeneous kernel map
  255. ** @param kernelType type of homogeneous kernel.
  256. ** @param gamma kernel homogeneity degree.
  257. ** @param order approximation order.
  258. ** @param period kernel period.
  259. ** @param windowType type of window used to truncate the kernel.
  260. ** @return the new homogeneous kernel map.
  261. **
  262. ** The function intializes a new homogeneous kernel map for the
  263. ** specified kernel type, homogeneity degree, approximation order,
  264. ** period, and truncation window. See @ref homkermap-fundamentals for
  265. ** details.
  266. **
  267. ** The homogeneity degree @c gamma must be positive (the standard
  268. ** kernels are obtained by setting @c gamma to 1). When unsure, set
  269. ** @c windowType to ::VlHomogeneousKernelMapWindowRectangular. The @c
  270. ** period should be non-negative; specifying a negative or null value
  271. ** causes the function to switch to a default value.
  272. **
  273. ** The function returns @c NULL if there is not enough free memory.
  274. **/
  275. VlHomogeneousKernelMap *
  276. vl_homogeneouskernelmap_new (VlHomogeneousKernelType kernelType,
  277. double gamma,
  278. vl_size order,
  279. double period,
  280. VlHomogeneousKernelMapWindowType windowType)
  281. {
  282. int tableWidth, tableHeight ;
  283. VlHomogeneousKernelMap * self = (VlHomogeneousKernelMap*)vl_malloc(sizeof(VlHomogeneousKernelMap)) ;
  284. if (! self) return NULL ;
  285. assert(gamma > 0) ;
  286. assert(kernelType == VlHomogeneousKernelIntersection ||
  287. kernelType == VlHomogeneousKernelChi2 ||
  288. kernelType == VlHomogeneousKernelJS) ;
  289. assert(windowType == VlHomogeneousKernelMapWindowUniform ||
  290. windowType == VlHomogeneousKernelMapWindowRectangular) ;
  291. if (period < 0) {
  292. switch (windowType) {
  293. case VlHomogeneousKernelMapWindowUniform:
  294. switch (kernelType) {
  295. case VlHomogeneousKernelChi2: period = 5.86 * sqrt((double)order + 0) + 3.65 ; break ;
  296. case VlHomogeneousKernelJS: period = 6.64 * sqrt((double)order + 0) + 7.24 ; break ;
  297. case VlHomogeneousKernelIntersection: period = 2.38 * log((double)order + 0.8) + 5.6 ; break ;
  298. }
  299. break ;
  300. case VlHomogeneousKernelMapWindowRectangular:
  301. switch (kernelType) {
  302. case VlHomogeneousKernelChi2: period = 8.80 * sqrt((double)order + 4.44) - 12.6 ; break ;
  303. case VlHomogeneousKernelJS: period = 9.63 * sqrt((double)order + 1.00) - 2.93; break ;
  304. case VlHomogeneousKernelIntersection: period = 2.00 * log((double)order + 0.99) + 3.52 ; break ;
  305. }
  306. break ;
  307. }
  308. period = VL_MAX(period, 1.0) ;
  309. }
  310. self->kernelType = kernelType ;
  311. self->windowType = windowType ;
  312. self->gamma = gamma ;
  313. self->order = order ;
  314. self->period = period ;
  315. self->numSubdivisions = 8 + 8*order ;
  316. self->subdivision = 1.0 / self->numSubdivisions ;
  317. self->minExponent = -20 ;
  318. self->maxExponent = 8 ;
  319. tableHeight = (int) (2*self->order + 1) ;
  320. tableWidth = (int) (self->numSubdivisions * (self->maxExponent - self->minExponent + 1)) ;
  321. self->table = (double*)vl_malloc (sizeof(double) *
  322. (tableHeight * tableWidth + 2*(1+self->order))) ;
  323. if (! self->table) {
  324. vl_free(self) ;
  325. return NULL ;
  326. }
  327. {
  328. vl_index exponent ;
  329. vl_uindex i, j ;
  330. double * tablep = self->table ;
  331. double * kappa = self->table + tableHeight * tableWidth ;
  332. double * freq = kappa + (1+self->order) ;
  333. double L = 2.0 * VL_PI / self->period ;
  334. /* precompute the sampled periodicized spectrum */
  335. j = 0 ;
  336. i = 0 ;
  337. while (i <= self->order) {
  338. freq[i] = j ;
  339. kappa[i] = vl_homogeneouskernelmap_get_smooth_spectrum(self, j * L) ;
  340. ++ j ;
  341. if (kappa[i] > 0 || j >= 3*i) ++ i ;
  342. }
  343. /* fill table */
  344. for (exponent = self->minExponent ;
  345. exponent <= self->maxExponent ; ++ exponent) {
  346. double x, Lxgamma, Llogx, xgamma ;
  347. double sqrt2kappaLxgamma ;
  348. double mantissa = 1.0 ;
  349. for (i = 0 ; i < self->numSubdivisions ;
  350. ++i, mantissa += self->subdivision) {
  351. x = ldexp(mantissa, (int)exponent) ;
  352. xgamma = pow(x, self->gamma) ;
  353. Lxgamma = L * xgamma ;
  354. Llogx = L * log(x) ;
  355. *tablep++ = sqrt(Lxgamma * kappa[0]) ;
  356. for (j = 1 ; j <= self->order ; ++j) {
  357. sqrt2kappaLxgamma = sqrt(2.0 * Lxgamma * kappa[j]) ;
  358. *tablep++ = sqrt2kappaLxgamma * cos(freq[j] * Llogx) ;
  359. *tablep++ = sqrt2kappaLxgamma * sin(freq[j] * Llogx) ;
  360. }
  361. } /* next mantissa */
  362. } /* next exponent */
  363. }
  364. return self ;
  365. }
  366. /** @brief Delete an object instance.
  367. ** @param self object.
  368. ** The function deletes the specified map object.
  369. **/
  370. void
  371. vl_homogeneouskernelmap_delete (VlHomogeneousKernelMap * self)
  372. {
  373. vl_free(self->table) ;
  374. self->table = NULL ;
  375. vl_free(self) ;
  376. }
  377. /* ---------------------------------------------------------------- */
  378. /* Retrieve data and parameters */
  379. /* ---------------------------------------------------------------- */
  380. /** @brief Get the map order.
  381. ** @param self object.
  382. ** @return the map order.
  383. **/
  384. vl_size
  385. vl_homogeneouskernelmap_get_order (VlHomogeneousKernelMap const * self)
  386. {
  387. assert(self) ;
  388. return self->order ;
  389. }
  390. /** @brief Get the map dimension.
  391. ** @param self object.
  392. ** @return the map dimension (2 @c order +1).
  393. **/
  394. vl_size
  395. vl_homogeneouskernelmap_get_dimension (VlHomogeneousKernelMap const * self)
  396. {
  397. assert(self) ;
  398. return 2 * self->order + 1 ;
  399. }
  400. /** @brief Get the kernel type.
  401. ** @param self object.
  402. ** @return kernel type.
  403. **/
  404. VlHomogeneousKernelType
  405. vl_homogeneouskernelmap_get_kernel_type (VlHomogeneousKernelMap const * self)
  406. {
  407. assert(self) ;
  408. return self->kernelType ;
  409. }
  410. /** @brief Get the window type.
  411. ** @param self object.
  412. ** @return window type.
  413. **/
  414. VlHomogeneousKernelMapWindowType
  415. vl_homogeneouskernelmap_get_window_type (VlHomogeneousKernelMap const * self)
  416. {
  417. assert(self) ;
  418. return self->windowType ;
  419. }
  420. /* ---------------------------------------------------------------- */
  421. /* Process data */
  422. /* ---------------------------------------------------------------- */
  423. /** @fn ::vl_homogeneouskernelmap_evaluate_d(VlHomogeneousKernelMap const*,double*,vl_size,double)
  424. ** @brief Evaluate map
  425. ** @param self map object.
  426. ** @param destination output buffer.
  427. ** @param stride stride of the output buffer.
  428. ** @param x value to expand.
  429. **
  430. ** The function evaluates the feature map on @a x and stores the
  431. ** resulting <code>2*order+1</code> dimensional vector to
  432. ** @a destination[0], @a destination[stride], @a destination[2*stride], ....
  433. **/
  434. /** @fn ::vl_homogeneouskernelmap_evaluate_f(VlHomogeneousKernelMap const*,float*,vl_size,double)
  435. ** @copydetails ::vl_homogeneouskernelmap_evaluate_d(VlHomogeneousKernelMap const*,double*,vl_size,double)
  436. **/
  437. #define FLT VL_TYPE_FLOAT
  438. #define VL_HOMKERMAP_INSTANTIATING
  439. #include "homkermap.c"
  440. #define FLT VL_TYPE_DOUBLE
  441. #define VL_HOMKERMAP_INSTANTIATING
  442. #include "homkermap.c"
  443. /* VL_HOMKERMAP_INSTANTIATING */
  444. #endif
  445. /* ---------------------------------------------------------------- */
  446. #ifdef VL_HOMKERMAP_INSTANTIATING
  447. /* ---------------------------------------------------------------- */
  448. #include "float.th"
  449. void
  450. VL_XCAT(vl_homogeneouskernelmap_evaluate_,SFX)
  451. (VlHomogeneousKernelMap const * self,
  452. T * destination,
  453. vl_size stride,
  454. double x)
  455. {
  456. /* break value into exponent and mantissa */
  457. int exponent ;
  458. int unsigned j ;
  459. double mantissa = frexp(x, &exponent) ;
  460. double sign = (mantissa >= 0.0) ? +1.0 : -1.0 ;
  461. mantissa *= 2*sign ;
  462. exponent -- ;
  463. if (mantissa == 0 ||
  464. exponent <= self->minExponent ||
  465. exponent >= self->maxExponent) {
  466. for (j = 0 ; j < 2*self->order+1 ; ++j) {
  467. *destination = (T) 0.0 ;
  468. destination += stride ;
  469. }
  470. return ;
  471. }
  472. {
  473. vl_size featureDimension = 2*self->order + 1 ;
  474. double const * v1 = self->table +
  475. (exponent - self->minExponent) * self->numSubdivisions * featureDimension ;
  476. double const * v2 ;
  477. double f1, f2 ;
  478. mantissa -= 1.0 ;
  479. while (mantissa >= self->subdivision) {
  480. mantissa -= self->subdivision ;
  481. v1 += featureDimension ;
  482. }
  483. v2 = v1 + featureDimension ;
  484. for (j = 0 ; j < featureDimension ; ++j) {
  485. f1 = *v1++ ;
  486. f2 = *v2++ ;
  487. *destination = (T) sign * ((f2 - f1) * (self->numSubdivisions * mantissa) + f1) ;
  488. destination += stride ;
  489. }
  490. }
  491. }
  492. #undef FLT
  493. #undef VL_HOMKERMAP_INSTANTIATING
  494. /* VL_HOMKERMAP_INSTANTIATING */
  495. #endif