From c44f0527358a5b54ff65e5fb8e78261df8847ff0 Mon Sep 17 00:00:00 2001 From: Chandra Pratap Date: Wed, 7 Aug 2024 17:54:04 +0530 Subject: Implement sinetone.sci in Scilab --- macros/sinetone.sci | 94 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 30 deletions(-) diff --git a/macros/sinetone.sci b/macros/sinetone.sci index 407fa45..f174869 100644 --- a/macros/sinetone.sci +++ b/macros/sinetone.sci @@ -1,32 +1,66 @@ -function y= sinetone(x, varargin) +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 atsampling 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. -funcprot(0); -rhs= argn(2); -if(rhs<1 | rhs>4) -error("Wrong number of input arguments") -end - -select(rhs) - case 1 then - y= callOctave("sinetone", x); - case 2 then - y= callOctave("sinetone", x , varargin(1)); - case 3 then - y= callOctave("sinetone", x , varargin(1), varargin(2)); - case 4 then - y= callOctave("sinetone", x , varargin(1), varargin(2), varargin(3)); - -end +//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]); -- cgit