]> Creatis software - CreaPhase.git/blob - octave_packages/m/signal/sinetone.m
update packages
[CreaPhase.git] / octave_packages / m / signal / sinetone.m
1 ## Copyright (C) 1995-2012 Friedrich Leisch
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} sinetone (@var{freq}, @var{rate}, @var{sec}, @var{ampl})
21 ## Return a sinetone of frequency @var{freq} with length of @var{sec}
22 ## seconds at sampling rate @var{rate} and with amplitude @var{ampl}.
23 ## The arguments @var{freq} and @var{ampl} may be vectors of common size.
24 ##
25 ## Defaults are @var{rate} = 8000, @var{sec} = 1 and @var{ampl} = 64.
26 ## @end deftypefn
27
28 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at>
29 ## Description: Compute a sine tone
30
31 function retval = sinetone (freq, rate, sec, ampl)
32
33   if (nargin == 1)
34     rate = 8000;
35     sec = 1;
36     ampl = 64;
37   elseif (nargin == 2)
38     sec = 1;
39     ampl = 64;
40   elseif (nargin == 3)
41     ampl = 64;
42   elseif ((nargin < 1) || (nargin > 4))
43     print_usage ();
44   endif
45
46   [err, freq, ampl] = common_size (freq, ampl);
47   if (err || ! isvector (freq))
48     error ("sinetone: FREQ and AMPL must be vectors of common size");
49   endif
50
51   if (! (isscalar (rate) && isscalar (sec)))
52     error ("sinetone: RATE and SEC must be scalars");
53   endif
54
55   n = length (freq);
56   ns = round (rate * sec);
57
58   retval = zeros (ns, n);
59
60   for k = 1:n
61     retval (:, k) = ampl(k) * sin (2 * pi * (1:ns) / rate * freq(k))';
62   endfor
63
64 endfunction
65
66
67 %!assert (size (sinetone (18e6, 150e6, 19550/150e6, 1)), [19550, 1]);