]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/tripuls.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / tripuls.m
1 ## Copyright (C) 2001 Paul Kienzle <pkienzle@users.sf.net>
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 ## usage: y = tripuls(t, w, skew)
17 ##
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
20 ## series pulses.
21 ##
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.
25 ##
26 ## Example
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);
31 ##
32 ## See also: pulstran
33
34 function y = tripuls (t, w = 1, skew = 0)
35
36   if nargin<1 || nargin>3,
37     print_usage;
38   endif
39
40   y = zeros(size(t));
41   peak = skew*w/2;
42   try wfi = warning("off", "Octave:fortran-indexing");
43   catch wfi = 0;
44   end
45   unwind_protect
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
51     warning(wfi);
52   end_unwind_protect
53 endfunction
54
55 %!assert(tripuls(0:1/100:0.3,.1), tripuls([0:1/100:0.3]',.1)');
56 %!assert(isempty(tripuls([],.1)));
57 %!demo
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);
64 %! subplot(212);
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("");