changeset 10668:38ea81c19717 octave-forge

vectorization example
author cdf
date Fri, 10 Aug 2012 13:17:33 +0000
parents a5685d61d7b6
children 5996840e1cb9
files extra/lssa/inst/fastlscomplex.m
diffstat 1 files changed, 55 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/lssa/inst/fastlscomplex.m	Fri Aug 10 13:17:33 2012 +0000
@@ -0,0 +1,55 @@
+## Copyright (C) 2012 Benjamin Lewis <benjf5@gmail.com>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{t} =} lscomplex (@var{time}, @var{mag}, @var{maxfreq}, @var{numcoeff}, @var{numoctaves})
+## 
+## Return the complex least-squares transform of the (@var{time},@var{mag})
+## series, considering frequencies up to @var{maxfreq}, over @var{numoctaves}
+## octaves and @var{numcoeff} coefficients.
+##
+## @seealso{lsreal}
+## @end deftypefn
+
+
+function transform = fastlscomplex (t, x, omegamax, ncoeff, noctave)
+
+  ## t will be unrolled to a column vector below
+  ## no metter what its original shape is
+  n = numel (t); 
+   
+  iter = 0 : (ncoeff * noctave - 1);
+  omul = (2 .^ (- iter / ncoeff));
+
+  ot = t(:) * (omul * omegamax);
+
+  ## See the paper for the expression below
+  transform = sum ((cos (ot) - (sin (ot) .* i)) .* x(:), 1) / n; 
+  
+endfunction 
+
+%!test
+%! maxfreq = 4 / ( 2 * pi );
+%! t = [0:0.008:8];
+%! x = ( 2 .* sin (maxfreq .* t) +
+%!       3 .* sin ( (3 / 4) * maxfreq .* t)-
+%!       0.5 .* sin ((1/4) * maxfreq .* t) -
+%!       0.2 .* cos (maxfreq .* t) + 
+%!       cos ((1/4) * maxfreq .* t));
+%! assert (fastlscomplex (t, x, maxfreq, 2, 2), 
+%!       [(-0.400924546169395 - 2.371555305867469i), ...
+%!        (1.218065147708429 - 2.256125004156890i), ... 
+%!        (1.935428592212907 - 1.539488163739336i), ...
+%!        (2.136692292751917 - 0.980532175174563i)], 5e-10);