summaryrefslogtreecommitdiff
path: root/macros/sinetone.sci
blob: f174869a781be57a65c9399e925b32218176c8b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function y = sinetone(freq, rate, sec, ampl)
//Return a sinetone of the input
//Calling Sequence:
//y= sinetone(freq)
//y= sinetone(freq, rate)
//y= sinetone(freq, rate, sec)
//y= sinetone(freq, rate, sec, ampl)
//Parameters:
//freq: frequency of sinetone
//rate: sampling rate
//sec: length in seconds
//ampl: amplitude
//Description:
//Return a sinetone of frequency 'freq' with a length of 'sec' seconds at sampling rate 'rate' and with amplitude 'ampl'.
//The arguments freq and ampl may be vectors of common size. The defaults are rate = 8000, sec = 1, and ampl = 64.
//Examples:
//sinetone(5, 2, 1, 8)
//ans = [4.89E-15; -9.79E-15]

    funcprot(0);
    rhs= argn(2);

    select rhs
        case 1 then
            rate = 8000;
            sec = 1;
            ampl = 64;
        case 2 then
            sec = 1;
            ampl = 64;
        case 3 then
            ampl = 64;
        case 4 then
            break;
        else
            error("sinetone: wrong number of input arguments");
    end

    if ( isvector(freq) & isvector(ampl) & ~isequal(size(freq), size(ampl)) ) then
        error("sinetone: freq and ampl must be vectors of equal size");
    end

    if ( ~(isscalar(rate) && isscalar(sec)) ) then
        error("sinetone: rate and sec must be scalars");
    end

    n = length (freq);
    ns = round (rate * sec);
    y = zeros (ns, n);

    for k = 1:n
        y(:, k) = ampl(k) * sin (2 * %pi * (1:ns) / rate * freq(k))';
    end

endfunction

//input validation:
//assert_checkerror("sinetone()", "sinetone: wrong number of input arguments");
//assert_checkerror("sinetone([6, 9, 4, 2], 2, 3, [6, 2, 0])", "sinetone: freq and ampl must be vectors of equal size");
//assert_checkerror("sinetone(1, [1, 2])", "sinetone: rate and sec must be scalars");
//assert_checkerror("sinetone(1, 2, [4, 3])", "sinetone: rate and sec must be scalars");

//tests:
//assert_checkequal(size(sinetone(18e6, 150e6, 19550/150e6, 1)), [19550, 1]);
//assert_checkequal(sinetone(5), sinetone(5, 8000, 1, 64));
//assert_checkequal(size(sinetone([1, 2, 3], 4000, 1, [8, 9, 6])), [4000, 3]);