From 12f72c647026be2fc8e28a04ad0130fb20154e7e Mon Sep 17 00:00:00 2001 From: Chandra Pratap Date: Wed, 7 Aug 2024 17:54:26 +0530 Subject: Implement sinewave.sci in Scilab --- macros/sinewave.sci | 84 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/macros/sinewave.sci b/macros/sinewave.sci index 5263f35..31ee6b0 100644 --- a/macros/sinewave.sci +++ b/macros/sinewave.sci @@ -1,29 +1,57 @@ -function y= sinewave(x, varargin) -//Return an M-element vector with 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: Input vector -//N: The default value for N is M -//D: The default value for D is 0 -//AMPL: Amplitude -//Description -//Return an M-element vector with I-th element given by 'sin(2* pi *(I+D-1)/N).' -funcprot(0); -rhs= argn(2); -if(rhs<1 | rhs>3) -error("Wrong number of input arguments") -end - -select(rhs) - case 1 then - y= callOctave("sinewave", x); - case 2 then - y= callOctave("sinewave", x , varargin(1)); - case 3 then - y= callOctave("sinewave", x , varargin(1), varargin(2)); - -end +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); -- cgit