Você está na página 1de 7

Scilab 5.3.

3 code for Type 1 Eigen Filter

Dec 2014

1. Introduction
Fir filters are designed by choosing filter coefficients to approximate in a " best manner " an ideal desired
specification in the stop and pass band using many methods. One of the methods that allows optimization of deviation energy
is the eigen filter .(ref On The Eigenfilter Design Method and Its Applications: A Tutorial
Andre Tkacenko, Student Member, IEEE, P. P. Vaidyanathan, Fellow, IEEE, andTruong Q. Nguyen, Senior Member, IEEE)
The basis of the eigen filter is the z transform for FIR viz
H(z) = h0 +h1*z^-1 +h2*z^-2 +.... hn*z^-N

which can be written as


[h0 h1 ...hn ]* [ 1 z^-1

z^-2

z^-N ]transpose

or with z=e^(jw) (w as digital freq )


H(e^jw)= h* e(jw)

where h is the coefficient vector and e(jw) is the column vector of the delay chain z

Type 1 filter is obtained by settin h(n ) = h(N-n)

N can be odd or even .

assume N= even say 4 then


H(z) = h0 +h1*z^-1 +h2*z^-2 +.... h1*z^-3+ h0*z^-4
H(e^jw) = [ h2 + 2*h1cos(w) +2* h0cos(2w) ]*e^-j2w)
or Hr(w) = bo+ b1cos(w) +b2cos(2w)

b0=2h2 b1= 2h1 b2= h0

Next using Hr(w) as the basis for design we can write


Hr(w) = [ bo b1 b2 ]*[ 1 cos(w) cos(2w) ]
we wish to determine [ bo b1 b2 ] ( and subsequently hn)..
Let us view Hr(w) with arbitray values for bn say [0.5534498 0.8161665 0.1660287
npt =100
dw= %pi/npt ;
omg= zeros(1:2*npt);
//omg(1)= -%pi ;
n1 =3
hseqa = [ 0.5534498 0.8161665 0.1660287 ]
for k=1:2*npt
xm(k)= 0;
omg(k)=omg(k)+(k-1)* dw;
for i=1:n1
xm(k) = hseqa(i)*cos(omg(k)*(i -1)) +xm(k) ;
end
end
figure(0);
plot(omg, xm);
//figure(0);

]..Then we obtain plot as below.

mx= max(xm)
xml = 20*log10( abs(xm/mx));
oma=omg/2/%pi ;
figure(1);
plot(oma, xml);
// figure(1)

Now we can convert the bvector to hvector using C matrix


C =[ 0.
0.
1.
0.
0.

0. 0.5 ;
0.5 0. ;
0.
0. ;
0.5 0. ;
0. 0.5 ; ]

so htr = C *b

hz =c*hseqa' //gives
hz =
0.0830144
0.4080832
0.5534498
0.4080832
0.0830144
2 Theory

of eigen filter method

The intuition in eigen filter is as follows :


2.1 In pass band the magnitude | H(z) | is required to be say 1
2.2 In terms of filter coefficients we want [ bo b1 b2 ... ]*[ 1 cos(w) cos(2w) ....]trans =1 at say w=0
2.3 The above is expressed as b*c(w) vectors and with w=0 this is b *1 (b vector , 1 vetor of 1's )
2.4 Hence deviation or error^2 in pass band is [ b * ( 1 -c(w) ) ] *[ ( 1 -c(w) )trans *btrans]
2.5 ( 1 -c(w) ) ] *[ ( 1 -c(w) )trans is a matrix say Q
2.6 Similarly error ^2 i stop band with 1 vector set to zero is P = c(w) ) * c(w) )trans
2.7 Now total error is integral of Q in pass band sig1 to sig2 + integral of P in Stop band sig3 to sig4
2.8 The transition band is ignored
2.9 The error is each band is wieihted with alp and we get matrix htot = alp*P +1-alp)*Q
2.10 The error is now b*htot*btrans. this is minimized using Hermitian matrix peoperties.
2.11 Because we are dealing with error squared htot turns out to be positive semidefinite
2.12 If we choose b to be the norm eigen vector corresponding to min eigen value error^2 is minimum
2.13 It is easy to find maximum eigen value/ vector by power method
2.14 So we use hin = inverse of htot
2.15 The normed max eigen vector of hin is the desired filter in b vector form
3. Modifications
Other types of filters can be obtained by imposing structures on htot matrix by e.g. weight matrix W and different reference
values for pass bands making sure that it can always be written as b*c(w) form. The actual filter coeff h(n) can be obtained using
a transformation matrix C as already shown.
4. Scilab 5.3.3 code .

