summaryrefslogtreecommitdiff
path: root/macros/filtic.sci
blob: 56367b7de416eb038bed9e2fd690e4c3eeb73f79 (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
function zf = filtic(b,a,y,x)
//This function finds the initial conditions for the delays in the transposed direct-form II filter implementation
//Calling Sequence
//zf = filtic (b, a, y)
//zf = filtic (b, a, y, x)
//Parameters 
//b: vector of real or complex numbers 
//a: vector of real or complex numbers 
//y: vector of real or complex numbers 
//x: vector of real or complex numbers 
//Description
//This function finds the initial conditions for the delays in the transposed direct-form II filter implementation.
//The vectors b and a represent the numerator and denominator coefficients of the filter's transfer function. 
//Examples
//filtic([%i,1,-%i,5], [1,2,3*%i], [0.8*%i,7,9])
//ans =
//    0.00000 - 22.60000i
//    2.40000 +  0.00000i
//    0.00000 +  0.00000i

  if (argn(2)>4 | argn(2)<3) | (argn(1)>1)
    error("Wrong number of input agruments.")
  end
  if argn(2) < 4, x = []; end

  nz = max(length(a)-1,length(b)-1);
  zf=zeros(nz,1);

  
  if length(a)<(nz+1)
    a(length(a)+1:nz+1)=0;
  end
  if length(b)<(nz+1)
    b(length(b)+1:nz+1)=0;
  end
 
  if length(x) < nz
    x(length(x)+1:nz)=0;
  end
  if length(y) < nz
    y(length(y)+1:nz)=0;
  end

  for i=nz:-1:1
    for j=i:nz-1
      zf(j) = b(j+1)*x(i) - a(j+1)*y(i)+zf(j+1);
    end
    zf(nz)=b(nz+1)*x(i)-a(nz+1)*y(i);
  end

endfunction