File: frfit.sci

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (211 lines) | stat: -rw-r--r-- 4,960 bytes parent folder | download | duplicates (2)
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
function [num,den]=frfit(w,fresp,order,weight)
//Calling sequence:
//[num,den]=frfit(w,fresp,r,weight)
//sys=frfit(w,fresp,r,weight)
//
//  w: vector of frequencies in Hz
//  fresp: vector of frequency responses at these frequencies.   
//  weight: vector of weights given to each point
//
//  Fits frequency response data points by a bi-stable transfer 
//  function
//                G(s) = num(s)/den(s)
//  of order r.
//  freq(num,den,%i*w) should be close to fresp
//
// changing frequencies to rad/s 
// Copyright INRIA
w=2*%pi*w;
[LHS,RHS]=argn(0);
if RHS==3 
weight=ones(w);
end
w=w(:);fresp=fresp(:);weight=weight(:);

Mean=sum(fresp)/size(fresp,'*');
if max(abs(fresp-Mean)) < .1*max(abs(fresp))
   num=real(Mean); den=1; return
end

order1=order+1;npts=length(w);wmed=1;

if RHS < 4 then
weight=ones(npts,1); 
end

M0=zeros(order+1,1); M1=M0;
M0(1)=1; M1(2)=1; M=[M0 M1];
for i=1:order-1,
   Mt=2*[0;M1(1:order)]-M0;
   M=[M Mt];
   M0=M1; M1=Mt;
end

k=2;km=1;
sl0=round((log10(abs(fresp(k)))-log10(abs(fresp(km))))/(log10(w(k))-log10(w(km))));
k=npts;km=k-1;
slinf=round((log10(abs(fresp(k)))-log10(abs(fresp(km))))/(log10(w(k))-log10(w(km))));

if slinf>0&slinf<20
  w=[w;[10;15]*w(npts)];
  fresp=[fresp;[1;1]*10^slinf*abs(fresp(npts))];
  weight=[weight;1;1];
  npts=npts+2;
  slinf=0;
end


if sl0>0 
mindeg=max(abs(sl0),-slinf+abs(sl0)); 
         else
mindeg=max(abs(sl0),-slinf);
end

if mindeg > order
  warning('Filter order too small');
  sl0=sign(sl0)*min(order,sl0);
  if sl0>0 then
    slinf=-(order-abs(sl0));
  else
  end
  slinf=-order;
end


jw=%i*w;mag=abs(fresp);t0=ones(npts,1); t1=jw;A=[ones(npts,1),jw];

for i=1:order-1,
   t=2*jw.*t1-t0;
   A=[A,t];
   t0=t1; t1=t;
end

Aom=A;

//A=[A -diag(fresp)*A];AA=A;om0=w;
A=[A, -fresp*ones(1,size(A,2)).*A];AA=A;om0=w;

Acons=[]; ycons=[];
if sl0<=0         
  Acons=[Acons; zeros(-sl0,order1) M(1:-sl0,:);...
                M(1,:) -(mag(1)*w(1)^(-sl0))*M(1-sl0,:)];
  ycons=[ycons; zeros(-sl0+1,1)];
elseif sl0>0      
  Acons=[Acons; M(1:sl0,:) zeros(sl0,order1);...
                M(1+sl0,:)*w(1)^sl0 -mag(1)*M(1,:)];
  ycons=[ycons; zeros(sl0+1,1)];
end

Acons=[Acons;zeros(1,order1) M(order1,:)];ycons=[ycons;1];

if slinf<=0
  Acons=[Acons; M(order1+slinf:order1,:) zeros(-slinf+1,order1)];
  ycons=[ycons; mag(npts)*w(npts)^(-slinf); zeros(-slinf,1)];
end

[nc,nv]=size(Acons);

if nc >= nv
  x=Acons\ycons;
