summaryrefslogtreecommitdiff
path: root/macros/intfilt.sci
blob: 44fffbfc40627b449446f97d88f267f9b54a4dd5 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
function [h, a]= intfilt(R, L, freqmult)


    // This function estimate Interpolated FIR Filter Design.
    // Calling Sequence
    // h=intfilt(R,L,freqmult)
    // [h a]=intfilt(R,L,freqmult)

    // Parameters
    // R: Samples. It should be numeric
    // L: bandlimited interpolation samples. It must be nonzero.
    // freqmult: bandlimitedness of ALPHA times the Nyquist frequency, IT can be numeric or character ('B' or 'L', B is length
    //   (N+1)*L-1 for N odd and (N+1)*L for N even)

    // h:  linear phase FIR filter.

    // Examples
    // h=intfilt(20,10,'l')     // The output of this example has 220 columns ,so it is difficult to write it here.
    // h=intfilt(20,10,1)     // The output of this example has 220 columns ,so it is difficult to write it here.

    //h1=intfilt(2,3,'l');
    //OUTPUT :
    //               - 0.0625    0.    0.5625    1.    0.5625    0.  - 0.0625

    //h2=intfilt(4,1,1);
    //OUTPUT :
    //               0.3001054    0.6366198    0.9003163    1.    0.9003163    0.6366198    0.3001054

    // See also
    // Authors
    // Jitendra Singh

funcprot(0);
[lhs,rhs]=argn(0);

if (rhs~=3) then
	error ("Wrong number of input arguments.")
end

if (lhs<1 | lhs>2) then
	error ("Wrong number of input arguments.")
end

    if or(type(R)==10) | or(type(L)==10) then
              error ('Argument R and L must be numeric.')

              else




         if argn(2)==3 then
                   if type(freqmult)==10 then
                             typ=freqmult;
                             n=L;
                   else
                             freqmult=double(freqmult);
                             typ='b';
                             end

     end

     if freqmult==0 then
         h=repmat(%nan,[1,(2*R*L-1)])
         a=1;
         else


     //typ(1)=='b' | typ(1)=='B'

         if convstr(typ(1), 'u') =='B' then
                   n=2*R*L-1;


                   if freqmult==1 then
                             M=[R R 0 0];
                             F= [0 1/(2*R) 1/(2*R) 0.5];
                   else
                             M=R*[1 1];

                  if type(freqmult)==10 then
               F=[0 98/2/R];
           else
               F=[0 freqmult/2/R]
               end

        for f=(1/R):(1/R):.5,

            if type(freqmult)==10 then
            F=[F f-(98/2/R) f+(98/2/R)];
        else
            F=[F f-(freqmult/2/R) f+(freqmult/2/R)];
            end

            M=[M 0 0];
        end;

        if (F(length(F))>.5),
            F(length(F))=.5;
        end;
    end
        N=n-1; F=F*2; M=M


if (max(F)>1) | (min(F)<0)
    error('Frequencies in F must be in range [0,1]')
end




if ((length(F)-fix(length(F)./2).*2)~=0)
    error('Argument F should of even length');
end

if (length(F) ~= length(M))
    error('The input arguments F & A must have same length');
end


    W = ones(length(F)/2,1);
    ftype = '';


    ftype = 0;  differ = 0;


N = N+1;

F=F(:)/2;  M=M(:);  W=sqrt(W(:));
dF = diff(F);

if (length(F) ~= length(W)*2)
    error('There should be one weight per band.');
end


if or(dF<0),

    error('F frequency must be increasing')

end



if and(dF(2:2:length(dF)-1)==0) & length(dF) > 1,
    band = 1;
else
    band = 0;
end
if and((W-W(1))==0)
    weights = 1;
else
    weights = 0;
end

L=(N-1)/2;

Nodd = N-fix(N./2).*2;


    if ~Nodd
        m=(0:L)+.5;
    else
        m=(0:L);
    end


    k=m';
    need_matrix = (~band) | (~weights);




    if need_matrix

        I1=k(:,ones(size(m,1),size(m,2)))+m(ones(size(k,1),size(k,2)),:);
        I2=k(:,ones(size(m,1),size(m,2)))-m(ones(size(k,1),size(k,2)),:);
        G=zeros(size(I1,1),size(I1,2));
    end

    if Nodd
        k=k(2:length(k));
        b0=0;
    end;
    b=zeros(size(k,1),size(k,2));

    dd=diff(F);

    if or(dd==0) & R==1 then

        h=repmat(%nan,[1,n])
        a=1

        else
    for s=1:2:length(F),


        m=(M(s+1)-M(s))/(F(s+1)-F(s));
        b1=M(s)-m*F(s);
        if Nodd
            b0 = b0 + (b1*(F(s+1)-F(s)) + m/2*(F(s+1)*F(s+1)-F(s)*F(s)))* abs(W((s+1)/2)^2) ;
        end

      b=b(:)
        b = b+(m/(4*%pi*%pi)*(cos(2*%pi*k*F(s+1))-cos(2*%pi*k*F(s)))./(k.*k))* abs(W((s+1)/2)^2);



        b = b' + (F(s+1)*(m*F(s+1)+b1)*sinf(2*k*F(s+1))- F(s)*(m*F(s)+b1)*sinf(2*k*F(s)))* abs(W((s+1)/2)^2);
        if need_matrix



               mat=matrix((.5*F(s+1)*(sinf(2*I1*F(s+1))+sinf(2*I2*F(s+1)))- .5*F(s)*(sinf(2*I1*F(s))+sinf(2*I2*F(s))) ) * abs(W((s+1)/2)^2),size(G,1),size(G,2)) ;
                mat=mat';
                G=G+mat;


        end
    end;


    if Nodd
        b=[b0; b'];
    end;

    if need_matrix
        a=G\b;
    else
        a=(W(1)^2)*4*b;
        if Nodd
            a(1) = a(1)/2;
        end
    end
    if Nodd
        h=[a(L+1:-1:2)/2; a(1); a(2:L+1)/2].';
    else
        h=.5*[flipud(a); a].';
    end;
     end;

  //typ(1)=='l' | typ(1)=='L'



       elseif convstr(typ(1), 'u') =='L'  then

            if n==0 then
                      h=ones(1,R)
                      return
                      end

        t=0:n*R+1;
        l=ones(n+1,length(t));

        for i=1:n+1
                  for j=1:n+1
                            if (j~=i) then
                                      l(i,:)=l(i,:).*(t/R-j+1)/(i-j);
                            end
                  end
        end

        h=zeros(1,(n+1)*R);

        for i=0:R-1
        for j=0:n
                  h(j*R+i+1)=l((n-j)+1,round((n-1)/2*R+i+1));
        end
end



if h(1) == 0,
        h(1) = [];
    end

else
          error ('This type of filter is not recognized.')


         end
         a=1;


 end
 end

endfunction



////// Supplementary function

function y=sinf(x)

     for i=1:length(x)
               if x(i)==0 then
                         y(i)=1;
                         else

               y(i)=sin(%pi*x(i))/(%pi*x(i));
               end

               end

          y=y';
endfunction