home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2001 December
/
Chip_2001-12_cd1.bin
/
zkuste
/
delphi
/
kolekce
/
d56
/
DM2KVCL.ZIP
/
COMMON.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
2000-11-05
|
11KB
|
275 lines
{****************************************************************************}
{ Data Master 2000 }
{****************************************************************************}
unit Common;
{$B-}
interface
uses SysUtils;
const
MaxCols=26; {max. number of values in string (= number of letters!)}
CRLF=#13#10; LFCR=#10#13; CR=#13; LF=#10; {string terminators}
type
TColIndex=1..MaxCols; {trealarray index type}
TReal=extended; {basic realtype}
TComplex=record {complex value type}
X,Y: TReal;
end;
TRealPoint=TComplex;
PRealArray=^TRealArray; {string of TReal}
TRealArray=array [TColIndex] of TReal;
TFormat=packed record {numeric format}
Width, Decimals: byte;
FType: TFloatFormat;
end;
TFormatArray=array [TColIndex] of TFormat;
PFormatArray=^TFormatArray;
{additional string processing}
function Str2Real(S: string; var R: TRealArray): byte; {string => realarray}
function NumWords(S: string): integer; {returns number of words in string}
function WordStr(S: string; N: integer): string; {returns N-th word or ''}
function FindParameter(P: string): boolean;{scan CMD line for given parameter}
{complex numbers support:}
function StrC(Z: TComplex): string; {returns s=x+iy}
function ModuleC(Z: TComplex): TReal; {returns |Z|}
function ArgC(Z: TComplex): TReal; {returns arg(Z)}
function MakeC(X,Y: TReal): TComplex; {returns X+iY}
function CreateC(X,Y: TReal): TComplex; {returns Xexp(iY)}
function OneC: TComplex; {returns 1+i0}
function AddC(X,Y: TComplex): TComplex; {returns X+Y}
function SubC(X,Y: TComplex): TComplex; {returns X-Y}
function MulC(X,Y: TComplex): TComplex; {returns X*Y}
function DivC(X,Y: TComplex): TComplex; {returns X/Y}
function ConjC(X: TComplex): TComplex; {returns conjugate X}
function SinC(X: TComplex): TComplex; {returns sin(X)}
function CosC(X: TComplex): TComplex; {returns cos(X)}
function TanC(X: TComplex): TComplex; {returns tan(X)}
function ExpC(X: TComplex): TComplex; {returns exp(X)}
function LnC(X: TComplex): TComplex; {returns Ln(X)}
function SqrC(X: TComplex): TComplex; {returns Sqr(X)}
function SqrtC(X: TComplex): TComplex; {returns sqrt(X)}
function ShC(X: TComplex): TComplex; {returns sh(X)}
function ChC(X: TComplex): TComplex; {returns ch(X)}
function ThC(X: TComplex): TComplex; {returns th(X)}
function InvC(X: TComplex): TComplex; {returns 1/X}
{special functions}
function LineInterpolate(X1, X2, Y1, Y2, X: TReal): TReal; {linear interpol.}
function GCompensation(M,Fi,X,Y: TReal): TComplex; {Gx,Gy->Zx,Zy}
function OSCompensation(ZoX,ZoY,ZsX,ZsY,ZX,ZY: TReal): TComplex; {open/short}
function VDP(Ra,Rb: TReal): TReal; {return VDP formfactor}
implementation
{additional string processing}
function Str2Real(S: string; var R: TRealArray): byte; {converts string to}
var Ss: string; I,J: byte; Flag: integer; Rr: TReal; {TRealArray. returns}
begin {size of array}
I:=1; J:=0; {numbers may be delimited by spaces, tabstops and commas}
while(I<=length(S)) and (J<MaxCols) do
begin
if (S[I]=' ') or (S[I]=#9) or (S[I]=',') then Inc(I)
else
begin
SS:='';
while (I<=length(S)) and (S[I]<>#9) and (S[I]<>',') and (S[I]<>' ') do
begin Ss:=Ss+S[I]; Inc(I); end;
Val(Ss, Rr, Flag); Str2Real:=J; if Flag<>0 then Exit;
Inc(J); R[J]:=Rr;
end;
end;
Str2Real:=J;
end;
function NumWords(S: string): integer; {returns number of words in string}
var I, J: integer;
begin
I:=1; J:=0; {I-position in string, J-in word}
while I<=length(S) do
begin
case S[I] of {all chars framed by | symbols treated as single word!!}
' ', #9: Inc(I); {omit separators}
'|' :begin
Inc(I);
while (I<=length(S)) and (S[I]<>'|') do Inc(I);
Inc(J); Inc(I);
end;
else
begin
while (I<=length(S)) and (S[I]<>' ') and (S[I]<>#9) do Inc(I);
Inc(J);
end
end; {case}
end;
NumWords:=J;
end;
function WordStr(S: string; N: integer): string; {returns N-th word or ''}
var I, J: integer; Ss: string;
begin
I:=1; J:=0; SS:=''; {I-position in string, J-in word}
while I<=length(S) do
begin
SS:='';
case S[I] of
' ', #9: Inc(I);
'|' : begin
Inc(I); {I to 1-st symbol of wordstring}
if I>length(S) then Break; {if it's last symbol}
SS:='';
while (I<=length(S)) and (S[I]<>'|') do
begin Ss:=Ss+S[I]; Inc(I); end;
Inc(J); Inc(I); if J=N then Break;
end;
else
begin {in Ss-accumulate word}
while (I<=length(S)) and (S[I]<>' ') and (S[I]<>#9) do
begin Ss:=Ss+S[I]; Inc(I); end;
Inc(J); if J=N then Break;
end
end; {case}
end;
if J=N then WordStr:=SS else WordStr:='';
end;
function FindParameter(P: string): boolean; {returns true if found}
var I: byte; S: string;
begin
P:=UpperCase(P); Result:=false; {NOTE: case-insensitive}
for I:=1 to ParamCount do
begin
S:=UpperCase(ParamStr(I));
if ((S[1]='/') or (S[1]='-')) and (Copy(S,2,length(S)-1)=P)
then begin Result:=true; Exit; end; {found!!! This all work}
end;
end;
{--- complex: ---}
function StrC(Z: TComplex): string; {returns s=x+iy}
var S, Ss: string;
begin Str(Z.X, S); Str(Z.Y, Ss); StrC:=S+'+i'+Ss; end;
function ModuleC(Z: TComplex): TReal; {returns |Z|}
begin ModuleC:=Sqrt(Sqr(Z.X)+Sqr(Z.Y)); end;
function ArgC(Z: TComplex): TReal; {returns arg(Z)}
begin
if Z.X<>0 then ArgC:=Arctan(Z.Y/Z.X) else {else-value is imaginary!}
begin
if Z.Y<0 then ArgC:=-Pi/2;
if Z.Y>0 then ArgC:=Pi/2; if Z.Y=0 then ArgC:=0;
end;
end;
function MakeC(X,Y: TReal): TComplex; {Z:=X+iY}
begin Result.X:=X; Result.Y:=Y; end;
function CreateC(X,Y: TReal): TComplex; {Z:=Xexp(iY)}
begin Result.X:=X*cos(Y); Result.Y:=X*sin(Y); end;
function OneC: TComplex; {returns 1+i0}
begin Result.X:=1; Result.Y:=0; end;
function AddC(X,Y: TComplex): TComplex; {Z:=X+Y}
begin Result.X:=X.X+Y.X; Result.Y:=X.Y+Y.Y; end;
function SubC(X,Y: TComplex): TComplex; {Z:=X-Y}
begin Result.X:=X.X-Y.X; Result.Y:=X.Y-Y.Y; end;
function MulC(X,Y: TComplex): TComplex; {Z:=X*Y}
begin Result.X:=X.X*Y.X-X.Y*Y.Y; Result.Y:=X.X*Y.Y+X.Y*Y.X; end;
function DivC(X,Y: TComplex): TComplex; {Z:=X/Y}
begin {Z1/Z2=Z1*_Z2/|Z2|^2}
{Result:=DivC(MulC(X,ConjC(Y)),MakeC(Sqr(Module(Y)),0)); may cause stack ovf!}
Result.X:=(X.X*Y.X+X.Y*Y.Y)/(sqr(Y.X)+sqr(Y.Y));
Result.Y:=(Y.X*X.Y-X.X*Y.Y)/(sqr(Y.X)+sqr(Y.Y));
end;
function ConjC(X: TComplex): TComplex; {Y:=conjugate X}
begin Result.X:=X.X; Result.Y:=-X.Y; end;
function SinC(X: TComplex): TComplex; {Y:=sin(X)}
begin
Result.X:=sin(X.X)*(exp(X.Y)+exp(-X.Y))/2;
Result.Y:=cos(X.X)*(exp(X.Y)-exp(-X.Y))/2;
end;
function CosC(X: TComplex): TComplex; {Y:=cos(X)}
begin
Result.X:=cos(X.X)*(exp(X.Y)+exp(-X.Y))/2;
Result.Y:=sin(X.X)*(exp(-X.Y)-exp(X.Y))/2;
end;
function TanC(X: TComplex): TComplex; {Y:=tan(X)}
begin Result:=DivC(SinC(X), CosC(X)); end;
function ExpC(X: TComplex): TComplex; {Y:=exp(X)}
begin Result.X:=exp(X.X)*cos(X.Y); Result.Y:=exp(X.X)*sin(X.Y); end;
function LnC(X: TComplex): TComplex; {Y:=Ln(X)}
begin Result.X:=ln(Abs(ModuleC(X))); Result.Y:=ArgC(X); end;
function SqrC(X: TComplex): TComplex; {Y:=Sqr(X)}
begin Result:=MulC(X,X); end;
function SqrtC(X: TComplex): TComplex; {Y:=sqrt(X)}
begin Result:=CreateC(Sqrt(ModuleC(X)), ArgC(X)/2); end;
function ShC(X: TComplex): TComplex; {Y:=sh(X)}
begin
Result:=MulC(MakeC(0,-1),SinC(MulC(X,MakeC(0,1)))); {sh(X)=-i*sin(iX)}
end;
function ChC(X: TComplex): TComplex; {Y:=ch(X)}
begin Result:=CosC(MulC(X,MakeC(0,1))); end; {ch(X)=cos(iX)}
function ThC(X: TComplex): TComplex; {Y:=th(X)}
begin Result:=DivC(ShC(X),ChC(X)); end;
function InvC(X: TComplex): TComplex; {Y:=1/X}
begin Result:=DivC(OneC,X); end;
{from PROCESS.PAS:}
{Interpolation, VDP and cable compensation routines: Gamma and Open/Short}
function LineInterpolate(X1, X2, Y1, Y2, X: TReal): TReal;
begin {linear interpolation: Y:=Yk+(X-Xk)/(Xk+1-Xk)*(Yk+1-Yk)}
if X1=X2 then LineInterpolate:=Y1+Y2/2 {NOTE! process zero interval}
else LineInterpolate:=Y1+(X-X1)/(X2-X1)*(Y2-Y1);
end;
function VDP(Ra,Rb: TReal): TReal; {return VDP formfactor}
procedure Proc(r, y0: TReal; var d, f1:TReal);
var v, rv, a: TReal;
begin
rv:=r*y0; v:=y0; v:=exp(v); rv:=exp(rv); a:=rv+1/rv-v;
d:=r*(rv-1/rv)-v; f1:=a;
end;
var r, y0, y1, d, f0, f1: TReal;
begin
if ((Ra=0) or (Rb=0)) then begin Result:=1; Exit; end; {superconducor: f=1}
r:=(Ra-Rb)/(Ra+Rb); if r<0 then r:=-r; {<-- no need do ABS!!!} y0:=1;
Proc(r, y0, d, f0);
repeat
y1:=y0-f0/d; Proc(r, y1, d, f1); y0:=y1; f0:=f1;
until (abs(f1)<1.e-6);
Result:=y1;
end;
function GCompensation(M,Fi,X,Y: TReal): TComplex; {Gx,Gy->Zx,Zy}
var G,Z: TComplex; {Gtrue=G*Gcable}
begin {Z/Zi=(1+G)/(1-G)}
G:=MulC(CreateC(M,Fi),MakeC(X,Y)); Z:=DivC(AddC(OneC, G), SubC(OneC, G));
Z.X:=50*Z.X; Z.Y:=50*Z.Y; Result:=Z;
end;
function OSCompensation(ZoX,ZoY,ZsX,ZsY,ZX,ZY: TReal): TComplex;
begin {O/S comp. routine: Ztrue=(Zs-Z)/(Z/Zo-1)}
Result:=DivC(SubC(MakeC(ZsX, ZsY), MakeC(ZX, ZY)),
SubC(DivC(MakeC(ZX, ZY),MakeC(ZoX, ZoY)), OneC));
end;
end.