/matlab/plf_preimages.m

https://bitbucket.org/tetonedge/libsrvf · Objective C · 110 lines · 104 code · 6 blank · 0 comment · 11 complexity · f9d33dfb6db2743ab0fdf4997797b38e MD5 · raw file

  1. % libsrvf
  2. % =======
  3. %
  4. % A shape analysis library using the square root velocity framework.
  5. %
  6. % Copyright (C) 2012 FSU Statistical Shape Analysis and Modeling Group
  7. %
  8. % This program is free software: you can redistribute it and/or modify
  9. % it under the terms of the GNU General Public License as published by
  10. % the Free Software Foundation, either version 3 of the License, or
  11. % (at your option) any later version.
  12. %
  13. % This program is distributed in the hope that it will be useful,
  14. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. % GNU General Public License for more details.
  17. %
  18. % You should have received a copy of the GNU General Public License
  19. % along with this program. If not, see <http://www.gnu.org/licenses/>
  20. % --------------------------------------------------------------------
  21. % Computes preimages of the values in xv under the non-decreasing
  22. % piecewise-linear function F,T. The function must be a 1-D function,
  23. % and xv must be non-decreasing.
  24. %
  25. % If F is not a one-to-one function (i.e. F(i)==F(i+1) for some i),
  26. % then there may be more than one preimage for a given value xv(j).
  27. % In this case, there is at the moment no convention determining
  28. % which preimage is returned for such a value. The only guarantee
  29. % is that if you do this
  30. %
  31. % xvi=plf_preimages(F,T,xv);
  32. % Fxvi=plf_evaluate(F,T,xvi);
  33. %
  34. % then Fxvi will be equal (or at least close) to xv.
  35. % TODO: is there a sensible convention for choosing preimages?
  36. %
  37. % Inputs
  38. % F,T: the function. F and T must be non-decreasing.
  39. % xv: the sample points. Must be non-decreasing.
  40. %
  41. % Outputs
  42. % xvi: preimages of xv
  43. % --------------------------------------------------------------------------
  44. function xvi = plf_preimages( F, T, xv )
  45. assert( size(F,1) == 1 );
  46. assert( min(diff(F)) >= 0 );
  47. assert( min(diff(T)) >= 0 );
  48. xvi = zeros(1,length(xv));
  49. Fidx = 1;
  50. for xvidx = 1:length(xv)
  51. while( Fidx < length(F)-1 && xv(xvidx) > F(Fidx+1) )
  52. Fidx = Fidx+1;
  53. end
  54. dF = F(Fidx+1) - F(Fidx);
  55. if ( dF > 1e-4 )
  56. w1 = (F(Fidx+1) - xv(xvidx)) / dF;
  57. w2 = (xv(xvidx) - F(Fidx)) / dF;
  58. xvi(xvidx) = w1 * T(Fidx) + w2 * T(Fidx+1);
  59. else
  60. xvi(xvidx) = T(Fidx);
  61. end
  62. end
  63. end
  64. %!function v=_random_increasing_vector(v0,n)
  65. %! v=zeros(1,n);
  66. %! v(1)=v0;
  67. %! for i=2:n
  68. %! v(i)=v(i-1)+rand();
  69. %! end
  70. %! end
  71. %!
  72. %!test
  73. %! F=linspace(0,1,5);
  74. %! T=linspace(0,1,5);
  75. %! xv=linspace(0,1,100);
  76. %! xvi=plf_preimages(F,T,xv);
  77. %! assert(xvi,xv,1e-5);
  78. %!
  79. %!test
  80. %! F=[0 0 1/2 1/2 1 1];
  81. %! T=linspace(0,1,6);
  82. %! tv=linspace(0,1,100);
  83. %! xv=[0 0.2 0.499 0.5 0.501 0.999 1];
  84. %! xvie=[0 0.28 0.3996 0.4 0.6004 0.7996 0.8];
  85. %! xvi=plf_preimages(F,T,xv);
  86. %! assert(xvi,xvie,1e-4);
  87. %!
  88. %!test
  89. %! F=_random_increasing_vector(-100*rand(),50);
  90. %! T=_random_increasing_vector(-5*rand(),50);
  91. %! tv=linspace(T(1),T(end),100);
  92. %! xv=interp1(T,F,tv);
  93. %! xvi=plf_preimages(F,T,xv);
  94. %! assert(xvi,tv,1e-4)
  95. %!
  96. %!test
  97. %! F=_random_increasing_vector(0,1000);
  98. %! F=F/F(end);
  99. %! T=linspace(0,1,length(F));
  100. %! tv=linspace(T(1),T(end),2000);
  101. %! xv=interp1(T,F,tv);
  102. %! xvi=plf_preimages(F,T,xv);
  103. %! Fxvi=plf_evaluate(F,T,xvi);
  104. %! assert(Fxvi,xv,1e-4)