else        
  indin=1:nv; indout=[]; 
  for i=1:nc,
     if i<nc,
        [m,ix]=max(abs(Acons(i:nc,i:nv)));ix=ix(2);
     else
        [m,ix]=max(abs(Acons(i:nc,i:nv)));
     end
     Acons(:,i:nv)=[Acons(:,i+ix-1) Acons(:,[i:i+ix-2,i+ix:nv])]; 
     indout=[indout,indin(ix)];indin=indin([1:ix-1,ix+1:nv-i+1]); 

     x=Acons(i:nc,i); Atmp=Acons(i:nc,i+1:nv); ytmp=ycons(i:nc);
     if x(1)>=0
             sgn=1; 
     else 
            sgn=-1; 
     end
     aux=sgn*sqrt(x'*x);x(1)=x(1)+aux;nx2=.5*x'*x;
     Atmp=[[-aux;zeros(nc-i,1)] Atmp-x*((x'*Atmp)/nx2)];
     ytmp=ytmp-x*((x'*ytmp)/nx2);  
     Acons(i:nc,i:nv)=Atmp;ycons(i:nc)=ytmp;
  end
  perm=[indout indin];
  Ac1=Acons(:,1:nc); Ac2=Acons(:,nc+1:nv);
  A=A(1:npts,:);
  A=[real(A);imag(A)];
  A=A(:,perm);     
  A1=A(:,1:nc); A2=A(:,nc+1:nv);
  A=A2-A1*(Ac1\Ac2);  
  y=-A1*(Ac1\ycons);

  fweight=ones(npts,1); 
  ind=find(w > 10);

  fweight(ind)=((1) ./(w(ind)^order));
  ind=find(w < .01);

  fweight(ind)=(1./(w(ind)^min(0,sl0)))';
  fweight=weight.*fweight;
  //Wt=diag([fweight;fweight]);
  //x=pinv(Wt*A)*(Wt*y);
  //x=pinv([fweight;fweight]*ones(1,size(A,2)).*A)*(vvv.*y);
  vvv=[fweight;fweight];
  x=(vvv*ones(1,size(A,2)).*A)\(vvv.*y);
  x=[Ac1\(ycons-Ac2*x);x];
  [s,perm]=sort(-perm);s=-s;
  x=x(perm);              

  nresp=Aom*x(1:order1); dresp=Aom*x(order1+1:2*order1); 
  relerr=abs(abs(nresp./dresp)./mag-1)';
  for kk=1:prod(size(relerr));relerr(kk)=min(1,relerr(kk));end
  relerr=relerr(:);
  ind=find(mag < .01); relerr(ind)=relerr(ind)*.3;
  ind=find(relerr>.5);
  if ind<>[] 
  fweight(ind)=( exp(relerr(ind)*log(10)) ).*fweight(ind);
  end
  //Wt=diag([fweight;fweight]);
  //x=pinv(Wt*A)*(Wt*y);
  //x=pinv([fweight;fweight]*ones(1,size(A,2)).*A)*(vvv.*y);
  x=([fweight;fweight]*ones(1,size(A,2)).*A)\(vvv.*y);
  x=[Ac1\(ycons-Ac2*x);x];x=x(perm);                 
end

a=x(1:order1); b=x(order1+1:2*order1);
num=fliplr((M*a)');den=fliplr((M*b)');

err=abs(AA*x);
num=num(-slinf+1:order1);

junk=poly(fliplr(num),'s','c')
rn=roots(junk); rn=-abs(real(rn))+%i*imag(rn);
l=length(rn);
for k=1:l
     if real(rn(k))>-1e-3 
       rn(k)=-1.e-3+%i*imag(rn(k))
     end
end

junk=poly(fliplr(den),'s','c');
dn=roots(junk); dn=-abs(real(dn))+%i*imag(dn);
l=length(dn);
for k=1:l
     if real(dn(k))>-1e-3 
       dn(k)=-1.e-3+%i*imag(dn(k))
     end
end

polyrn=poly(fliplr(rn),'s');
polydn=poly(fliplr(dn),'s');
num=real(num(1)*polyrn); 
den=real(den(1)*polydn);
if LHS==1
 num=syslin('c',num/den)
end

function m=fliplr(m)
//Utility fct
[p,q]=size(m);
m=m(:,q:-1:1);