use it elsewhere

Correlation rig

Put it on your own page with two lines, or fork it and make it yours. It is CC-BY-4.0.

Embed

The script finds its own origin, so nothing else is needed; the widget runs in a sandboxed frame and cannot touch your page. Set the controls and the two lines update; the preview beneath shows what your readers get.

<script src="https://learn.mimmsy.com/learn-widget.js"></script> <learn-widget name="correlation-rig"></learn-widget>

It is CC-BY-4.0: keep the credit line the frame shows.

Preview

Fork

A fork is a copy whose manifest names its parent and the parent's version; lineage is kept forever, and the copy is yours to change. The library is its own repository: clone it, copy correlation-rig/ to a new name (one DNS label), set forkedFrom to { "name": "correlation-rig", "version": 1 }, change what you want, run npm run check, and open a pull request. Passing the check is the whole gate. Without a checkout, submit the same files to POST /api/widgets and it is served instantly as an unreviewed draft.

Source

The whole widget is these files; the repository has their history.

widget.json

{
  "name": "correlation-rig",
  "title": "Correlation rig",
  "version": 1,
  "claim": "The average of the pointwise product of a signal and a template is large when their signs stay aligned and cancels toward zero when they drift in and out of alignment.",
  "summary": "Set the template and signal frequencies in whole cycles per window; the top band overlays the two waveforms and the bottom band shades their pointwise product, whose net area is the correlation readout.",
  "topics": [
    "signals/correlation",
    "math/vectors"
  ],
  "aliases": [
    "correlation",
    "dot product",
    "inner product",
    "similarity"
  ],
  "params": {
    "templateFreq": {
      "type": "integer",
      "default": 5,
      "min": 1,
      "max": 12,
      "label": "template frequency (cycles per window)"
    },
    "signalFreq": {
      "type": "integer",
      "default": 5,
      "min": 1,
      "max": 12,
      "label": "signal frequency (cycles per window)"
    }
  },
  "check": [
    {
      "q": "Correlation multiplies signal and template pointwise, then averages. A large positive result means…",
      "options": [
        "the signal is louder than the template",
        "the two waveforms are identical",
        "the signal contains many frequencies",
        "the two tend to have the same sign at the same times"
      ],
      "answer": 3,
      "why": "Same-sign products are positive and opposite-sign products are negative, so persistent alignment is what survives the average; any aligned, scaled copy correlates strongly, not only an identical one."
    }
  ],
  "capabilities": [],
  "height": 510,
  "requires": [],
  "forkedFrom": null,
  "authors": []
}

