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

« back to all changes in this revision

Viewing changes to inst/spikeauto.m

  • 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
## Copyright (C) 1996-2015 Piotr Held
 
2
##
 
3
## This file is part of Octave.
 
4
##
 
5
## Octave is free software; you can redistribute it and/or
 
6
## modify it under the terms of the GNU General Public
 
7
## License as published by the Free Software Foundation;
 
8
## either version 3 of the License, or (at your option) any
 
9
## later version.
 
10
##
 
11
## Octave is distributed in the hope that it will be useful,
 
12
## but WITHOUT ANY WARRANTY; without even the implied
 
13
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
14
## PURPOSE.  See the GNU General Public License for more
 
15
## details.
 
16
##
 
17
## You should have received a copy of the GNU General Public
 
18
## License along with Octave; see the file COPYING.  If not,
 
19
## see <http://www.gnu.org/licenses/>.
 
20
 
 
21
## -*- texinfo -*-
 
22
## @deftypefn{Function File} {output =} spikeauto (@var{X}, @var{bin}, @var{bintot})
 
23
## @deftypefnx{Function File} {output =} spikeauto (@dots{}, '@var{inter}')
 
24
##
 
25
## Computes the binned autocorrelation function of a series of event times.
 
26
##
 
27
## The data is assumed to represent a sum of delta functions centered at the
 
28
## times given. The autocorrelation function is then a double sum of delta
 
29
## functions which must be binned to be representable. Therfore, you have to
 
30
## choose the duration of a single bin (with argument @var{bin}) and the maximum
 
31
## time lag (argument @var{bintot}) considered.
 
32
##
 
33
## @strong{Inputs}
 
34
##
 
35
## @table @var
 
36
## @item S
 
37
## This function always assumes that each time series is along the longer 
 
38
## dimension of matrix @var{S}. It also assumes that every dimension 
 
39
## (counting along the shorter dimension) of @var{S} is considered a 
 
40
## component of the time series.
 
41
## @item bin
 
42
## The duration of a single bin.
 
43
## @item bintot
 
44
## The maximum lag considered.
 
45
## @end table
 
46
##
 
47
## @strong{Switch}
 
48
##
 
49
## @table @var
 
50
## @item inter
 
51
## Treat the input as inter-event intervals instead of the time at which the event
 
52
## occured. 
 
53
## @end table
 
54
##
 
55
## @strong{Output}
 
56
##
 
57
## The output is alligned with the input. If the input was a column vector the
 
58
## output will consist of two columns, the first holds information about which
 
59
## bin did the autocorellation fit into, and the second the number of
 
60
## autocorellations that fit into that bin.
 
61
##
 
62
## @strong{Algorithms}
 
63
##
 
64
## The algorithms for this functions have been taken from the TISEAN package.
 
65
## @end deftypefn
 
66
 
 
67
## Author: Piotr Held <pjheld@gmail.com>.
 
68
## This function is based on spikeauto of TISEAN 3.0.1 
 
69
## https://github.com/heggus/Tisean"
 
70
 
 
71
function output = spikeauto (X, bin, totbin, varargin)
 
72
 
 
73
  # Initial input validation
 
74
  if (nargin < 3 || nargin > 4)
 
75
    print_usage;
 
76
  endif
 
77
 
 
78
  # Check if X is real vector
 
79
  if ((isvector (X) == false) || (isreal (X) == false))
 
80
    error ('Octave:invalid-input-arg', "X must be a real vector");
 
81
  endif
 
82
 
 
83
  # Check if X has at least 2 different elements
 
84
  if (min (X) == max (X))
 
85
    error ('Octave:invalid-input-arg',
 
86
           "X must contain at least 2 differing elements");
 
87
  endif
 
88
 
 
89
  inter = false;
 
90
 
 
91
  if (nargin == 4)
 
92
    if (strcmpi (varargin{1}, "inter"))
 
93
      inter = true;
 
94
    else
 
95
      error ('Octave:invalid-input-arg', "additional parameter is not 'inter'");
 
96
    endif
 
97
  endif
 
98
 
 
99
  # Correct X to always have more rows than columns
 
100
  trnspsd = false;
 
101
  if (rows (X) < columns (X))
 
102
    X = X.';
 
103
    trnspsd = true;
 
104
  endif
 
105
 
 
106
  # If the input is interval change to times
 
107
  if (inter)
 
108
    X = cumsum (X);
 
109
  endif
 
110
 
 
111
  X = sort (X);
 
112
 
 
113
  # Number of bins
 
114
  nbin = floor (totbin / bin) + 1;
 
115
 
 
116
  # The oct file is used for optimization (using a for loop in Octave is about 100
 
117
  # times slower and not using the for loop uses a lot of memory,
 
118
  # e.g. when lenght (X) == 2000 it uses 500 MB).
 
119
  ihist = __spikeauto__ (X, bin, nbin);
 
120
 
 
121
  idx = (1:nbin).';
 
122
  idx = (idx - 0.5) .* bin;
 
123
 
 
124
  output = [idx, ihist];
 
125
 
 
126
  if (trnspsd)
 
127
    output = output.';
 
128
  endif
 
129
 
 
130
endfunction
 
131
 
 
132
%% Test against TISEAN output
 
133
%!test
 
134
%! spikeauto_res = [0.25 403965;0.75 376230;1.25 331311;1.75 274509;2.25 209767;2.75 153597;3.25 104075;3.75 65683;4.25 39030;4.75 21812;5.25 10745;5.75 5090;6.25 2064;6.75 792;7.25 245;7.75 70;8.25 14;8.75 1;9.25 0;9.75 0;10.25 0];
 
135
%! rand ("seed", 1);
 
136
%! x = zeros (2000,1);
 
137
%! for i = 2:2000
 
138
%!   x(i) = 0.7*x(i-1) +  (-6 + sum (rand ([size(1), 12]), 3));
 
139
%! endfor
 
140
%! res = spikeauto (x, 0.5, 10);
 
141
%! assert (res, spikeauto_res, 1);
 
142
 
 
143
%% Testing input validation
 
144
%!error <Invalid call> spikeauto (1)
 
145
%!error <2 differing elements> spikeauto (ones (10,1), 1,3);
 
146
%!error <vector> spikeauto ([(1:10);(1:10)],1,2);