The spec function of scilab 5.3.3 is verified as it gives nearly same value as the power method . N=odd length filter
The bands are specified in
sig3 =.5 *%pi // stop band
sig4 = 1*%pi // stop band
sig1 = 0*%pi // passband
sig2 =.3*%pi // pass band
The integrated p matrix is computed using special case for row =0 col =0 etc to avoid division by zero.
//P matrix stop band use sig4=pi sig3 less
if ( (rn==0)&(cn==0) ) then
p(r,c) =sig4 -sig3 ;
end ;
if ( (rn==0)&(cn>0) ) then
p(r,c) = sin(sig4 *cn)/cn
- sin(sig3 *cn)/cn ;
end ;
if ( (rn> 0)&(cn==0) ) then
p(r,c) = sin(sig4 *rn)/rn
- sin(sig3 *rn)/rn ;
end ;
if ( (rn*cn> 0) & ( rn==cn) ) then
p(r,c) = 1/2* ( sig4 -sig3 + sin(sig4 *(rn +cn ) )/ (rn+cn) - sin(sig3 *(rn +cn ) )/ (rn+cn)
end ;
if ( (rn*cn> 0) & ( abs(rn-cn) >0 ) ) then
p(r,c) = 1/2* ( sin(sig4 *(rn +cn ) )/ (rn+cn) - sin(sig3 *(rn +cn ) )/ (rn+cn) ) ;
p(r,c) = p(r,c) + 1/2* ( sin(sig4 *(rn -cn ) )/ (rn-cn) - sin(sig3 *(rn -cn ) )/ (rn-cn) ) ;

);

Similarly the Q matrix .


if ( ( rn ==0)& (cn ==0 ) ) then
q(r,c)=0 ;
end ;
if ( ( rn ==0)& (cn >0 ) ) then
q(r,c)=0 ;
end;
if ( ( cn ==0)& (rn >0 ) ) then
q(r,c)=0 ;
end;
if ( ( (rn *cn) > 0) & (rn==cn) ) then
q(r,c) = sig2 - sin(rn*sig2)/rn - sin(cn*sig2)/cn ...
+ ( sin( (rn+cn)*sig2) /(rn+cn) + sig2
)/2;
q(r,c)=q(r,c) - ( sig1 - sin(rn*sig1)/rn - sin(cn*sig1)/cn ...
+ ( sin( (rn+cn)*sig1) /(rn+cn) + sig1
)/2 ) ;
end ;
if (
(rn*cn)>0 & ( abs(rn -cn) >0)
) then
q(r,c)= sig2 - sin(rn*sig2)/rn -sin(cn*sig2 )/cn ;
q(r,c)=q(r,c)+ ( sin( (rn+cn)*sig2) /(rn+cn) + sin( (rn-cn)*sig2) /(rn-cn)
)/2 ;
q(r,c)=q(r,c) - (sig1 - sin(rn*sig1)/rn - sin(cn*sig1)/cn ) ;
q(r,c)=q(r,c) - ( sin( (rn+cn)*sig1) /(rn+cn) + sin( (rn-cn)*sig1) /(rn-cn)

)/2 ;

Next the htot matrix is used to obtain the inverse hin and the max eigenvector of hin is the min eigenvector of htot .
5. Scilab 5.3.3 code for Eigen Type 1 FIR filter
// Eigenfilter ver 5 N-1 =even and m=( N-1)/2
N= 31 // N=odd only is considered
p=[] //the stop band matrix
q=[] // the pass band matrix
n1=(N-1)/2 +1 // modified M
// set sigma values passband sig1 sig2 in radians
// stop band sig3 sig4
sig3 =.5 *%pi // stop band
sig4 = 1*%pi // stop band
sig1 = 0*%pi // passband
sig2 =.3*%pi // pass band
p=zeros(n1,n1); // building the integrated p matrix
for r=1:n1
rn =r-1 ;
for c=1:n1
cn = c-1 ;
//P matrix stop band use sig4=pi sig3 less
if ( (rn==0)&(cn==0) ) then
p(r,c) =sig4 -sig3 ;

