1 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
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
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
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/>.
17 %% @deftypefn {Function File} @var{y} = sigmoid_train(@var{t}, @var{ranges}, @var{rc})
19 %% Evaluates a train of sigmoid functions at @var{t}.
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
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}.
28 %% Run @code{demo sigmoid_train} to some examples of the use of this function.
32 function envelope = sigmoid_train (t, range, timeconstant)
35 nRanges = size (range, 1);
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);
42 elseif any( size(timeconstant) != [1 1])
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.')
48 if isrow (timeconstant)
49 timeconstant = timeconstant';
51 timeconstant = repmat (timeconstant,1,2);
55 %% Make sure t is horizontal
56 flag_transposed = false;
59 flag_transposed = true;
61 [ncol nrow] = size (t);
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;
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);
75 envelope = envelope.';
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);
89 %! patch ([range(i,[2 2]) range(i,[1 1])], [0 1 1 0],...
90 %! 'facecolor', [1 0.8 0.8],'edgecolor','none');
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')
97 %! %-------------------------------------------------------------------------
98 %! % The colored regions show the limits defined in range.
102 %! t = linspace(0,2,200).';
103 %! ran = [0.5 1; 1.5 1.7];
105 %! dxdt = @(x_,t_) [ x_(2); sigmoid_train(t_, ran, rc) ];
106 %! y = lsode(dxdt,[0 0],t);
110 %! patch ([ran(i,[2 2]) ran(i,[1 1])], [0 1 1 0],...
111 %! 'facecolor', [1 0.8 0.8],'edgecolor','none');
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')
118 %! %-------------------------------------------------------------------------
119 %! % The colored regions show periods when the force is active.