[4ad237]: inst / lsreal.m Maximize Restore History

Download this file

lsreal.m    80 lines (64 with data), 2.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
## Copyright (C) 2012 Benjamin Lewis
##
## 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{transform} =} lsreal (@var{time}, @var{mag}, @var{maxfreq}, @var{numcoeff}, @var{numoctaves})
##
## Return the real least-squares transform of the time series
## defined, based on the maximal frequency @var{maxfreq}, the
## number of coefficients @var{numcoeff}, and the number of
## octaves @var{numoctaves}. Each complex-valued result is the
## pair (c_o, s_o) defining the coefficients which best fit the
## function y = c_o * cos(ot) + s_o * sin(ot) to the (@var{time}, @var{mag}) data.
##
## @seealso{lscomplex}
## @end deftypefn
function transform = lsreal (t, x, omegamax, ncoeff, noctave)
## FIXME : THIS IS VECTOR-ONLY. I'd need to add another bit of code to
## make it array-safe, and that's not knowing right now what else
## will be necessary.
k = n = length (t);
transform = zeros (1, (noctave * ncoeff));
od = 2 ^ (- 1 / ncoeff);
o = omegamax;
n1 = 1 / n;
ncoeffp = ncoeff * noctave;
for iter = 1:ncoeffp
## This method is an application of Eq. 8 on
## page 6 of the text, as well as Eq. 7
ot = o .* t;
zeta = n1 * sum ((cos (ot) - i * sin (ot)) .* x);
ot *= 2;
iota = n1 * sum (cos (ot) - i * sin (ot));
transform(iter) = (2 * (conj (zeta) - (conj (iota) * zeta)) /
(1 - (real (iota) ^ 2) - (imag (iota) ^ 2)));
o *= od;
endfor
endfunction
%!test
%! maxfreq = 4 / ( 2 * pi );
%! t = linspace(0,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 ) );
%! # In the assert here, I've got an error bound large enough to catch
%! # individual system errors which would present no real issue.
%! assert (lsreal (t,x,maxfreq,2,2),
%! [(-1.68275915310663 + 4.70126183846743i), ...
%! (1.93821553170889 + 4.95660209883437i), ...
%! (4.38145452686697 + 2.14403733658600i), ...
%! (5.27425332281147 - 0.73933440226597i)],
%! 5e-10)