summaryrefslogtreecommitdiff
path: root/macros/cell2sos.sci
diff options
context:
space:
mode:
Diffstat (limited to 'macros/cell2sos.sci')
-rw-r--r--macros/cell2sos.sci197
1 files changed, 106 insertions, 91 deletions
diff --git a/macros/cell2sos.sci b/macros/cell2sos.sci
index 39402b3..7cd657d 100644
--- a/macros/cell2sos.sci
+++ b/macros/cell2sos.sci
@@ -1,95 +1,110 @@
-function [s,g] = cell2sos(c)
-//Converts a cell array to a second order section matrix
-//Calling Sequences
-//s=cell2sos(c)
-//[s,g]=cell2sos(c)
-//Parameters
-//c
-//A cell array
-//g
-//The scalar gain
-//Description
-//s=cell2sos(c) converts a a cell array c = { {B1},{A1}, {B2},{A2}, ... {BL},{AL}}
-//to an L-by-6 second-order-section matrix s given by:
-// s = [B1 A1
-// B2 A2
-// ...
-// BL AL]
-//numerator vector Bi and denominator vector Ai contains the coefficients of a
-//linear or quadratic polynomial. If the polynomial is linear, the coefficients
-//zero-padded on the right.
-//[s,g]=cell2sos(c) estimates the gain from the leading term of the cell array
-//c={ {[g1,g2]},{B1},{A1}, {B2},{A2}, ... {BL},{AL}} to give g=g1/g2 as the gain
-//Example
-//c=cell(1,5);
-//
-//c(1,1).entries=[2, 1];
-//
-//c(1,2).entries=rand(1,3);
-//
-//c(1,3).entries=rand(1,3);
-//
-//c(1,4).entries=rand(1,3);
-//
-//c(1,5).entries=rand(1,3);
-//
-// c =
-// column 1 to 3
-//
-//![2,1] [0.2113249,0.7560439,0.0002211] [0.3303271,0.6653811,0.6283918] !
-//
-// column 4 to 5
-//
-//![0.8497452,0.6857310,0.8782165] [0.0683740,0.5608486,0.6623569] !
-//[s,g]=cell2sos(c);
-//s =
-//
-// column 1 to 5
-//
-// 0.2113249 0.7560439 0.0002211 0.3303271 0.6653811
-// 0.8497452 0.6857310 0.8782165 0.0683740 0.5608486
-//
-// column 6
-//
-// 0.6283918
-// 0.6623569
-//
-//g =
-//
-// 2.
-//Author
-//Ankur Mallick
- if(argn(2)~=1) then
- error("Wrong number of input arguments");
- end
+// 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: Abinash Singh Under FOSSEE Internship
+// Modifieded by: Abinash Singh Under FOSSEE Internship
+// Last Modified : 3 Feb 2024
+// Organization: FOSSEE, IIT Bombay
+// Email: toolbox@scilab.in
+/*
+Calling Sequence :
+ sos = cell2sos(cll)
+ [sos,g] = cell2sos(cll)
+Description
+ sos = cell2sos(cll) generates a matrix sos containing the coefficients of the filter system described by the second-order section cell array cll.
+ [sos,g] = cell2sos(cll) also returns the scale gain g.
+
+ Second-order section cell-array representation, specified as a cell array.
+
+Input Argument:
+ For a filter system with L sections, specify 'cll' using this structure:
+
+ * Cell array with L elements — For unity-gain filter systems. Each element of the cell
+ array corresponds to a second-order section. The kth cell array element of 'cll'
+
+ cll{k} = {[b_0k b_1k b_2k] [1 a_1k a_2k]}
+
+ contains the coefficients from the kth second-order-section of the filter system H(z):
+
+ H(z) = product(k=1 to L) H_k(z)
+ = product(k=1 to L) (b_0k + b_1k*z^(-1) + b_2k*z^(-2))/(1 + a_1k*z^(-1) + a_2k*z^(-2))
+
+ * Cell array with L+1 elements — If the gain of the filter system is different from 1.
+ The first element of 'cll' contains the system gains at the numerator (g_n) and at
+ the denominator (g_d). Then, the function appends each element of the cell array for
+ the corresponding second-order section.
+
+ The first and the k+1th cell array element of 'cll'
- L=prod(size(c));
- for i=1:L
- if(type(c(i))~=17)
- error('Cell contents must themselves be cell objects');
+ cll{1} = {g_n g_d}
+ cll{k+1} = {[b_0k b_1k b_2k] [1 a_1k a_2k]}
+
+ contain the system gain and the coefficients from the kth second-order section of
+ the filter system H(z), respectively, such that:
+
+ H(z) = (g_n/g_d) * product(k=1 to L) H_k(z)
+ = (g_n/g_d) * product(k=1 to L) (b_0k + b_1k*z^(-1) + b_2k*z^(-2))/(1 + a_1k*z^(-1) + a_2k*z^(-2))
+
+Output Argument:
+ Second-order section representation, returned as an L-by-6 matrix, where L is the
+ number of second-order sections. The matrix
+
+ sos = [b_01 b_11 b_21 1 a_11 a_21]
+ [b_02 b_12 b_22 1 a_12 a_22]
+ [ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ]
+ [b_0L b_1L b_2L 1 a_1L a_2L]
+
+ represents the second-order sections of H(z):
+
+ H(z) = g * product(k=1 to L) H_k(z)
+ = g * product(k=1 to L) (b_0k + b_1k*z^(-1) + b_2k*z^(-2))/(1 + a_1k*z^(-1) + a_2k*z^(-2))
+*/
+
+function [s,g] = cell2sos(c)
+ if(argn(2)~=1) then
+ error("cell2sos: Wrong number of input arguments");
end
- end
- if (argn(1)==2)
- d=c(1).entries;
- if(length(d)==2)
- g1=d(1);
- g2=d(2);
- g=g1/g2;
- c=c(2:L);
- else
- g=1;
+ L=prod(size(c));
+ k=1
+ for i=1:L
+ if(type(c(i))~=17)
+ error('cell2sos: Cell contents must themselves be cell objects');
+ end
+ end
+ gain_p = 0
+ if length(cell2mat(c{1}))== 2 then
+ gain_vec=cell2mat(c{1})
+ k=gain_vec(1)/gain_vec(2)
+ L=L-1
+ gain_p=1
+ end
+ s = zeros(L,6);
+ for i=1:L
+ if gain_p
+ s(i,:)=cell2mat(cll{i+1})
+ else
+ s(i,:)=cell2mat(cll{i})
+ end
end
- end
- L=prod(size(c));
- s=zeros(L/2,6);
- for i=1:2:L-1
- j=ceil(i/2)
- b=c(i).entries;
- a=c(i+1).entries;
- b=b(:).';
- a=a(:).';
- b=[b,zeros(1,3-length(b))];
- a=[a,zeros(1,3-length(b))];
- s(j,:)=[b,a];
- end
+ if nargout < 2 then
+ s(1,1:3)= k * s(1,1:3)
+ else
+ g=k;
+ end
endfunction
+
+/*
+cll = {{[3 6 7] [1 1 2]}
+ {[1 4 5] [1 9 3]}
+ {[2 7 1] [1 7 8]}};
+sos = cell2sos(cll) // passed
+
+cll = {{1 2} {[3 6 7] [1 1 2]}
+ {[1 4 5] [1 9 3]}
+ {[2 7 1] [1 7 8]}};
+sos = cell2sos(cll) // passed
+
+*/ \ No newline at end of file