home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Hacker Chronicles 2
/
HACKER2.BIN
/
137.CREATEWA.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1988-06-15
|
10KB
|
327 lines
UNIT CreateWaveform;
INTERFACE
USES
{$IFDEF DOSCrt}
DOSCrt,
{$ELSE}
Crt,
{$ENDIF}
Extended_Reals,
TextOps,
Global,
GraphWaveform;
PROCEDURE AnalyticWaveform;
(****************************************************************************)
IMPLEMENTATION
CONST
{$IFOPT N-}
TestValue = 85;
{$ELSE}
TestValue = 10000;
{$ENDIF}
VAR
ch : CHAR;
PROCEDURE AnalyticMenu;
BEGIN {AnalyticMenu}
ClrScr;
WriteXY ('Create Waveform Menu',StartColumn+8,3);
WriteXY ('Select waveform to create by typing a number.',
StartColumn,5);
WriteXY ('1. Damped Sine Wave ',StartColumn+8,8);
WriteXY ('2. EMP Double Exponential ',StartColumn+8,10);
WriteXY ('3. Reciprocal Double Exponential',StartColumn+8,12);
WriteXY ('4. General Exponential Sum',StartColumn+8,14);
WriteXY ('9. Exit to Main Menu ',StartColumn+8,16);
WriteXY ('Your Choice? ',StartColumn+8,24);
REPEAT
ch:=ReadKey;
UNTIL (ch IN ['1'..'4','9']);
IF ch <> '9' THEN BEGIN
NumPoints:=TNArraySize;
NumFreqs :=TNArraySize div 2;
END; {IF}
END; {AnalyticMenu}
{----------------------------------------------------------------------------}
PROCEDURE MakeDampSine;
VAR
i : INTEGER;
t0 : REAL;
A : REAL;
alpha : REAL;
t : REAL;
beta : REAL;
cycles : REAL;
phi : REAL;
dummy : string;
first : REAL;
BEGIN {MakeDampSine}
WriteXY ('Damped Sine Wave',StartColumn+8,2);
WriteXY ('This wave is of the form ',1,5);
Write ('A · exp(-α(t-t0)) · sin(2πf(t-t0) + φ).');
REPEAT
WriteXY ('Period of wave = ',1,8);
GotoXY (18,8); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,beta,dummy);
UNTIL (beta <> 0);
REPEAT
WriteXY ('Number of cycles = ',1,10);
GotoXY (20,10); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,cycles,dummy);
UNTIL (cycles <> 0);
WriteXY ('Time delay, t0 = ',1,12);
GotoXY (18,12); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,t0,dummy);
REPEAT
WriteXY ('A = ',1,14);
GotoXY (5,14); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,A,dummy);
UNTIL (A <> 0);
WriteXY ('α = ',1,16); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,alpha,dummy);
WriteXY ('φ (in degrees) = ',1,18); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,phi,dummy);
phi:=phi*pi/180;
FOR i:=0 TO NumPoints-1 DO BEGIN
t:=beta*i*cycles/(NumPoints-1)+t0;
time^[i]:=t;
IF (alpha*(t-t0) > TestValue)
THEN first:=0
ELSE first:=EXP(-alpha*(t-t0));
ampl^[i]:=A*first*SIN(2*PI*(t-t0)/beta+phi);
END; {FOR}
END; {MakeDampSine}
{----------------------------------------------------------------------------}
PROCEDURE MakeDoubleExp;
VAR
ch : CHAR;
i : INTEGER;
t0 : REAL;
A : REAL;
alpha : REAL;
t : REAL;
beta : REAL;
timeofmax : REAL;
maxtime : REAL;
dummy : string;
first : REAL;
second : REAL;
BEGIN {MakeDoubleExp}
WriteXY ('EMP Double Exponential',StartColumn+8,2);
WriteXY ('This wave is of the form ',1,5);
Write ('A · [exp(-α·t) - exp(-ß·t)].');
REPEAT
WriteXY ('Maximum amplitude = ',1,8); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,A,dummy);
UNTIL (A <> 0);
REPEAT
WriteXY ('risetime = ',1,10);
GotoXY (12,10); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,beta,dummy);
UNTIL (beta > 0);
REPEAT
WriteXY ('falltime = ',1,12);
GotoXY (12,12); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,alpha,dummy);
UNTIL (alpha > 0);
maxtime:=3*alpha;
alpha:=LN(10)/alpha;
beta :=LN(9) /beta;
timeofmax:=LN(beta/alpha)/(beta-alpha);
A:=A/(EXP(-alpha*timeofmax)-EXP(-beta*timeofmax));
FOR i:= 0 TO NumPoints-1 DO BEGIN
t:=maxtime*i/(NumPoints-1);
time^[i]:=t;
IF alpha*t > TestValue
THEN first:=0
ELSE first:=EXP(-alpha*t);
IF beta*t > TestValue
THEN second:=0
ELSE second:=EXP(-beta*t);
ampl^[i]:=A*(first-second);
END; {FOR}
END; {MakeDoubleExp}
{----------------------------------------------------------------------------}
PROCEDURE MakeRecipDoubleExp;
VAR
i : INTEGER;
A : REAL;
alpha : REAL;
b : REAL;
t : REAL;
beta : REAL;
t0 : REAL;
dummy : string;
BEGIN {MakeRecipDoubleExp}
WriteXY ('Reciprocal Double Exponential',StartColumn+8,2);
WriteXY ('This wave is of the form ',1,5);
Write ('A / {exp[α·(t-t0)] - exp[ß·(t-t0)]}.');
WriteXY ('Initial time = ',1,8); ReadLn (dummy);
String_to_Value (dummy,t0,dummy);
WriteXY ('Maximum time = ',1,10); ReadLn (dummy);
String_to_Value (dummy,beta,dummy);
WriteXY ('A = ',1,12); ReadLn (dummy);
String_to_Value (dummy,A,dummy);
WriteXY ('α = ',1,14); ReadLn (dummy);
String_to_Value (dummy,alpha,dummy);
WriteXY ('ß = ',1,16); ReadLn (dummy);
String_to_Value (dummy,b,dummy);
FOR i:=0 TO NumPoints-1 DO BEGIN
t:=beta*i/(NumPoints-1);
time^[i]:=t;
IF ((alpha*(t-t0) > TestValue) OR (b*(t-t0) > TestValue))
THEN ampl^[i]:=0
ELSE ampl^[i]:=A/(EXP(alpha*(t-t0))-EXP(b*(t-t0)));
END; {FOR}
END; {MakeRecipDoubleExp}
{----------------------------------------------------------------------------}
PROCEDURE MakeGeneralSum;
VAR
i : INTEGER;
j : INTEGER;
A : REAL;
alpha : REAL;
t : REAL;
t0 : REAL;
beta : REAL;
NumTerms : INTEGER;
dummy : string;
BEGIN {MakeGeneralSum}
WriteXY ('General Exponential Sum',StartColumn+8,2);
WriteXY ('This wave is of the form ',1,5);
Write ('SUM A·exp(α·(t-t0)).');
REPEAT
WriteXY ('Maximum time = ',1,8);
GotoXY (16,8); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,beta,dummy);
UNTIL (beta > 0);
WriteXY ('Time delay, t0 = ',1,10);
GotoXY (18,10); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,t0,dummy);
FOR i:=0 TO NumPoints-1 DO BEGIN
t:=beta*i/(NumPoints-1)+t0;
time^[i]:=t;
ampl^[i]:=0;
END; {FOR}
WriteXY ('Number of exponential terms: ',1,12);
GotoXY (30,12); ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,t,dummy);
NumTerms:=round(t);
ORIG:=FALSE;
FOR i:=1 TO NumTerms DO BEGIN
GotoXY (1,16); ClrEOL;
GotoXY (1,18); ClrEOL;
GotoXY (1,14); WriteLn ('Exponential term #',i);
WriteXY ('A = ',5,16); ClrEOL; ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,A,dummy);
IF NOT ORIG THEN IF A <> 0 THEN ORIG:=TRUE;
WriteXY ('α = ',5,18); ClrEOL; ReadLn (dummy);
IF dummy = '' THEN dummy:='0';
String_to_Value (dummy,alpha,dummy);
FOR j:=0 TO NumPoints-1 DO IF (alpha*(time^[j]-t0) > TestValue)
THEN ampl^[j]:=A*EXP(TestValue) + ampl^[j]
ELSE ampl^[j]:=A*EXP(alpha*(time^[j]-t0)) + ampl^[j];
END; {FOR}
END; {MakeGeneralSum}
{----------------------------------------------------------------------------}
PROCEDURE AddNoise;
VAR
ch : CHAR; (* Add noise? *)
dummy : string; (* Scale value of noise *)
Scale : REAL; (* Scale value of noise *)
max_r : REAL; (* Max value of waveform *)
max_i : INTEGER; (* Index of max value of waveform *)
i : INTEGER; (* Counter variable *)
noise : REAL;
BEGIN {AddNoise}
WriteXY ('Add noise to waveform (y/n)? ',1,20);
REPEAT
ch:=UpCase(ReadKey);
UNTIL (ch IN ['Y','N',ENTER]);
IF (ch = ENTER)
THEN Write ('N')
ELSE Write (ch);
IF (ch = 'Y') THEN BEGIN
WriteXY ('Signal-to-Noise ratio (dB) = ',1,22); ReadLn (dummy);
IF dummy = ''
THEN Scale:=1000
ELSE String_to_Value (dummy,Scale,dummy);
max_i:=0;
FOR i:=1 TO NumPoints-1 DO
IF ampl^[i] > ampl^[max_i] THEN max_i:=i;
max_r:=ampl^[max_i];
Scale:=max_r*EXP((-Scale/10)*LN(10)); {converts dB to real #}
Randomize;
FOR i:=0 TO NumPoints-1 DO BEGIN
noise:=Scale*random;
ampl^[i]:=ampl^[i]+noise;
END; {FOR}
END; {IF}
END; {AddNoise}
{----------------------------------------------------------------------------}
PROCEDURE AnalyticWaveform;
BEGIN {AnalyticWaveform}
AnalyticMenu;
ClrScr;
CASE ch OF
'1': MakeDampSine;
'2': MakeDoubleExp;
'3': MakeRecipDoubleExp;
'4': MakeGeneralSum;
'9': Exit;
END; {CASE}
AddNoise;
TRANS:=FALSE;
ACCEPT:=TRUE;
ORIG:=TRUE;
GraphResults ('T');
END; {AnalyticWaveform}
(****************************************************************************)
BEGIN {Initialization}
END. {UNIT CreateWaveform}