~ubuntu-branches/ubuntu/raring/octave-signal/raring

« back to all changes in this revision

Viewing changes to inst/sos2tf.m

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2012-09-23 09:14:33 UTC
  • mfrom: (1.1.7)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: package-import@ubuntu.com-20120923091433-wzfir0y7ongjrubp
Tags: upstream-1.2.0
Import upstream version 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
%% Copyright (C) 2005 Julius O. Smith III
2
 
%%
3
 
%% This program is free software; you can redistribute it and/or modify
4
 
%% it under the terms of the GNU General Public License as published by
5
 
%% the Free Software Foundation; either version 2 of the License, or
6
 
%% (at your option) any later version.
7
 
%%
8
 
%% This program is distributed in the hope that it will be useful,
9
 
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
%% GNU General Public License for more details.
12
 
%%
13
 
%% You should have received a copy of the GNU General Public License
14
 
%% along with this program; If not, see <http://www.gnu.org/licenses/>.
 
1
%% Copyright (C) 2005 Julius O. Smith III <jos@ccrma.stanford.edu>
 
2
%%
 
3
%% This program is free software; you can redistribute it and/or modify it under
 
4
%% the terms of the GNU General Public License as published by the Free Software
 
5
%% Foundation; either version 3 of the License, or (at your option) any later
 
6
%% version.
 
7
%%
 
8
%% This program is distributed in the hope that it will be useful, but WITHOUT
 
9
%% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
10
%% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 
11
%% details.
 
12
%%
 
13
%% You should have received a copy of the GNU General Public License along with
 
14
%% this program; if not, see <http://www.gnu.org/licenses/>.
15
15
 
16
16
%% -*- texinfo -*-
17
17
%% @deftypefn {Function File} {[@var{B}, @var{A}] =} sos2tf (@var{sos}, @var{Bscale})
23
23
%% @item
24
24
%% @var{sos} = matrix of series second-order sections, one per row:@*
25
25
%% @var{sos} = [@var{B1}.' @var{A1}.'; ...; @var{BN}.' @var{AN}.'], where@*
26
 
%% @code{@var{B1}.'==[b0 b1 b2] and @var{A1}.'==[1 a1 a2]} for 
 
26
%% @code{@var{B1}.'==[b0 b1 b2] and @var{A1}.'==[1 a1 a2]} for
27
27
%% section 1, etc.@*
28
28
%% b0 must be nonzero for each section.@*
29
29
%% See @code{filter()} for documentation of the
33
33
%% @var{Bscale} is an overall gain factor that effectively scales
34
34
%% the output @var{B} vector (or any one of the input @var{B}i vectors).
35
35
%% @end itemize
36
 
%% 
 
36
%%
37
37
%% RETURNED:
38
38
%% @var{B} and @var{A} are vectors specifying the digital filter @math{H(z) = B(z)/A(z)}.
39
39
%% See @code{filter()} for further details.
40
 
%% 
 
40
%%
41
41
%% @seealso{tf2sos zp2sos sos2pz zp2tf tf2zp}
42
42
%% @end deftypefn
43
43
 
44
 
function [B,A] = sos2tf(sos,Bscale)
45
 
 
46
 
if nargin<2, Bscale=1; end
47
 
[N,M] = size(sos);
48
 
if M~=6, error('sos2tf: sos matrix should be N by 6'); end
49
 
A = 1;
50
 
B = 1;
51
 
for i=1:N
52
 
  B = conv(B, sos(i,1:3));
53
 
  A = conv(A, sos(i,4:6));
54
 
end
55
 
nB = length(B);
56
 
while nB & B(nB)==0, B=B(1:nB-1); nB=length(B); end
57
 
nA = length(A);
58
 
while nA & A(nA)==0, A=A(1:nA-1); nA=length(A); end
59
 
B = B * Bscale;
 
44
function [B,A] = sos2tf(sos, Bscale = 1)
 
45
 
 
46
  if (nargin < 1 || nargin > 2)
 
47
    print_usage;
 
48
  endif
 
49
 
 
50
  [N,M] = size(sos);
 
51
 
 
52
  if M~=6
 
53
    error('sos2tf: sos matrix should be N by 6');
 
54
  end
 
55
 
 
56
  A = 1;
 
57
  B = 1;
 
58
 
 
59
  for i=1:N
 
60
    B = conv(B, sos(i,1:3));
 
61
    A = conv(A, sos(i,4:6));
 
62
  end
 
63
 
 
64
  nB = length(B);
 
65
  while nB && B(nB)==0
 
66
    B=B(1:nB-1);
 
67
    nB=length(B);
 
68
  end
 
69
 
 
70
  nA = length(A);
 
71
  while nA && A(nA)==0
 
72
    A=A(1:nA-1);
 
73
    nA=length(A);
 
74
  end
 
75
  B = B * Bscale;
 
76
 
 
77
endfunction
60
78
 
61
79
%!test
62
80
%! B=[1   1];
66
84
%! assert({Bh,Ah},{B,A},10*eps);
67
85
 
68
86
%!test
69
 
%! B=[1 0 0 0 0   1]; 
 
87
%! B=[1 0 0 0 0   1];
70
88
%! A=[1 0 0 0 0 0.9];
71
89
%! [sos,g] = tf2sos(B,A);
72
90
%! [Bh,Ah] = sos2tf(sos,g);