~ubuntu-branches/debian/sid/octave-tisean/sid

« back to all changes in this revision

Viewing changes to src/lazy.cc

  • Committer: Package Import Robot
  • Author(s): Rafael Laboissiere
  • Date: 2017-08-14 12:53:47 UTC
  • Revision ID: package-import@ubuntu.com-20170814125347-ju5owr4dggr53a2n
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- coding: utf-8 -*- */
 
2
/* Copyright (C) 1996-2015 Piotr Held <pjheld@gmail.com>
 
3
 *
 
4
 * This file is part of Octave.
 
5
 *
 
6
 * Octave is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public
 
8
 * License as published by the Free Software Foundation;
 
9
 * either version 3 of the License, or (at your option) any
 
10
 * later version.
 
11
 *
 
12
 * Octave is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied
 
14
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
15
 * PURPOSE.  See the GNU General Public License for more
 
16
 * details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public
 
19
 * License along with Octave; see the file COPYING.  If not,
 
20
 * see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
/********************************************************************/
 
23
/********************************************************************/
 
24
#define HELPTEXT "\
 
25
-*- texinfo -*-\n\
 
26
@deftypefn{Function File} {@var{cleaned} =} lazy (@var{X}, @var{m}, @var{rv})\n\
 
27
@deftypefnx{Function File} {[@var{cleaned}, @var{diff}] =} lazy (@var{X}, @var{m}, @var{rv})\n\
 
28
@deftypefnx{Function File} {@dots{} =} lazy (@var{X}, @var{m}, @var{rv}, @var{imax})\n\
 
29
\n\
 
30
Performs simple nonlinear noise reduction\n\
 
31
\n\
 
32
@strong {Inputs}\n\
 
33
\n\
 
34
@table @var\n\
 
35
@item X\n\
 
36
Must be realvector. If it is a row vector then the output will be row vectors as well.\n\
 
37
@item m\n\
 
38
Embedding dimension. Must be postive integer.\n\
 
39
@item rv\n\
 
40
If @var{rv} > 0 then it is equal to the absolute radius of the neighbourhoods. If @var{rv} < 0 then its opposite (-@var{rv}) is equal to the fraction of standard deviation used. It cannot be equal 0.\n\
 
41
@item imax\n\
 
42
The number of iterations [default = 1].\n\
 
43
@end table\n\
 
44
\n\
 
45
@strong {Output}\n\
 
46
\n\
 
47
@table @var\n\
 
48
@item cleaned\n\
 
49
Vector containing the cleaned data.\n\
 
50
@item diff\n\
 
51
Difference between the clean and noisy data. \n\
 
52
@end table\n\
 
53
\n\
 
54
See the demo for example of how lazy works. \n\
 
55
\n\
 
56
@strong{Algorithm}@*\n\
 
57
Uses TISEAN package lazy\n\
 
58
@end deftypefn"
 
59
/******************************************************************************/
 
60
/******************************************************************************/
 
61
/* Author: Piotr Held <pjheld@gmail.com>.
 
62
 * This function is based on lazy of TISEAN 3.0.1 
 
63
 * https://github.com/heggus/Tisean"
 
64
 */
 
65
 
 
66
#include <octave/oct.h>
 
67
#include <octave/f77-fcn.h>
 
68
 
 
69
#define DEFAULT_IMAX 1
 
70
 
 
71
// In order to avoid clobbered warnings transposed is initialized globally.
 
72
bool transposed;
 
73
 
 
74
extern "C"
 
75
{
 
76
  F77_RET_T
 
77
  F77_FUNC (ts_lazy, TS_LAZY)
 
78
            (const int& m, const double& rv,
 
79
             const int& imax, const int& lines_read,
 
80
             double* in_out1, double* in_out2);
 
81
}
 
82
 
 
83
 
 
84
DEFUN_DLD (lazy, args, nargout, HELPTEXT)
 
