PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/R2008-02-16/main/irsa/inst/irsa_jitsp.m

#
MATLAB | 124 lines | 104 code | 3 blank | 17 comment | 1 complexity | fe4ecee246476ec7d8410be076bdfbcf MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. ## Copyright (C) 2003 Joerg Huber
  2. ##
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2 of the License, or
  6. ## (at your option) any later version.
  7. ##
  8. ## This program is distributed in the hope that it will be useful,
  9. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ## GNU General Public License for more details.
  12. ##
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this program; If not, see <http://www.gnu.org/licenses/>.
  15. ## -*- texinfo -*-
  16. ## @deftypefn {Function File} {irsa_jitsp.m}
  17. ## @var{jitxp} = irsa_jitsp (@var{Tm}, @var{jit}, @var{N} [, @var{rfunc}])
  18. ##
  19. ## Generate @var{N} sampling points with a minimum distance @var{Tm} and
  20. ## an additional random distance @var{jit} with random distribution
  21. ## @var{rfunc}
  22. ##
  23. ## Input:
  24. ##
  25. ## @var{Tm} : Scalar -- mean sampling period
  26. ##
  27. ## @var{N} : Scalar -- number of sampling points to generate
  28. ##
  29. ## @var{jit} : Scalar -- factor in [0,1] describing the part of Tm
  30. ## which is jittered.
  31. ##
  32. ## @var{rfunc}: String (i.e. in quotes) of a random distribution
  33. ## function. Has to take the number of rows as the first and the number of
  34. ## columns as the second argument. Default is 'rand'.
  35. ##
  36. ## @var{rmin} : Scalar -- Lower limit of random distribution function
  37. ## computed with @var{rfunc}
  38. ##
  39. ## @var{rmax} : Scalar -- Upper limit of random distribution function
  40. ##
  41. ## Output:
  42. ##
  43. ## @var{jitxp} : Columnvector -- sampling points with a jitter
  44. ##
  45. ## @emph{Note:}
  46. ##
  47. ## 1) The first sampling point will be 0, the last (N-1)*Tm. No
  48. ## jitter is added to them.
  49. ##
  50. ## 2) If you use a random distribution function and dont give a upper
  51. ## or lower limit it's assumed to be limited by its present computed
  52. ## maximum or minimum values.
  53. ## @end deftypefn
  54. function jitxp = irsa_jitsp( Tm, N, jit, rfunc, rmin, rmax )
  55. if( nargin < 2 || nargin > 6 )
  56. usage( "jitsp = irsa_jitsp( Tm, N, [jit], [rfunc], [rmin], [rmax] )" );
  57. endif
  58. if( nargin < 3 || isempty( jit ) )
  59. jit = 1;
  60. endif
  61. if( nargin < 4 || isempty( rfunc ) )
  62. rfunc = "rand";
  63. rmin = 0;
  64. rmax = 1;
  65. default = true;
  66. else
  67. default = false;
  68. endif
  69. r = feval( rfunc, N-2, 1 );
  70. if( !default )
  71. if( nargin < 5 || isempty( rmin ) )
  72. rmin = min(r);
  73. endif
  74. if( nargin < 4 || isempty( rmax ) )
  75. rmax = max(r);
  76. endif
  77. endif
  78. if( jit < 0 || jit > 1 )
  79. error( "The jitter has to be a factor in [0,1]" );
  80. endif
  81. if( any( r > rmax ) )
  82. error( "Upper limit for 'rfunc' violated." );
  83. endif
  84. if( any( r < rmin ) )
  85. error( "Lower limit for 'rfunc' violated." );
  86. endif
  87. ## Scale the distribution of r to [-jit/2,jit/2]
  88. r -= rmin;
  89. r /= rmax; # r is now in [-1,1]
  90. r -= 0.5;
  91. r *= jit; # r is now in [-jit/2,jit/2]
  92. jitxp = linspace(0,N-1,N)' * Tm;
  93. jitxp(2:N-1) .+= r;
  94. endfunction
  95. ## demo section
  96. %!demo
  97. %! N = 12;
  98. %! eqxp = irsa_jitsp( 1 , N, 0 ) + 1;
  99. %! jitxp = irsa_jitsp( 1 , N, 1 ) + 1;
  100. %! o = ones(N,1);
  101. %! ## Plot
  102. %! figure();
  103. %! legend('off');
  104. %! axis([0,13,0,1.5]);
  105. %! subplot( 211 );
  106. %! plot( eqxp, o, '^b', eqxp, o, '*b' ); text(); title("");
  107. %! title( "Jittered Sampling versus regular (equidistant) sampling" );
  108. %! text( 2,1.25, 'regular sampling with distance = 1' );
  109. %! subplot( 212 );
  110. %! plot( jitxp, o, '^r', jitxp, o, 'xr' ); text;
  111. %! xlabel( "Time" );
  112. %! text( 2,1.25, 'jittered sampling with mean distance = 1 and i.i.d. jitter within a range of 1' );
  113. ### Local Variables:
  114. ### mode: octave
  115. ### End: