]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/sigmoid_train.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / sigmoid_train.m
1 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
2 %%
3 %% This program is free software; you can redistribute it and/or modify it under
4 %% the terms of the GNU General Public License as published by the Free Software
5 %% Foundation; either version 3 of the License, or (at your option) any later
6 %% version.
7 %%
8 %% This program is distributed in the hope that it will be useful, but WITHOUT
9 %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 %% details.
12 %%
13 %% You should have received a copy of the GNU General Public License along with
14 %% this program; if not, see <http://www.gnu.org/licenses/>.
15
16 %% -*- texinfo -*-
17 %% @deftypefn {Function File} @var{y} = sigmoid_train(@var{t}, @var{ranges}, @var{rc})
18 %%
19 %% Evaluates a train of sigmoid functions at @var{t}.
20 %%
21 %% The number and duration of each sigmoid is determined from @var{ranges}. Each
22 %% row of @var{ranges} represents a real interval, e.g. if sigmod @code{i} starts
23 %% at @code{t=0.1} and ends at @code{t=0.5}, then @code{@var{ranges}(i,:) = [0.1
24 %% 0.5]}.
25 %% The input @var{rc} is a array that defines the rising and falling time
26 %% constants of each sigmoids. Its size must equal the size of @var{ranges}.
27 %%
28 %% Run @code{demo sigmoid_train} to some examples of the use of this function.
29 %%
30 %% @end deftypefn
31
32 function envelope = sigmoid_train (t, range, timeconstant)
33
34   % number of sigmoids
35   nRanges = size (range, 1);
36
37   %% Parse time constants
38   if isscalar (timeconstant)
39     %% All bumps have the same time constant and are symmetric
40     timeconstant = timeconstant * ones (nRanges,2);
41
42   elseif any( size(timeconstant) != [1 1])
43
44     %% All bumps have different time constant but are symmetric
45     if length(timeconstant) ~= nRanges
46       error('signalError','Length of time constant must equal number of ranges.')
47     end
48     if isrow (timeconstant)
49       timeconstant = timeconstant';
50     end
51     timeconstant = repmat (timeconstant,1,2);
52
53   end
54
55   %% Make sure t is horizontal
56   flag_transposed = false;
57   if iscolumn (t)
58    t               = t.';
59    flag_transposed = true;
60   end
61   [ncol nrow]     = size (t);
62
63   % Compute arguments of each sigmoid
64   T    = repmat (t, nRanges, 1);
65   RC1  = repmat (timeconstant(:,1), 1, nrow);
66   RC2  = repmat (timeconstant(:,2), 1, nrow);
67   a_up = (repmat (range(:,1), 1 ,nrow) - T)./RC1;
68   a_dw = (repmat (range(:,2), 1 ,nrow) - T)./RC2;
69
70   % Evaluate the sigmoids and mix them
71   Y        = 1 ./ ( 1 + exp (a_up) ) .* (1 - 1 ./ ( 1 + exp (a_dw) ) );
72   envelope = max(Y,[],1);
73
74   if flag_transposed
75     envelope = envelope.';
76   end
77
78 end
79
80 %!demo
81 %! % Vectorized
82 %! t = linspace (0, 2, 500);
83 %! range = [0.1 0.4; 0.6 0.8; 1 2];
84 %! rc = [1e-2 1e-2; 1e-3 1e-3; 2e-2 2e-2];
85 %! y = sigmoid_train (t, range, rc);
86 %!
87 %! close all
88 %! for i=1:3
89 %!     patch ([range(i,[2 2]) range(i,[1 1])], [0 1 1 0],...
90 %!               'facecolor', [1 0.8 0.8],'edgecolor','none');
91 %! end
92 %! hold on; plot (t, y, 'b;Sigmoid train;','linewidth',2); hold off
93 %! xlabel('time'); ylabel('S(t)')
94 %! title ('Vectorized use of sigmoid train')
95 %! axis tight
96 %!
97 %! %-------------------------------------------------------------------------
98 %! % The colored regions show the limits defined in range.
99
100 %!demo
101 %! % On demand
102 %! t = linspace(0,2,200).';
103 %! ran = [0.5 1; 1.5 1.7];
104 %! rc = 3e-2;
105 %! dxdt = @(x_,t_) [ x_(2); sigmoid_train(t_, ran, rc) ];
106 %! y = lsode(dxdt,[0 0],t);
107 %!
108 %! close all
109 %! for i=1:2
110 %!     patch ([ran(i,[2 2]) ran(i,[1 1])], [0 1 1 0],...
111 %!               'facecolor', [1 0.8 0.8],'edgecolor','none');
112 %! end
113 %! hold on; plot (t, y(:,2), 'b;Speed;','linewidth',2); hold off
114 %! xlabel('time'); ylabel('V(t)')
115 %! title ('On demand use of sigmoid train')
116 %! axis tight
117 %!
118 %! %-------------------------------------------------------------------------
119 %! % The colored regions show periods when the force is active.