85
{
 
86
  octave_value_list retval;
 
87
  int nargin = args.length ();
 
88
 
 
89
 
 
90
  if ((nargin != 4) && (nargin != 3))
 
91
    {
 
92
      print_usage ();
 
93
    }
 
94
  else if (nargout > 2)
 
95
    {
 
96
      error_with_id ("Octave:invalid-fun-call", \
 
97
                     "Lazy only produces two outputs");
 
98
    }
 
99
  else
 
100
    {
 
101
    // Assigning inputs
 
102
      Matrix in_out1 = args(0).matrix_value();
 
103
      int m          = args(1).int_value();
 
104
      double rv      = args(2).double_value();
 
105
      int imax       = DEFAULT_IMAX;
 
106
 
 
107
      if (nargin == 4)
 
108
        imax = args(3).int_value();
 
109
 
 
110
// --- DATA VALIDATION ---
 
111
 
 
112
    // Checking if matrix is vector.
 
113
 
 
114
      int rows = in_out1.rows();
 
115
      int cols = in_out1.cols();
 
116
 
 
117
      if (((rows != 1) && (cols != 1)) || (cols == 0) || (rows == 0))
 
118
        error_with_id ("Octave:invalid-input-arg",\
 
119
                       "input X must be a vector");
 
120
 
 
121
    // Checking parameters
 
122
      if (m < 1)
 
123
        error_with_id ("Octave:invalid-input-arg",\
 
124
                       "Embedding dimension (M) must be a positive integer");
 
125
 
 
126
      if (rv == 0.)
 
127
        error_with_id ("Octave:invalid-input-arg",\
 
128
                       "Set either radious of neighbourhoods"
 
129
                       " or fraction of std deviation");
 
130
 
 
131
      if (imax < 1)
 
132
        error_with_id ("Octave:invalid-input-arg",\
 
133
                       "Number of iterations (IMAX) must be a positive "
 
134
                       "integer");
 
135
 
 
136
      if (! error_state)
 
137
        {
 
138
        // If vector is in 1 row: transpose (we will transpose the output to fit)
 
139
            transposed = 0;
 
140
 
 
141
          if ((rows == 1) && (cols > 1))
 
142
            {
 
143
              transposed = 1;
 
144
              in_out1    = in_out1.transpose();
 
145
            }
 
146
 
 
147
          int lines_read = in_out1.numel();
 
148
          NDArray in_out2 (Matrix (lines_read, 1));
 
149
 
 
150
          F77_XFCN (ts_lazy, TS_LAZY,
 
151
                    (m, rv, imax, lines_read,
 
152
                      in_out1.fortran_vec(), in_out2.fortran_vec()));
 
153
 
 
154
          // Transpose the output to resemble the input
 
155
          if (transposed)
 
156
          {
 
157
            in_out1 = in_out1.transpose();
 
158
            in_out2 = in_out2.transpose();
 
159
          }
 
160
 
 
161
          retval(0) = in_out1;
 
162
          retval(1) = in_out2;
 
163
        }
 
164
    }
 
165
  return retval;
 
166
}
 
167
 
 
168
/*
 
169
%!demo
 
170
%! hen    = henon (10000);
 
171
%! "The following line is equvalent to 'addnoise -v0.02 hen' from TISEAN";
 
172
%! hen    = hen + std (hen) * 0.02 .* (-6 + sum (rand ([size(hen), 12]), 3));
 
173
%! hendel = delay (hen(:,1));
 
174
%! henlaz = lazy (hen(:,1),7,-0.06,3);
 
175
%! henlaz = delay (henlaz);
 
176
%!
 
177
%! subplot (2,3,1)
 
178
%! plot (hendel(:,1), hendel(:,2), 'b.','markersize', 3);
 
179
%! title ("Noisy data");
 
180
%! pbaspect ([1 1 1]);
 
181
%! axis tight
 
182
%! axis off
 
183
%!
 
184
%! subplot (2,3,4)
 
185
%! plot (henlaz(:,1), henlaz(:,2),'r.','markersize', 3);
 
186
%! title ("Clean data");
 
187
%! pbaspect ([1 1 1]);
 
188
%! axis tight
 
189
%! axis off
 
190
%!
 
191
%! subplot (2,3,[2 3 5 6])
 
192
%! plot (hendel(:,1), hendel(:,2), 'b.','markersize', 3,...
 
193
%!       henlaz(:,1), henlaz(:,2),'r.','markersize', 3);
 
194
%! legend ("Noisy", "Clean");
 
195
%! title ("Superimposed data");
 
196
%! axis tight
 
197
 
 
198
%!###############################################################
 
199
 
 
200
%!fail("lazy([(1:10);(1:10)],7,-0.06)");
 
201
 
 
202
%!fail("lazy((1:10),0,0.04)");
 
203
 
 
204
%!fail("[a,b,c] = lazy((1:10),1,0.05)");
 
205
 
 
206
%!test
 
207
%! "In is generated from Octave using 'in = 1 + 0.5 * rand(10,1);'";
 
208
%! in = [1.47007925526322;1.168775342017635;1.10943000146922; 1.174293926353764;  1.075741574572656; 1.373465364407417; 1.089417388489702; 1.403669883669071;1.452726826806777;  1.016960990335037];
 
209
%! "res was generated using 'lazy -m1 -v0.06 in.dat' from TISEAN 'lazy'";
 
210
%! res = [1.47007930, 0.00000000; 1.17153454, -2.75921822E-03; 1.10942996, 0.00000000; 1.17153454, 2.75933743E-03; 1.07574153, 0.00000000; 1.37346542, 0.00000000; 1.08941734, 0.00000000; 1.40366983, 0.00000000; 1.45272684, 0.00000000; 1.01696098, 0.00000000];
 
211
%! [al,bl] = lazy(in, 1, -0.06);
 
212
%! assert([al,bl],res,1e-6);
 
213
*/