summaryrefslogtreecommitdiff
path: root/macros/latc2tf.sci
blob: fb19e6d4fbc7d652b001499e34160dc56ff68a4e (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
function [num,den] = latc2tf(k,varargin)
    // Convert lattice filter parameters to transfer function coefficients
    //
    //
    // Calling sequence
    // [num,den] = latc2tf(k,v)
    // [num,den] = latc2tf(k,'iiroption')
    // num = latc2tf(k,'firoption')
    //
    //
    // Description
    // [num,den] = latc2tf(k,v)
    //      Finds the transfer function of the IIR filter from the lattice
    //      coefficients k and ladder coefficients v.
    // [num,den] = latc2tf(k,'iiroption')
    //      Finds the transfer function of the allpass or allpole (specified by 
    //      the iiroption flag) IIR filter.
    // num = latc2tf(k,'firoption')
    //      Finds the transfer function of the FIR filter from the lattice
    //      coefficients k. The firoption flag specifies the type of the FIR
    //      filter (can be 'min, 'max', or 'FIR')
    //
    // Parameters:
    // k - double - vector
    //      Lattice coefficients
    //      Lattice coefficients for FIR/IIR filter. Can be real or complex. 
    // v - double - vector
    //      Ladder coefficients
    //      Ladder coefficients for IIR filters. Can be real or complex.
    // iiroption - string flag - 'allpole', or 'allpass'
    //      Specification of the type if IIR filter
    // firoption - string flag - 'min', 'max', or 'FIR' (default)
    //      Speficication of the type of FIR filter
    // 
    // Examples
    // 1) FIR filter
    //      k1 = [1/2 1/2 1/4];
    //      [num1,den1] = latc2tf(k1);
    // 
    // See also
    // latcfilt | tf2latc
    //
    // References
    // [1] J.G. Proakis, D.G. Manolakis, Digital Signal Processing,
    //    3rd ed., Prentice Hall, N.J., 1996, Chapter 7.
    // [2] S. K. Mitra, Digital Signal Processing, A Computer
    //    Based Approach, McGraw-Hill, N.Y., 1998, Chapter 6.
    //
    // Authors
    // Ayush Baid
    
    
    [numOutArgs,numInArgs] = argn(0);
    
// ** Check on number of input arguments **
    if numOutArgs<1 | numOutArgs>2 then
        msg = "cummin: Wrong number of output argument; 1-2 expected";
        error(78,msg);
    end
    
    if numInArgs<1 | numInArgs>2 then
        msg = "cummin: Wrong number of input argument; 1-2 expected";
        error(77,msg);
    end
    
    
// ** Parsing the input arguments **

// 1) k:
//      must be a vector
//      can be real or complex valued, (negative values allowed)

    // checking data type
    if  ~(type(k)==1 | type(k)==8) then
        msg = "latc2tf: Wrong type for argument #1 (k); Real or complex vector expected";
        error(53,msg);
    end
    
    // checking the dimensions (must be a vector)
    if ~(size(k,1)==1 | size(k,2)==1) then
        msg = "latc2tf: Wrong type for argument #1 (k); Real or complex vector expected";
        error(53,msg);
    end
    // convert to column vector
    if size(k,1)==1 then
        k = k(:);
    end
    
    L = length(varargin);
// Parsing the 2nd argument
    if L>=1 then
        arg2 = varargin(1);
        // string check
        if type(arg2)==10 then
            if strcmpi(arg2,'FIR')==0 then
                [num,den] = latc2tf_fir(k,2);
            elseif strcmpi(arg2,'min')==0 then
                [num,den] = latc2tf_fir(k,0);
            elseif strcmpi(arg2,'max')==0 then
                [num,den] = latc2tf_fir(k,1);
            elseif strcmpi(arg2,'allpole')==0 then
                [num,den] = latc2tf_iir2(k,1);
            elseif strcmpi(arg2,'allpass')==0 then
                [num,den] = latc2tf_iir1(k);
            else
                msg = "latc2tf: Wrong value for argument #2 (string flag) ";
                error(53,msg);
            end
        elseif (type(arg2)==1 | type(arg2)==8) then
            // arg2 is ladder coefficients
            
            // check for vector input
            if ~(size(arg2,1)==1 | size(arg2,2)==1) then
                msg = "latc2tf: Wrong type for argument #2 (v); Real or complex vector expected";
                error(53,msg);
            end
            
            // convert to column vector
            if size(arg2,1)==1 then
                arg2 = arg2(:);
            end
            
            [num,den] = latc2tf_iir2(k,arg2);
            
        else
            msg = "latc2tf: Wrong type for argument #2; Real or complex vector" + ...
                "(ladder coeffs k) or string flag expected";
            error(53,msg);
        end
    else
        [num,den] = latc2tf_fir(k,2);
    end
    
    num = num';
    den = den';
    
endfunction

function [num,den] = latc2tf_fir(k,option)
    // latc2tf for FIR filters
    // Input Arguments
    // k:   lattice coefficients
    // option: 0 for min-phase (default), 1 for max-phase; 2 for fir
    
    if isempty(k) then
        num = 1;
        den = 1;
        return
    end

    den = 1;
    
    p = length(k);
    num = 1;

    for j=2:p+1
    	num = [num; 0] + k(j-1)*[0; conj(num($:-1:1,:))];
	end

	if option==1 then
		num = conj(num($:-1:1));
	end

    
endfunction

function [num,den] = latc2tf_iir1(k)
    // for all pass IIR filters
    
    if isempty(k) then
        num = 1;
        den = 1;
        return
    end

    p = length(k);
    den = 1;

    for j=2:p+1
    	den = [den; 0] + k(j-1)*[0; conj(den($:-1:1,:))];
	end

	num = conj(den($:-1:1));
    
   
endfunction 


function [num,den] = latc2tf_iir2(k,v)
    // for allpole and general IIR fitlers
    if isempty(k) & isempty(v) then
        num = [];
        den = [];
    elseif isempty(k) & length(v)==1 then
        num = v;
        den = 1;
    else
        // k is a column vector
        M = size(k,1)+1;
        
        // pad v with appropriate number of zeros
        l_v = size(v,1);
        diff = M - l_v;
        if diff>0 then
            v = [v; zeros(diff,1)];
        elseif diff<0 then
        	k = [k; zeros(-diff,1)];
    	end


        p = length(k);
    	den = 1;

    	for j=2:p+1
	    	den = [den; 0] + k(j-1)*[0; conj(den($:-1:1,:))];
		end
        
        exec('rlevinson.sci',-1);

        [r,temp] = rlevinson(den,1);

        num = (temp*v);
    end    

endfunction