Public
Edited
Dec 2, 2022
Insert cell
Insert cell
maxRandomZeroes = 32
Insert cell
windowSizeMs = 1000
Insert cell
timeBetweenEvents = 25 // milliseconds
Insert cell
setpoint = 1 // events/ second
Insert cell
Kp = 0.7
Insert cell
Ki = 0.5
Insert cell
Kd = 0
Insert cell
Insert cell
function* eventGenerator() {
let normalizedRate = 0;
let events = 0;
const pid = new PID(normalizedRate, setpoint, Kp, Ki, Kd, "reverse");
pid.setMode("auto");
let lastEventMs = Date.now();
let lastTime = Date.now();
let output = 0;
while (true) {
const zeroes = Math.floor(Math.random() * maxRandomZeroes);

pid.setInput((normalizedRate * 1000) / windowSizeMs);
do {} while (!pid.compute()); // takes about 100 ms
//output = pid.getOutput();
//if (pid.compute()) {
output = pid.getOutput();
//}

const currentMs = Date.now();
if (currentMs - lastTime >= timeBetweenEvents) {
lastTime = currentMs;

const period = currentMs - lastEventMs;
lastEventMs = currentMs;
const normalizedFreq = windowSizeMs / period;
const alpha = Math.min(1, 1 / normalizedFreq);
if (zeroes >= output) {
normalizedRate = alpha * normalizedFreq + (1 - alpha) * normalizedRate;
events++;
} else {
normalizedRate = (1 - alpha) * normalizedRate;
}
}

yield {
rate: (normalizedRate * 1000) / windowSizeMs,
minPow: Math.floor(output),
events,
zeroes
};
}
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more