end ;
if ( (rn==0)&(cn>0) ) then
p(r,c) = sin(sig4 *cn)/cn
- sin(sig3 *cn)/cn ;
end ;
if ( (rn> 0)&(cn==0) ) then
p(r,c) = sin(sig4 *rn)/rn
- sin(sig3 *rn)/rn ;
end ;
if ( (rn*cn> 0) & ( rn==cn) ) then
p(r,c) = 1/2* ( sig4 -sig3 + sin(sig4 *(rn +cn ) )/ (rn+cn) - sin(sig3 *(rn +cn ) )/ (rn+cn)
end ;
if ( (rn*cn> 0) & ( abs(rn-cn) >0 ) ) then
p(r,c) = 1/2* ( sin(sig4 *(rn +cn ) )/ (rn+cn) - sin(sig3 *(rn +cn ) )/ (rn+cn) ) ;
p(r,c) = p(r,c) + 1/2* ( sin(sig4 *(rn -cn ) )/ (rn-cn) - sin(sig3 *(rn -cn ) )/ (rn-cn) ) ;
end ;
p(r,c) =p(r,c)/%pi ;
end;
end ;
//Q matrix passband use sig2=.3 >sig1=0
for r=1:n1
rn =r-1 ;
for c=1:n1
cn = c-1 ;
if ( ( rn ==0)& (cn ==0 ) ) then
q(r,c)=0 ;
end ;
if ( ( rn ==0)& (cn >0 ) ) then
q(r,c)=0 ;
end;
if ( ( cn ==0)& (rn >0 ) ) then
q(r,c)=0 ;
end;
if ( ( (rn *cn) > 0) & (rn==cn) ) then
q(r,c) = sig2 - sin(rn*sig2)/rn - sin(cn*sig2)/cn ...
+ ( sin( (rn+cn)*sig2) /(rn+cn) + sig2
)/2;
q(r,c)=q(r,c) - ( sig1 - sin(rn*sig1)/rn - sin(cn*sig1)/cn ...
+ ( sin( (rn+cn)*sig1) /(rn+cn) + sig1
)/2 ) ;
end ;
if (
(rn*cn)>0 & ( abs(rn -cn) >0)
) then
q(r,c)= sig2 - sin(rn*sig2)/rn -sin(cn*sig2 )/cn ;
q(r,c)=q(r,c)+ ( sin( (rn+cn)*sig2) /(rn+cn) + sin( (rn-cn)*sig2) /(rn-cn)
)/2 ;
q(r,c)=q(r,c) - (sig1 - sin(rn*sig1)/rn - sin(cn*sig1)/cn ) ;
q(r,c)=q(r,c) - ( sin( (rn+cn)*sig1) /(rn+cn) + sin( (rn-cn)*sig1) /(rn-cn)
end
q(r,c)= q(r,c)/%pi ;
end;
end ;

// compute composite htot = alp*P+ (1-alp)*Q eigen values and vectors.

alp=.5
htot = alp*p+(1-alp)*q;
// finding the min eigen value vector
hin = htot^-1 ; // invert htot and we seek max of hin
nit=100 // max iterations
v2= ones(1, n1) ;
v2=v2';
smx = sqrt( v2'*v2) ;
v2=v2/smx;
lamo=0 ;
for i=1:nit
v3= hin*v2 ;
lamn= sqrt ( ( v3'*v3 ) / (v2'*v2 ) ) ;
rer= abs(lamo- lamn ) ;
if ( rer< .001) then break end;

)/2 ;

);

lamo=lamn ;
v2= v3/sqrt( v3'*v3 ) ;
end
h s eq a = v2 ; // thi s i s the fiter w e s e e k
npt =100 // number of plot points
dw= %pi/npt ;
omg= zeros(1:npt);
for k=1:npt
xm(k)= 0;
omg(k)=omg(k)+(k-1)* dw;
for i=1:n1
xm(k) = hseqa(i)*cos(omg(k)*(i-1) ) +xm(k) ;
end
end
xm =real(xm) ;
plot(omg, xm); // the magn vs w response
figure(1);
mx= max(xm)
xml = 20*log10( abs(xm/mx)); // in db
oma=omg/2/%pi ;
plot(oma, xml); // db magn vs per unit freq
// lamn =1836256.4
//1/lamn =0.0000005
//hseqa =
0.5197220
0.7810746
0.2383787
- 0.1455732
- 0.1698201
- 0.0035968
0.0906716
0.0448063
- 0.0291368
- 0.0377701
- 0.0018667
0.0180220
0.0086882
- 0.0038613
- 0.0045153
- 0.0004323
//figure 0

figure1

// using

scilab 5.3.3 spec function

[evseqr ,diagevalsr]=spec(htot) ;
diag(diagevalsr ) ; // print all eigen values
// ans =
2.308924
0.3732253
0.0508677
0.0082529
0.0000005
0.0001573
0.1889106
0.2370807
0.2496339
0.2499695
0.2499997
0.2500000
0.25
0.25
0.25
0.25
h s eq a = r e al ( ev s eqr(:, 5) ) // printout min eig en value ve ctor ie 5 th ve ctor in our e x a mpl e

hseqa =
- 0.5197220
- 0.7810746
- 0.2383787
0.1455732
0.1698201
0.0035968
- 0.0906716
- 0.0448063
0.0291368
0.0377701
0.0018667
- 0.0180220
- 0.0086882
0.0038613
0.0045153
0.0004323
// notice hseqa is negative of previous result
// so set hseqa= -hseqa
hseqa= -(hseqa) ; // change to real type
//plot using rsult of spec function
npt =100
dw= %pi/npt ;
omg= zeros(1:npt);
for k=1:npt

xm(k)= 0;
omg(k)=omg(k)+(k-1)* dw;
for i=1:n1
xm(k) = hseqa(i)*cos(omg(k)*(i-1) ) +xm(k) ;
end
end
xm =real(xm) ;
figure(2);
plot(omg, xm); // plot the magn vs freq
figure(3);
mx= max(xm)
xml = 20*log10( abs(xm/mx));
oma=omg/2/%pi ;
plot(oma, xml);

figure(2) // db magn vs per unit freq

figure(3)

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Você também pode gostar