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
|
function y = sinewave (m, n, d)
//Return a m-element vector with the i-th element given by {sin (2 * pi * (i+d-1) / n}.
//Calling Sequence:
//y= sinewave(m)
//y= sinewave(m,n)
//y= sinewave(m,n,d)
//Parameters:
//m: Real positive scalar
//n: The value of n in the formula {sin (2 * pi * (i+d-1) / n}. The default value for n is m.
//d: The value of d in the formula {sin (2 * pi * (i+d-1) / n}. The default value for d is 0.
//Examples:
//sinewave(1, 4, 1)
//ans = 1
funcprot(0);
rhs = argn(2);
if (rhs < 1 | rhs > 3) then
error("sinewave: wrong number of input arguments");
end
if (m < 0) then
error("sinewave: arg1 (m) must be non-negative");
end
if (rhs < 2) then
n = m;
end
if (n == 0) then
error("sinewave: arg2 (n) must be non-zero");
end
if (~isscalar(m)) then
error("sinewave: arg1 (m) must be a real scalar");
end
if (rhs < 3) then
d = 0;
end
y = sin(((1 : m) + d - 1) * 2 * %pi / n);
endfunction
//input validation:
//assert_checkerror("sinewave()", "sinewave: wrong number of input arguments");
//assert_checkerror("sinewave(-1, 2)", "sinewave: arg1 (m) must be non-negative");
//assert_checkerror("sinewave([-2, 5, -3], 2)", "sinewave: arg1 (m) must be a real scalar");
//assert_checkerror("sinewave(0, 0, 5)", "sinewave: arg2 (n) must be non-zero");
//tests:
//assert_checkequal(sinewave(1), 0);
//assert_checkequal(sinewave(1), sinewave(1, 1, 0));
//assert_checkequal(sinewave(3, 4), sinewave(3, 4, 0));
//assert_checkalmostequal(sinewave(1, 12, 1), 1/2, %eps);
//assert_checkalmostequal(sinewave(1, 20, 1), (sqrt(5)-1)/4, %eps);
//assert_checkequal(sinewave(3, 4, 1), [1, sin(%pi), -1]);
//assert_checkalmostequal(sinewave(4, 12, 1), [1/2, sqrt(3)/2, 1, sqrt(3)/2], %eps);
|