1
/* -*- coding: utf-8 -*- */
2
/* Copyright (C) 1996-2015 Piotr Held <pjheld@gmail.com>
4
* This file is part of Octave.
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
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
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/>.
22
/********************************************************************/
23
/********************************************************************/
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\
30
Performs simple nonlinear noise reduction\n\
36
Must be realvector. If it is a row vector then the output will be row vectors as well.\n\
38
Embedding dimension. Must be postive integer.\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\
42
The number of iterations [default = 1].\n\
49
Vector containing the cleaned data.\n\
51
Difference between the clean and noisy data. \n\
54
See the demo for example of how lazy works. \n\
56
@strong{Algorithm}@*\n\
57
Uses TISEAN package lazy\n\
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"
66
#include <octave/oct.h>
67
#include <octave/f77-fcn.h>
69
#define DEFAULT_IMAX 1
71
// In order to avoid clobbered warnings transposed is initialized globally.
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);
84
DEFUN_DLD (lazy, args, nargout, HELPTEXT)
86
octave_value_list retval;
87
int nargin = args.length ();
90
if ((nargin != 4) && (nargin != 3))
96
error_with_id ("Octave:invalid-fun-call", \
97
"Lazy only produces two outputs");
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;
108
imax = args(3).int_value();
110
// --- DATA VALIDATION ---
112
// Checking if matrix is vector.
114
int rows = in_out1.rows();
115
int cols = in_out1.cols();
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");
121
// Checking parameters
123
error_with_id ("Octave:invalid-input-arg",\
124
"Embedding dimension (M) must be a positive integer");
127
error_with_id ("Octave:invalid-input-arg",\
128
"Set either radious of neighbourhoods"
129
" or fraction of std deviation");
132
error_with_id ("Octave:invalid-input-arg",\
133
"Number of iterations (IMAX) must be a positive "
138
// If vector is in 1 row: transpose (we will transpose the output to fit)
141
if ((rows == 1) && (cols > 1))
144
in_out1 = in_out1.transpose();
147
int lines_read = in_out1.numel();
148
NDArray in_out2 (Matrix (lines_read, 1));
150
F77_XFCN (ts_lazy, TS_LAZY,
151
(m, rv, imax, lines_read,
152
in_out1.fortran_vec(), in_out2.fortran_vec()));
154
// Transpose the output to resemble the input
157
in_out1 = in_out1.transpose();
158
in_out2 = in_out2.transpose();
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);
178
%! plot (hendel(:,1), hendel(:,2), 'b.','markersize', 3);
179
%! title ("Noisy data");
180
%! pbaspect ([1 1 1]);
185
%! plot (henlaz(:,1), henlaz(:,2),'r.','markersize', 3);
186
%! title ("Clean data");
187
%! pbaspect ([1 1 1]);
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");
198
%!###############################################################
200
%!fail("lazy([(1:10);(1:10)],7,-0.06)");
202
%!fail("lazy((1:10),0,0.04)");
204
%!fail("[a,b,c] = lazy((1:10),1,0.05)");
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);