summaryrefslogtreecommitdiff
path: root/macros/residue.sci
blob: 70dc91fdfbc2d8f36d80078ea71d9afe4c09bec8 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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]
// Organization: FOSSEE, IIT Bombay
// Email: toolbox@scilab.in


function [r, p, k, e] = residue (b, a, varargin)


//  [r, p, k, e] = residue (b, a)
//  [b, a] = residue (r, p, k)
// [b, a] = residue (r, p, k, e)
// The first calling form computes the partial fraction expansion for the
// quotient of the polynomials, b and a.
//
// The quotient is defined as

// B(s)    M       r(m)        N
// ---- = SUM ------------- + SUM k(i)*s^(N-i)
// A(s)   m=1 (s-p(m))^e(m)   i=1

// where M is the number of poles (the length of the r, p,
// and e), the k vector is a polynomial of order N-1
// representing the direct contribution, and the e vector specifies the
// multiplicity of the m-th residue's pole.
//
//NOTE that the polynomials 'b' and 'a' should have real coefficients(because of the function 'filter' used in polyval)
//
//Test case
//1.
// b = [1, 1, 1];
// a = [1, -5, 8, -4];
// [r, p, k, e] = residue (b, a)
//    result r = [-2; 7; 3]
//    result p = [2; 2; 1]
//    result k = [](0x0)
//    result e = [1; 2; 1]
//

//2.
//[r,p,k,e]=residue([1 2 1],[1 -5 8 -4])
//OUTPUT
//r =
//  -3.0000
//   9.0000
//   4.0000
//
//p =
//   2.0000
//   2.0000
//   1.0000
//
//f = [](0x0)
//e =
//   1
//   2
//   1
//


 [nargout,nargin]=argn();

  if (nargin < 2 | nargin > 4)
    error ("wrong umber of input arguments");
  end

  toler = .001;

  if (nargin >= 3)
    if (nargin >= 4)
      e = varargin(2);
    else
      e = [];
    end
    // The inputs are the residue, pole, and direct part. Solve for the
    // corresponding numerator and denominator polynomials
    [r, p] = rresidue (b, a, varargin(1), toler, e);
    return;
  end

  // Make sure both polynomials are in reduced form.

  a = polyreduce (a);
  b = polyreduce (b);

  b = b / a(1);
  a = a / a(1);

  la = length (a);
  lb = length (b);

  // Handle special cases here.

  if (la == 0 | lb == 0)
    k =[];
    r = [];
    p = [];
    e = [];
    return;
  elseif (la == 1)
    k = b / a;
    r = [];
    p = [];
    e = [];
    return;
  end

  // Find the poles.

  p = roots (a);
  lp = length (p);

  // Sort poles so that multiplicity loop will work.

  [e, indx] = mpoles (p, toler, 0);
  p = p(indx);

  // For each group of pole multiplicity, set the value of each
  // pole to the average of the group. This reduces the error in
  // the resulting poles.

  p_group = cumsum (e == 1);
  for ng = 1:p_group($)
    m = find (p_group == ng);
    p(m) = mean (p(m));
  end

  // Find the direct term if there is one.

  if (lb >= la)
    // Also return the reduced numerator.
    [k, b] = deconv (b, a);
    lb = length (b);
  else
    k = [];
  end

  // Determine if the poles are (effectively) zero.

  small = max (abs (p));
  if (type(a)==1 | type(b)==1)
    small = max ([small, 1]) * 1.1921e-07 * 1e4 * (1 + length (p))^2;
  else
    small = max ([small, 1]) * %eps * 1e4 * (1 + length (p))^2;
  end
  p(abs (p) < small) = 0;

  // Determine if the poles are (effectively) real, or imaginary.

  index = (abs (imag (p)) < small);
  p(index) = real (p(index));
  index = (abs (real (p)) < small);
  p(index) = 1*%i * imag (p(index));

  // The remainder determines the residues.  The case of one pole
  // is trivial.

  if (lp == 1)
    r = polyval (b, p);
    return;
  end

  // Determine the order of the denominator and remaining numerator.
  // With the direct term removed the potential order of the numerator
  // is one less than the order of the denominator.

  aorder = length (a) - 1;
  border = aorder - 1;

  // Construct a system of equations relating the individual
  // contributions from each residue to the complete numerator.

  A = zeros (border+1, border+1);
  B = prepad (matrix (b, [length(b), 1]), border+1, 0,2);
  for ip = 1:length (p)
    ri = zeros (size (p,1),size(p,2));
    ri(ip) = 1;
    A(:,ip) = prepad (rresidue (ri, p, [], toler), border+1, 0,2).';
  end

  // Solve for the residues.
if(size(A,1)~=size(B,1))
    if(size(A,1)<size(B,1))
        A=[A;zeros((size(B,1)-size(A,1)),(size(A,2)))];
    else
        B=[zeros((size(A,1)-size(B,1)),(size(B,2)));B];
    end

    end
  r = A \ B;
  r=r(:,$);

endfunction

function [pnum, pden, e] = rresidue (rm, p, k, toler, e)

  // Reconstitute the numerator and denominator polynomials from the
  // residues, poles, and direct term.
[nargout,nargin]=argn();
  if (nargin < 2 | nargin > 5)
    error ("wrong number of input arguments");
  end

  if (nargin < 5)
    e = [];
  end

  if (nargin < 4)
    toler = [];
  end

  if (nargin < 3)
    k = [];
  end

  if (length (e))
    indx = 1:length (p);
  else
    [e, indx] = mpoles (p, toler, 0);
    p = p(indx);
    rm = rm(indx);
  end

  indx = 1:length (p);

  for n = indx
    pn = [1, -p(n)];
    if (n == 1)
      pden = pn;
    else
      pden = conv (pden, pn);
    end
  end

  // D is the order of the denominator
  // K is the order of the direct polynomial
  // N is the order of the resulting numerator
  // pnum(1:(N+1)) is the numerator's polynomial
  // pden(1:(D+1)) is the denominator's polynomial
  // pm is the multible pole for the nth residue
  // pn is the numerator contribution for the nth residue

  D = length (pden) - 1;
  K = length (k) - 1;
  N = K + D;
  pnum = zeros (1, N+1);
  for n = indx(abs (rm) > 0)
    p1 = [1, -p(n)];
    for m = 1:e(n)
      if (m == 1)
        pm = p1;
      else
        pm = conv (pm, p1);
      end
    end
    pn = deconv (real(pden),real(pm));
    pn = rm(n) * pn;
    pnum = pnum + prepad (real(pn), N+1, 0, 2);
  end

  // Add the direct term.

  if (length (k))
    pnum = pnum + conv (pden, k);
  end

  // Check for leading zeros and trim the polynomial coefficients.
  if (type(rm)==1 | type(p)==1 | type(k)==1)
    small = max ([max(abs(pden)), max(abs(pnum)), 1]) * 1.1921e-07;
  else
    small = max ([max(abs(pden)), max(abs(pnum)), 1]) *%eps;
  end

  pnum(abs (pnum) < small) = 0;
  pden(abs (pden) < small) = 0;

  pnum = polyreduce (pnum);
  pden = polyreduce (pden);

endfunction