LarkSineTableImpl.mesa
Last modified:
Stewart, December 22, 1983 3:11 pm
DIRECTORY
Commander,
FS,
IO,
Real,
RealFns,
Rope,
U255;
LarkSineTableImpl: PROGRAM IMPORTS Commander, FS, IO, Real, RealFns, U255 = {
OPEN U255;
Main: Commander.CommandProc = TRUSTED {
ENABLE ABORTED => GOTO Quit;
out: IO.STREAM;
name: Rope.ROPE;
label: Rope.ROPE;
res: BOOL;
l: NAT;
loss: REAL;
out ← cmd.out;
out.PutF["\nLarkSineTables running at %t\n", IO.time[]];
FOR i: NAT IN [0..7] DO
l ← i*3;
loss ← l;
loss ← loss - 3.16; -- codec clipping level is at 3.16 dB
name ← IO.PutFR["SineTable%02d.dsm", IO.int[l]];
label ← IO.PutFR["STab%02d", IO.int[l]];
out.PutF["Writing %g ... ", IO.rope[name]];
res ← WriteDSMFile[label: label, name: name, data: MakeSineTable[dBToLoss[l]]];
out.PutF[IF res THEN "OK.\n" ELSE "Failed!\n"];
ENDLOOP;
out.PutF["\nLarkSineTables finished at %t\n", IO.time[]];
out.Flush[];
EXITS
Quit => NULL;
};
Pi: REAL = 3.1415926535;
TwoPi: REAL = 2.0 * Pi;
Constructs a 256 x 8 bit table
Address = phase fraction [0..255]
Value = u-255 value = sine(phase) attenuated by loss
MakeSineTable: PROC [loss: REAL] RETURNS [t: REF BTab] = {
int: INTEGER;
x: REAL;
t ← NEW[BTab[256]];
FOR i: CARDINAL IN [0..256) DO
x ← i;
x ← x/256.0; -- fraction of circle
x ← x * TwoPi; -- phase in radians
x ← RealFns.Sin[x]; -- IN [-1..1]
x ← x*loss; -- attenuate
x ← x*32767; -- scale to left justified two's complement
int ← Real.FixI[x]; -- Fix
t[i] ← U255.Encode[int];
ENDLOOP;
};
WriteDSMFile: PROC [label: Rope.ROPE, name: Rope.ROPE, data: REF BTab] RETURNS [BOOL] = {
f: IO.STREAM;
IF data.length MOD 2 # 0 THEN GOTO Fail;
f ← FS.StreamOpen[fileName: name, accessOptions: $create ! FS.Error => TRUSTED {GOTO Fail}];
f.PutF["; %g\n; L. Stewart %t\n\n", IO.rope[name], IO.time[]];
f.PutF["\nC𡤌ODE SEGMENT\n\n"];
f.PutF["←%g\tLABEL\tNEAR\n", IO.rope[label]];
FOR i: CARDINAL IN [0..data.length/2) DO
f.PutF["\tDW\t0%02x%02xH\n", IO.card[data[i+i+1]], IO.card[data[i+i]]];
ENDLOOP;
f.PutF["\n\nPUBLIC ←%g\n\nC𡤌ODE ENDS\n\tEND\n", IO.rope[label]];
f.Close[];
RETURN [TRUE];
EXITS
Fail => RETURN [FALSE];
};
dBToLoss: PROC [dBloss: REAL] RETURNS [loss:REAL] = {
RETURN [RealFns.Power[base: 10.0, exponent: -dBloss/20.0]];
};
Init: PROC = { Commander.Register["LarkSineTables", Main, "Make Lark Sinewave tables"]; };
Main program
Init[];
}.
Stewart, October 27, 1982 11:05 pm
Stewart, March 18, 1983 8:26 pm, Cedar 3.5
Stewart, June 6, 1983 12:36 pm, more sine tables
Stewart, September 29, 1983 9:52 pm, added 3.16 dB correction!
Stewart, December 22, 1983 3:11 pm, Cedar 5