index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Correlation rig</title>
<style>
  :root {
    color-scheme: light dark;
    --bg: #f7f3ea;
    --bg-card: #efe9da;
    --border: #d8cfba;
    --text: #211d14;
    --text-dim: #6e6553;
    --accent: #31597f;
    --accent2: #b04e1b;
    --hot: #a82433;
    --ok: #3d6b4f;
    --ink-rgb: 33, 29, 20;
    --paper-rgb: 247, 243, 234;
    --accent-rgb: 49, 89, 127;
    --accent2-rgb: 176, 78, 27;
    --hot-rgb: 168, 36, 51;
    --ok-rgb: 61, 107, 79;
    --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #161410;
      --bg-card: #1e1b15;
      --border: #383225;
      --text: #e9e3d3;
      --text-dim: #9c917c;
      --accent: #8fb8e0;
      --accent2: #dd9355;
      --hot: #df7a88;
      --ok: #82bd97;
      --ink-rgb: 233, 227, 211;
      --paper-rgb: 22, 20, 16;
      --accent-rgb: 143, 184, 224;
      --accent2-rgb: 221, 147, 85;
      --hot-rgb: 223, 122, 136;
      --ok-rgb: 130, 189, 151;
    }
  }

  * { box-sizing: border-box; }
  html, body { margin: 0; }
  body { background: transparent; color: var(--text); font-family: var(--serif); font-size: 15px; line-height: 1.5; }

  .widget { background: var(--bg-card); border: 1px solid var(--border); border-radius: 4px; padding: 22px; }

  .control-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
  .control-row label { font-size: 14px; color: var(--text-dim); white-space: nowrap; }
  .control-row.sym { display: grid; grid-template-columns: 88px 1fr 1fr; gap: 12px 16px; align-items: center; }
  .control-row.sym.single { grid-template-columns: 88px 1fr; }
  .ctl { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .ctl input[type="range"] { flex: 1; min-width: 0; }
  .ctl .val { flex: none; width: 64px; text-align: right; font-size: 13px; }
  @media (max-width: 600px) {
    .control-row.sym { grid-template-columns: 64px 1fr; }
    .control-row.sym .ctl:nth-of-type(2) { grid-column: 2; }
  }
  input[type="range"] { flex: 1; min-width: 110px; accent-color: var(--accent); }
  .toggle { display: inline-flex; align-items: baseline; gap: 7px; font-size: 13.5px; color: var(--text-dim); cursor: pointer; }
  .toggle input { accent-color: var(--accent2); }
  .control-row .toggle { white-space: normal; }

  .mono { font-family: var(--mono); }
  .dim { color: var(--text-dim); font-size: 12.5px; }

  .chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
  .chip, .btn {
    font-family: var(--mono); font-size: 12.5px; color: var(--text); background: transparent;
    border: 1px solid var(--border); border-radius: 3px; padding: 6px 13px; cursor: pointer;
    transition: border-color 0.15s, color 0.15s;
  }
  .chip:hover, .btn:hover { border-color: var(--accent); color: var(--accent); }
  .btn.primary { border-color: var(--accent); color: var(--accent); }
  .btn.playing, .btn.playing:hover { border-color: var(--hot); color: var(--hot); }
  .btn.off { border-color: var(--hot); color: var(--hot); opacity: 0.85; }
  .btn:disabled { opacity: 0.35; cursor: default; }
  .btn:disabled:hover { border-color: var(--border); color: var(--text); }
  .chip.sel-chip { border-color: var(--accent); color: var(--accent); }

  .readout-row { display: flex; gap: 28px; flex-wrap: wrap; margin: 14px 0 0; }
  .readout { text-align: left; }
  .readout.inline { margin-left: auto; text-align: right; }
  .readout .big { display: block; font-size: 30px; font-weight: 600; color: var(--accent); line-height: 1.1; min-width: 5ch; }
  .readout.accent .big { color: var(--accent2); }
  .readout .big.hot { color: var(--hot); }
  .readout small { color: var(--text-dim); font-size: 12.5px; }
  @media (max-width: 560px) { .readout .big { font-size: 25px; } }

  .plot { display: block; width: 100%; height: 220px; margin-top: 8px; }
  .plot.tall { height: 280px; }
  .plot.duo { height: 320px; }
  .plot.phasor { height: 400px; }
  .plot.spec { height: 230px; }

  .mono, .ctl .val, .readout .big, .readout small { font-variant-numeric: tabular-nums; }
</style>
</head>
<body>
  <div class="widget">

    <div class="control-row sym single">
      <label for="corr-f1">template freq</label>
      <span class="ctl wide"><input type="range" id="corr-f1" min="1" max="12" value="5"><span class="mono val" id="corr-f1-label">5 cyc</span></span>
    </div>
    <div class="control-row sym single">
      <label for="corr-f2">signal freq</label>
      <span class="ctl wide"><input type="range" id="corr-f2" min="1" max="12" value="5"><span class="mono val" id="corr-f2-label">5 cyc</span></span>
    </div>
    <canvas id="corr-canvas" class="plot duo"></canvas>
    <div class="readout-row">
      <div class="readout">
        <span class="big mono" id="corr-val">0.500</span>
        <small>correlation &middot; average of the product</small>
      </div>
    </div>
  </div>
  <script src="/w/_sdk/host.js?v=1"></script>
  <script src="/w/_lib/dsp.js?v=1"></script>
  <script src="widget.js?v=1"></script>
</body>
</html>

widget.js

/* Correlation rig: template × signal pointwise, signed product area, averaged. */
(() => {
  'use strict';
  const $ = (id) => document.getElementById(id);
  const { T, setupCanvas, hline, trace, okWash, hotWash, zero, signal } = DSP;

  function render() {
    const f1 = +$('corr-f1').value;
    const f2 = +$('corr-f2').value;
    $('corr-f1-label').textContent = f1 + ' cyc';
    $('corr-f2-label').textContent = f2 + ' cyc';

    const N = 700;
    const probe = (t) => Math.sin(2 * Math.PI * f1 * t);
    const sig = (t) => Math.sin(2 * Math.PI * f2 * t);
    let corr = 0;
    for (let i = 0; i < N; i++) corr += probe(i / N) * sig(i / N);
    corr /= N;
    $('corr-val').textContent = zero(corr).toFixed(3);

    const { ctx, w, h } = setupCanvas($('corr-canvas'));
    const x0 = 14;
    const pw = w - 28;
    const topMid = h * 0.26;
    const topAmp = h * 0.18;
    const botMid = h * 0.72;
    const botAmp = h * 0.2;

    ctx.clearRect(0, 0, w, h);
    ctx.font = DSP.font(10);
    hline(ctx, x0, w - 14, topMid, T.GRID);
    hline(ctx, x0, w - 14, botMid, T.GRID);

    trace(ctx, probe, { x0, pw, mid: topMid, amp: topAmp, color: T.ACCENT, lw: 1.8 });
    trace(ctx, sig, { x0, pw, mid: topMid, amp: topAmp, color: T.ACCENT2, lw: 1.8 });
    DSP.label(ctx, 'template (blue) · signal (orange)', x0 + 2, 12);

    for (let x = 0; x <= pw; x++) {
      const v = probe(x / pw) * sig(x / pw);
      ctx.strokeStyle = v >= 0 ? okWash(0.4) : hotWash(0.4);
      ctx.beginPath();
      ctx.moveTo(x0 + x, botMid);
      ctx.lineTo(x0 + x, botMid - botAmp * v);
      ctx.stroke();
    }
    DSP.label(ctx, 'template × signal: net shaded area = correlation', x0 + 2, botMid - botAmp - 8);
  }

  ['corr-f1', 'corr-f2'].forEach((id) => $(id).addEventListener('input', () => {
    render();
    signal('interaction', { control: id });
  }));

  function applyParams(p) {
    if (Number.isInteger(p.templateFreq)) $('corr-f1').value = p.templateFreq;
    if (Number.isInteger(p.signalFreq)) $('corr-f2').value = p.signalFreq;
    render();
  }

  render();
  DSP.onResize(render);
  DSP.connect({ render, params: applyParams });
})();