<> <> <> DIRECTORY Basics, Commander, CommandTool, Convert, IO, Random, Real, RealFns, U255; LarkSilenceTestImpl: CEDAR PROGRAM IMPORTS Basics, Commander, CommandTool, Convert, IO, Random, Real, RealFns, U255 = { Main: Commander.CommandProc = { out: IO.STREAM _ cmd.out; argv: CommandTool.ArgumentVector; intFrequency, intPower, intPhase, intAmplitude: INT; frequency, phaseIncrement, phase, power, amplitude: REAL; muValue: INTEGER; sineTotal, squareTotal, amiTotal: INT; sineSum, squareSum, amiSum: ARRAY [0..6) OF INT; sqsineTotal, sqsquareTotal, sqamiTotal: INT; sqsineSum, sqsquareSum, sqamiSum: ARRAY [0..6) OF INT; sineGain, squareGain, amiDutyCycle: REAL; argv _ CommandTool.Parse[cmd ! CommandTool.Failed => { msg _ errorMsg; CONTINUE; }]; IF argv = NIL THEN RETURN[$Failure, msg]; [] _ Random.Init[range: 0, seed: -1]; <> IF argv.argc < 2 THEN { out.PutRope["Usage LarkSilenceTest frequency\n"]; RETURN; }; intFrequency _ Convert.IntFromRope[argv[1] ! Convert.Error => GOTO BadArgs]; frequency _ intFrequency; phaseIncrement _ (frequency * TwoPi) / 8000.0; out.PutF["Frequency %d\n", IO.int[intFrequency]]; out.PutRope["dBm square sine ami\n"]; FOR intPower IN [-50..10] DO power _ intPower; <<>> <> <<>> <> sineGain _ RealFns.Power[base: 10.0, exponent: (power - 3.16) / 20.0]; <<>> <> squareGain _ RealFns.Power[base: 10.0, exponent: (power - 6.18) / 20.0]; <<>> <> amiDutyCycle _ RealFns.Power[base: 10.0, exponent: (power - 6.18) / 10.0]; <> intPhase _ Random.Choose[min: 0, max: 359]; phase _ intPhase; phase _ phase / TwoPi; sineSum _ squareSum _ amiSum _ ALL[0]; sineTotal _ squareTotal _ amiTotal _ 0; sqsineSum _ sqsquareSum _ sqamiSum _ ALL[0]; sqsineTotal _ sqsquareTotal _ sqamiTotal _ 0; FOR trial: NAT IN [0..6) DO FOR i: NAT IN [0..160) DO phase _ phase + phaseIncrement; WHILE phase < 0 DO phase _ phase + TwoPi; ENDLOOP; WHILE phase >= TwoPi DO phase _ phase - TwoPi; ENDLOOP; <> amplitude _ RealFns.Sin[radians: phase]; amplitude _ amplitude * sineGain * 32767.0; IF amplitude > 32767.0 THEN amplitude _ 32767.0; IF amplitude < -32767.0 THEN amplitude _ -32767.0; intAmplitude _ Real.FixI[amplitude]; TRUSTED { muValue _ U255.Encode[intAmplitude]; }; muValue _ Basics.BITAND[muValue, 177B]; -- mask off sign bit sineSum[trial] _ sineSum[trial] + muValue; sqsineSum[trial] _ sqsineSum[trial] + NewSilence[muValue]; <> amplitude _ squareGain * 32767.0; IF amplitude > 32767.0 THEN amplitude _ 32767.0; IF amplitude < -32767.0 THEN amplitude _ -32767.0; intAmplitude _ Real.FixI[amplitude]; TRUSTED { muValue _ U255.Encode[intAmplitude]; }; muValue _ Basics.BITAND[muValue, 177B]; -- mask off sign bit squareSum[trial] _ squareSum[trial] + muValue; sqsquareSum[trial] _ sqsquareSum[trial] + NewSilence[muValue]; <> IF phase < (TwoPi * amiDutyCycle) THEN amplitude _ 32767.0 ELSE amplitude _ 0.0; intAmplitude _ Real.FixI[amplitude]; TRUSTED { muValue _ U255.Encode[intAmplitude]; }; muValue _ Basics.BITAND[muValue, 177B]; -- mask off sign bit amiSum[trial] _ amiSum[trial] + muValue; sqamiSum[trial] _ sqamiSum[trial] + NewSilence[muValue]; ENDLOOP; sineTotal _ sineTotal + sineSum[trial]; squareTotal _ squareTotal + squareSum[trial]; amiTotal _ amiTotal + amiSum[trial]; sqsineTotal _ sqsineTotal + sqsineSum[trial]; sqsquareTotal _ sqsquareTotal + sqsquareSum[trial]; sqamiTotal _ sqamiTotal + sqamiSum[trial]; ENDLOOP; sineTotal _ sineTotal / 6; squareTotal _ squareTotal / 6; amiTotal _ amiTotal / 6; sqsineTotal _ sqsineTotal / 6; sqsquareTotal _ sqsquareTotal / 6; sqamiTotal _ sqamiTotal / 6; out.PutF["%3d: %8d %8d %8d", IO.int[intPower], IO.int[squareTotal], IO.int[sineTotal], IO.int[amiTotal]]; out.PutF[" %8d %8d %8d\n", IO.int[sqsquareTotal], IO.int[sqsineTotal], IO.int[sqamiTotal]]; ENDLOOP; EXITS BadArgs => NULL; }; NewSilence: PROC [v: INTEGER] RETURNS [CARDINAL] = { max: REAL; thisValue: REAL; TRUSTED { max _ U255.Decode[127]; thisValue _ U255.Decode[v]; }; max _ max * max; thisValue _ thisValue * thisValue; thisValue _ thisValue * (408.0 / max); thisValue _ thisValue + 1; RETURN[Real.FixC[thisValue]]; }; Pi: REAL = 3.1415926535; TwoPi: REAL = 2.0 * Pi; Init: PROC = { Commander.Register["LarkSilenceTest", Main, "LarkSilenceTest frequency"]; }; <
> Init[]; }. <> <> <>