1 ## Copyright (C) 2001 Paul Kienzle <pkienzle@users.sf.net>
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/>.
16 ## usage: y = tripuls(t, w, skew)
18 ## Generate a triangular pulse over the interval [-w/2,w/2), sampled at
19 ## times t. This is useful with the function pulstran for generating a
22 ## skew is a value between -1 and 1, indicating the relative placement
23 ## of the peak within the width. -1 indicates that the peak should be
24 ## at -w/2, and 1 indicates that the peak should be at w/2.
27 ## fs = 11025; # arbitrary sample rate
28 ## f0 = 100; # pulse train sample rate
29 ## w = 0.3/f0; # pulse width 3/10th the distance between pulses
30 ## auplot(pulstran(0:1/fs:4/f0, 0:1/f0:4/f0, 'tripuls', w), fs);
34 function y = tripuls (t, w = 1, skew = 0)
36 if nargin<1 || nargin>3,
42 try wfi = warning("off", "Octave:fortran-indexing");
46 idx = find(t>=-w/2 & t <= peak);
47 if (idx) y(idx) = ( t(idx) + w/2 ) / ( peak + w/2 ); endif
48 idx = find(t>peak & t < w/2);
49 if (idx) y(idx) = ( t(idx) - w/2 ) / ( peak - w/2 ); endif
50 unwind_protect_cleanup
55 %!assert(tripuls(0:1/100:0.3,.1), tripuls([0:1/100:0.3]',.1)');
56 %!assert(isempty(tripuls([],.1)));
58 %! fs = 11025; # arbitrary sample rate
59 %! f0 = 100; # pulse train sample rate
60 %! w = 0.5/f0; # pulse width 1/10th the distance between pulses
61 %! subplot(211); ylabel("amplitude"); xlabel("time (ms)");
62 %! title("graph shows 5 ms pulses at 0,10,20,30 and 40 ms");
63 %! auplot(pulstran(0:1/fs:4/f0, 0:1/f0:4/f0, 'tripuls', w), fs);
65 %! title("graph shows 5 ms pulses at 0,10,20,30 and 40 ms, skew -0.5");
66 %! auplot(pulstran(0:1/fs:4/f0, 0:1/f0:4/f0, 'tripuls', w, -0.5), fs);
67 %! title(""); xlabel(""); ylabel("");