diff options
author | avinashlalotra | 2025-03-13 01:42:04 +0530 |
---|---|---|
committer | avinashlalotra | 2025-03-13 01:42:04 +0530 |
commit | f3e66915373c36161ec86e36a8d7d30966167287 (patch) | |
tree | f59467708f181a481dfe70431e32d77c2b6de7a0 | |
parent | af6fe82f90dcb2314a3d37a9a1e297fb0fc447f3 (diff) | |
download | FOSSEE-Signal-Processing-Toolbox-f3e66915373c36161ec86e36a8d7d30966167287.tar.gz FOSSEE-Signal-Processing-Toolbox-f3e66915373c36161ec86e36a8d7d30966167287.tar.bz2 FOSSEE-Signal-Processing-Toolbox-f3e66915373c36161ec86e36a8d7d30966167287.zip |
FOSSEE Winter Internship 2024 work done by Abinash Singh
-rw-r--r-- | macros/besselap.sci | 29 | ||||
-rw-r--r-- | macros/besself.sci | 41 | ||||
-rw-r--r-- | macros/bilinear.sci | 28 | ||||
-rw-r--r-- | macros/postpad.sci | 82 | ||||
-rw-r--r-- | macros/prepad.sci | 118 | ||||
-rw-r--r-- | macros/sftrans.sci | 22 | ||||
-rw-r--r-- | macros/tf2zp.sci | 18 | ||||
-rw-r--r-- | macros/zp2tf.sci | 10 |
8 files changed, 193 insertions, 155 deletions
diff --git a/macros/besselap.sci b/macros/besselap.sci index 2ed0047..1e7d952 100644 --- a/macros/besselap.sci +++ b/macros/besselap.sci @@ -1,17 +1,14 @@ // Copyright (C) 2018 - IIT Bombay - FOSSEE -// // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt -// Original contribution: FOSSEE, IIT Bombay // Original Source : https://octave.sourceforge.io/signal/ -// Modifieded by: Sonu Sharma, RGIT Mumbai +// Modifieded by: Abinash Singh Under FOSSEE Internship +// Date of Modification: 3 Feb 2024 // Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in - - function [zero, pole, gain]=besselap(n) //Bessel analog filter prototype. @@ -44,38 +41,44 @@ function [zero, pole, gain]=besselap(n) // zero = // // [] - + // Dependencies + // prepad funcprot(0); [nargout, nargin] = argn() ; - if (nargin>1 | nargin<1) error("besselap : wrong number of input argument") end - // interpret the input parameters if (~(length(n)==1 & n == round(n) & n > 0)) error ("besselap: filter order n must be a positive integer"); end - p0=1; p1=[1 1]; for nn=2:n px=(2*nn-1)*p1; py=[p0 0 0]; - px=prepad(px,max(length(px),length(py)),0); + px=prepad(px,max(length(px),length(py))); py=prepad(py,length(px)); p0=p1; p1=px+py; end // p1 now contains the reverse bessel polynomial for n - // scale it by replacing s->s/w0 so that the gain becomes 1 p1=p1.*p1(length(p1)).^((length(p1)-1:-1:0)/(length(p1)-1)); - zero=[]; pole=roots(p1); gain=1; - endfunction + +/* +Note : The function is tested with Octave's outputs as a reference. +# all passed +[zero, pole, gain] = besselap (1) +[zero, pole, gain] = besselap (2) +[zero, pole, gain] = besselap (7) +[zero, pole, gain] = besselap (13) +[zero, pole, gain] = besselap (43) + +*/
\ No newline at end of file diff --git a/macros/besself.sci b/macros/besself.sci index c52f3ac..a6eef80 100644 --- a/macros/besself.sci +++ b/macros/besself.sci @@ -1,15 +1,14 @@ // Copyright (C) 2018 - IIT Bombay - FOSSEE -// // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt // Original Source : https://octave.sourceforge.io/signal/ -// Modifieded by:Sonu Sharma, RGIT Mumbai +// Modifieded by: Abinash Singh Under FOSSEE Internship +// Date of Modification: 3 Feb 2024 // Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in - function [a, b, c, d] = besself (n, w, varargin) //Bessel filter design. @@ -20,22 +19,17 @@ function [a, b, c, d] = besself (n, w, varargin) //[b, a] = besself (n, [Wl, Wh], "stop") //[z, p, g] = besself (…) //[…] = besself (…, "z") - - //Parameters //n: positive integer value (order of filter) //Wc: positive real value, // 1).Analog 3dB cutoff frequency/frequencies for analog filter, in the range [0, Inf] {rad/sec} // 2).Normalised digital 3dB cutoff frequency/frequencies for digital filter, in the range [0, 1] {dimensionless} - //Description //This function generates a Bessel filter. The default is a Laplace space (s) or analog filter. //If second argument is scalar the third parameter takes in high or low, the default value being low. The cutoff is Wc rad/sec. //If second argument is vector of length 2 ie [Wl Wh] then third parameter may be pass or stop default is pass for bandpass and band reject filter respectively //[z,p,g] = besself(...) returns filter as zero-pole-gain rather than coefficients of the numerator and denominator polynomials. //[...] = besself(...,’z’) returns a discrete space (Z) filter. Wc must be less than 1 {dimensionless}. - - //Examples //[b, a]=besself(2,.3,"high","z") //Output : @@ -46,14 +40,14 @@ function [a, b, c, d] = besself (n, w, varargin) // // 0.4668229 - 0.9336457 0.4668229 // - + // Dependencies + // besselap bilinear sftrans zp2tf funcprot(0); [nargout nargin] = argn(); if (nargin > 4 | nargin < 2 | nargout > 4 | nargout < 2) error("besself: invalid number of inputs") end - // interpret the input parameters if (~ (isscalar (n) & (n == fix (n)) & (n > 0))) error ("besself: filter order N must be a positive integer"); @@ -79,14 +73,13 @@ function [a, b, c, d] = besself (n, w, varargin) error ("besself: expected [high|stop] or [s|z]"); end end - // FIXME: Band-pass and stop-band currently non-functional, remove // this check once low-pass to band-pass transform is implemented. if (~ isscalar (w)) error ("besself: band-pass and stop-band filters not yet implemented"); end - [rows_w colums_w] = size(w); + [rows_w columns_w] = size(w); if (~ ((length (w) <= 2) & (rows_w == 1 | columns_w == 1))) error ("besself: frequency must be given as WC or [WL, WH]"); @@ -115,7 +108,6 @@ function [a, b, c, d] = besself (n, w, varargin) if (digital) [zero, pole, gain] = bilinear (zero, pole, gain, T); end - // convert to the correct output form if (nargout == 2) // a = real (gain * poly (zero)); @@ -127,7 +119,28 @@ function [a, b, c, d] = besself (n, w, varargin) c = gain; else // output ss results - // [a, b, c, d] = zp2ss (zero, pole, gain); + // FIXME : test zp2ss + //[a, b, c, d] = zp2ss (zero, pole, gain); error("besself: yet not implemented in state-space form OR invalid number of o/p arguments") end endfunction + +/* +Note : This function is tested with Octave's outputs as a reference. +# Test input validation +[a, b] = besself () // error passed +[a, b] = besself (1) // error passed +[a, b] = besself (1, 2, 3, 4, 5) // error passed +[a, b] = besself (.5, .2) // error passed +[a, b] = besself (3, .2, "invalid") // error passed + //[b, a] = besself(n, Wc) +[b a] = besself(2,1000) // passed +//[b, a] = besself (n, Wc, "high") +[b a] = besself(7,0.3,"high") // passed +//[b, a] = besself (n, [Wl, Wh]) +[b a] = besself(4,[1000 5000]) // error: besself: band-pass and stop-band filters not yet implemented +//[b, a] = besself (n, [Wl, Wh], "stop") +[b a] = besself(4,[1000 5000],"stop") // besself: band-pass and stop-band filters not yet implemented +//[z, p, g] = besself (…) +[z,p,g] = besself(9,0.8,"high") // passed +*/
\ No newline at end of file diff --git a/macros/bilinear.sci b/macros/bilinear.sci index d1ee3f7..1aa408a 100644 --- a/macros/bilinear.sci +++ b/macros/bilinear.sci @@ -1,15 +1,14 @@ // Copyright (C) 2018 - IIT Bombay - FOSSEE -// // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt // Original Source : https://octave.sourceforge.io/signal/ -// Modifieded by:Sonu Sharma, RGIT Mumbai +// Modifieded by: Abinash Singh Under FOSSEE Internship +// Date of Modification: 3 Feb 2024 // Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in - function [Zz, Zp, Zg] = bilinear(Sz, Sp, Sg, T) //Transforms a s-plane filter (Analog) into a z-plane filter (Digital) using Bilinear transformation @@ -48,13 +47,15 @@ function [Zz, Zp, Zg] = bilinear(Sz, Sp, Sg, T) // b = // // 0. - 0.1666667 - 0.3333333 2.5 - + // Dependencies + // tf2zp postpad zp2tf prepad funcprot(0); [nargout nargin] = argn(); ieee(2); if nargin==3 T = Sg; + // FIXME : tf2zp is not tested yet [Sz, Sp, Sg] = tf2zp(Sz, Sp); elseif nargin~=4 error("bilinear: invalid number of inputs") @@ -77,9 +78,6 @@ function [Zz, Zp, Zg] = bilinear(Sz, Sp, Sg, T) if Zg == 0 & nargout == 3 then error("bilinear: invalid value of gain due to zero(s) at infinity avoid z-p-g form and use tf form ") end - - - Zp = (2+Sp*T)./(2-Sp*T); SZp = size(Zp); if isempty(Sz) @@ -88,8 +86,6 @@ function [Zz, Zp, Zg] = bilinear(Sz, Sp, Sg, T) Zz = [(2+Sz*T)./(2-Sz*T)]; Zz = postpad(Zz, p, -1); end - - if nargout==2 // zero at infinity Zz1 = []; @@ -115,3 +111,17 @@ function [Zz, Zp, Zg] = bilinear(Sz, Sp, Sg, T) end ieee(0); endfunction +/* +// FIXME- not working with three argument + +Note : This function is tested with Octave's outputs as a reference. + +[Zb,Za] = bilinear([1 0],[1 1],1,0.5) // passed +[Zb, Za] = bilinear([], [], 1, 1) // error PASSED +[Zb, Za] = bilinear([0], [], 1, 0.5) // error PASSED +[Zb, Za] = bilinear([], [0], 1, 0.5) // PASSED + +[Zb, Za] = bilinear([2], [1], 1, 0.5) // PASSED +[Zb, Za] = bilinear([1; -1], [0.5; -0.5], 2, 1) //PASSED +[Zb, Za] = bilinear([0], [1], 1, 1) //PASSED +*/
\ No newline at end of file diff --git a/macros/postpad.sci b/macros/postpad.sci index f8042be..3797e08 100644 --- a/macros/postpad.sci +++ b/macros/postpad.sci @@ -1,49 +1,57 @@ // Copyright (C) 2018 - IIT Bombay - FOSSEE -// // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt -// Author:Sonu Sharma, RGIT Mumbai +// Author: Abinash Singh Under FOSSEE Internship +// Modifieded by: Abinash Singh Under FOSSEE Internship +// Last Modified on : 3 Feb 2024 // Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in - -// This is a supporting function - -function y = postpad(x, n, varargin) - - //Calling Sequences - // Y = postpad (X, L) - // Y = postpad (X, L, C) - - // Description : - // Append the scalar value C to the vector X until it is of length L. - // If C is not given, a value of 0 is used. - // - // If 'length (X) > L', elements from the end of X are removed until a - // vector of length L is obtained. - - //Example : - //x = [1 2 3]; - //L = 6; - //y = postpad(x, L) - //Output : - // y = - // - // 1. 2. 3. 0. 0. 0. - - funcprot(0); - if argn(2) > 3 | argn(2) < 2 then - error("postpad : wrong number of input argument ") - elseif argn(2) == 2 +/* +Calling Sequence : + postpad (x, l) + postpad (x, l, c) + postpad (x, l, c, dim) +Append the scalar value c to the vector x until it is of length l. If c is not given, a value of 0 is used. +If length (x) > l, elements from the end of x are removed until a vector of length l is obtained. +If x is a matrix, elements are appended or removed from each row. +If the optional argument dim is given, operate along this dimension. +If dim is larger than the dimensions of x, the result will have dim dimensions. +*/ +function res = postpad(x,l,c,dim) + if nargin < 2 then + error("Usage : postpad(x,l,c(optional),dim(optional))") + end + if nargin < 3 then c = 0 ; - else - c = varargin(1); end - - y = x; - for i = 1:(n-length(x)) - y = [y c]; + if nargin <= 4 then + if size(x,1) == 1 then + dim = 1 ; + elseif size(x,2) == 1 then + dim = 2 ; + else + dim = 2 ; // FIXME dim functionality not implemented + end + end + if l < size(x,dim) then + error("l must be greater then dimension of x") + end + + select dim + case 1 then + res = [x c*ones(size(x,1),l-size(x,2))]; + case 2 then + res = [x;c*ones(l-size(x,1),size(x,2))]; end endfunction + +/* +#test for row vectors +postpad([1 2 3 4],6) //passed +postpad([1 ;2 ;3 ;4],6) // passed +postpad([1 2 3 4;5 6 7 8;9 10 11 12],6) // passed +postpad([1 2 ;3 4;5 6],6,-1) //passed +*/
\ No newline at end of file diff --git a/macros/prepad.sci b/macros/prepad.sci index 4e517f8..d3595bf 100644 --- a/macros/prepad.sci +++ b/macros/prepad.sci @@ -1,83 +1,65 @@ // Copyright (C) 2018 - IIT Bombay - FOSSEE -// // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt -// Author:[insert name] +// Author: Abinash Singh Under FOSSEE Internship +// Modifieded by: Abinash Singh Under FOSSEE Internship +// Last Modified on : 3 Feb 2024 // Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in +/* +Calling Sequence : + prepad (x, l) + prepad (x, l, c) + prepad (x, l, c, dim) - -function y = prepad (x, l, c, dim) -// Calling sequence: -// y= prepad (x, l) -// y= prepad (x, l, c) -// y= prepad (x, l, c, dim) -// Prepend the scalar value c to the vector x until it is of length -// l. If c is not given, a value of 0 is used. -// -// If length (x) > l, elements from the beginning of x -// are removed until a vector of length l is obtained. -// -// If x is a matrix, elements are prepended or removed from each row. -// -// If the optional argument dim is given, operate along this dimension. -// -// If dim is larger than the dimensions of x, the result will have -// dim dimensions. - -//Test cases: -//prepad ([1,2], 4,0,2) -//Output: [0,0,1,2] - - -[nargout,nargin]=argn(); - if (nargin < 2 | nargin > 4) - error("wrong number of input arguments"); - end - - if (nargin < 3 | isempty (c)) - c = 0; - else - if (~ isscalar (c)) - error ("prepad: pad value C must be empty or a scalar"); +Prepend the scalar value c to the vector x until it is of length l. If c is not given, a value of 0 is used. +If length (x) > l, elements from the beginning of x are removed until a vector of length l is obtained. +If x is a matrix, elements are prepended or removed from each row. +If the optional argument dim is given, operate along this dimension. +If dim is larger than the dimensions of x, the result will have dim dimensions. +*/ +function res = prepad(x,l,c,dim) + if nargin < 2 then + error("Usage : postpad(x,l,c(optional),dim(optional))") end - end -//dim=1; - nd = ndims (x); - sz = size (x); - if (argn(2) < 4) - // Find the first non-singleton dimension. - dim = find (sz > 1, 1) - if (dim == []) - dim = 1 + if nargin < 3 then + c = 0 ; end - else - if (~(isscalar (dim) & dim == fix (dim) & dim >= 1)) - error ("prepad: DIM must be an integer and a valid dimension"); + if nargin < 4 then + dim = find(size(x)>1) + if isempty(dim) then dim = 2 end + if isvector(dim) then dim = dim(1) end end - end - - if (~ isscalar (l) | l < 0) - error ("prepad: length L must be a positive scalar"); - end - if (dim > nd) - sz(nd+1:dim) = 1; - end - - d = sz(dim); + if l < size(x,dim) then + if isvector(x) then + start = length(x) - l+1; + res=x(start:$) + return; + else + error("prepad : l must be greter than dim size for matrices") + end + + end + + if dim == 1 then + res = [ c* ones( l - size(x,1) , size(x,2)) ; x] + elseif dim == 2 then + res = [ c* ones( size(x,1) , l - size(x,2)) x] + else + error("prepad : Invalid value for arg dim 1 or 2 expected") + end +endfunction - if (d >= l) -// idx = repmat ({':'}, nd, 1); -// idx(dim) = (d-l+1) : d; -// y = x(idx(:)); - y=x; - else - sz(dim) = l - d; - y = cat (dim, c(ones (sz(1),sz(2))), x); - end +/* +#test for row vectors +prepad([1 2 3 4],6) +prepad([1 ;2 ;3 ;4],6) +prepad([1 2 3 4;5 6 7 8;9 10 11 12],6) +prepad([1 2 ;3 4;5 6],6,-1) -endfunction +// FIXME : Tests for 2d and high dimesnsional matrices +*/ diff --git a/macros/sftrans.sci b/macros/sftrans.sci index 2dfd2bb..f0c092e 100644 --- a/macros/sftrans.sci +++ b/macros/sftrans.sci @@ -1,15 +1,14 @@ // Copyright (C) 2018 - IIT Bombay - FOSSEE -// // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt // Original Source : https://octave.sourceforge.io/signal/ -// Modifieded by:Sonu Sharma, RGIT Mumbai +// Modifieded by: Abinash Singh Under FOSSEE Internship +// Date of Modification: 3 Feb 2024 // Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in - function [Sz, Sp, Sg] = sftrans (Sz, Sp, Sg, W, stop) //Transform band edges of a prototype filter (cutoff at W=1) represented in s-plane zero-pole-gain form (Frequency Transformation in Analog domain). @@ -71,7 +70,8 @@ function [Sz, Sp, Sg] = sftrans (Sz, Sp, Sg, W, stop) // Sz = // // 20. 10. 6.6666667 - + // dependencies + // funcprot(0); [nargout nargin]= argn(); @@ -115,12 +115,12 @@ function [Sz, Sp, Sg] = sftrans (Sz, Sp, Sg, W, stop) Sp = [b+sqrt(b.^2-Fh*Fl), b-sqrt(b.^2-Fh*Fl)]; extend = [sqrt(-Fh*Fl), -sqrt(-Fh*Fl)]; if isempty(Sz) - Sz = [extend(1+rem([1:2*p],2))]; + Sz = [extend(1+modulo([1:2*p],2))]; else b = (C*(Fh-Fl)/2)./Sz; Sz = [b+sqrt(b.^2-Fh*Fl), b-sqrt(b.^2-Fh*Fl)]; if (p > z) - Sz = [Sz, extend(1+rem([1:2*(p-z)],2))]; + Sz = [Sz, extend(1+modulo([1:2*(p-z)],2))]; end end else @@ -178,3 +178,13 @@ function [Sz, Sp, Sg] = sftrans (Sz, Sp, Sg, W, stop) end end endfunction +/** +Note : This function is tested with Octave's outputs as a reference. + +[Sz_new , Sp_new , Sg_new ] = sftrans([0.5;-0.5],[0.3 ; -0.3],2,0.5,0) // passed +[Sz_new , Sp_new , Sg_new ] = sftrans([0.5;-0.5],[0.3 ; -0.3],2,0.5,1) // passed +[Sz_new, Sp_new, Sg_new] = sftrans([0.5], [0.3], 1, [1.0, 2.0], 0) //passed +[Sz_new, Sp_new, Sg_new] = sftrans([0.5], [0.3], 1, [1.0, 2.0], 1) //passed +[Sz_new, Sp_new, Sg_new] = sftrans([], [], 1, 1.0, 0) //error passed +[Sz_new, Sp_new, Sg_new] = sftrans([0], [], 1, 1.0, 0)//error passed +*/
\ No newline at end of file diff --git a/macros/tf2zp.sci b/macros/tf2zp.sci index ca10fd4..f607ba0 100644 --- a/macros/tf2zp.sci +++ b/macros/tf2zp.sci @@ -1,13 +1,14 @@ // Copyright (C) 2018 - IIT Bombay - FOSSEE -// // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt -//Original contribution: FOSSEE, IIT Bombay +// Original Source : https://octave.sourceforge.io/signal/ +// Modifieded by: Abinash Singh Under FOSSEE Internship +// Date of Modification: 3 Feb 2024 +// Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in - function [z,p,k]=tf2zp(num,den) // Transfer function to zero pole conversion @@ -60,8 +61,9 @@ function [z,p,k]=tf2zp(num,den) if(rd>1) then error("Denominator must be row vector"); - elseif np>cod then - error("Improper transfer function"); + + // elseif np>cod then + // error("Improper transfer function"); it is a bug it should be removed end if (~isempty(den)) then coef=den(1); @@ -98,3 +100,9 @@ function [z,p,k]=tf2zp(num,den) end z=roots(num); endfunction +/* +Note : This function is tested with Octave's outputs as a reference. +[z p k] = tf2zp([9.6500e+02 -1.1773e+05 3.8648e+06 -4.5127e+071.5581e+08],[ 1 -3 2]) //pass +[z p k] = tf2zp([1 2 3 4],[ 1 -3 2]) //pass +[z p k] = tf2zp([4 5 6],[1]) // pass +*/
\ No newline at end of file diff --git a/macros/zp2tf.sci b/macros/zp2tf.sci index da3e8b1..10da276 100644 --- a/macros/zp2tf.sci +++ b/macros/zp2tf.sci @@ -1,14 +1,14 @@ // Copyright (C) 2018 - IIT Bombay - FOSSEE -// // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt -// Author:Sonu Sharma, RGIT Mumbai +// Original Source : https://octave.sourceforge.io/signal/ +// Modifieded by: Abinash Singh Under FOSSEE Internship +// Date of Modification: 3 Feb 2024 // Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in - function [num, den] = zp2tf (z, p, k) //Converts zeros / poles to a transfer function. @@ -54,3 +54,7 @@ function [num, den] = zp2tf (z, p, k) den = flipdim(den,2); endfunction +/* +[num,den] = zp2tf([1 3 4 5],[-4 -3 1 4],7) //passed +[num,den] = zp2tf([15 78 6 23],[2 1],965) //passed +*/
\ No newline at end of file |