Insert cell
Insert cell
Insert cell
DATA = PARSE(await FileAttachment("Raw data for strip 1 with 150000 bins").text());
Insert cell
EXPECTED_PEAKS = [
{ x: 32 },
{ x: 36.5 },
{ x: 659.0 },
]
Insert cell
function PARSE(text) {
const [x, y] = text
.split('\n')
.map((d) => (
d.split(' ')
.map((d) => Number.parseFloat(d))
));
const indices = new Array(x.length)
.fill(undefined)
.map((_, i) => ({ x: x[i], y: y[i], i }))
.filter(({ y }) => y > 0)
.map(({ i }) => i);

const sx = [], sy = [];
for (let i=0, n=indices.length; i<n; ++i) {
sx.push(x[indices[i]]);
sy.push(y[indices[i]]);
}
return { x: sx, y: sy };
}
Insert cell
{
const { x, y } = DATA;
const k = 63;

const ymax = Math.max(...y);
const xmin = 20.0;
const xmax = xmin + 60.0;

const originals = [];
const means = [];
const variances = [];
const amplitudes = [];
const offsets = [];
for (let i=k, n=x.length; i<n-k; ++i) {
if (!(xmin <= x[i] && x[i] <= xmax)) {
continue;
}
const { mean, variance, amplitude, offset } = stats({
x: x.slice(i-k, i+k+1),
y: y.slice(i-k, i+k+1),
});
originals.push({ x: x[i], y: y[i] });
means.push({ x: mean, y: mean - x[i] });
variances.push({ x: mean, y: variance });
amplitudes.push({ x: mean, y: amplitude });
offsets.push({ x: mean, y: amplitude });
}

return await embed({
width,
datasets: {
originals,
means,
variances,
amplitudes,
},

resolve: { scale: { x: 'shared', y: 'independent' } },

layer: [
...EXPECTED_PEAKS.map(({ x }) => ({
data: { values: [{}] },
mark: { type: 'rule', color: '#000000', size: 1, scale: { domainMin: xmin, domainMax: xmax } },
encoding: { x: { type: 'quantitative', datum: x } },
})),
...[
{ data: { name: 'originals' }, color: { value: '#ff0000' } },
{ data: { name: 'means' }, color: { value: '#00ff00' } },
{ data: { name: 'variances' }, color: { value: '#0000ff' } },
{ data: { name: 'amplitudes' }, color: { value: '#ffff00' } },
{ data: { name: 'amplitudes' }, color: { value: '#ff00ff' } },
].map(({ data, color }) => ({
data,
mark: 'line',
encoding: {
x: { field: 'x', type: 'quantitative', scale: { domainMin: xmin, domainMax: xmax } },
y: { field: 'y', type: 'quantitative' },
color,
},
})),
],
})
}
Insert cell
function stats({ x, y }) {
const ymin = Math.min(...y);
const ymax = Math.max(...y);

const B = 0;
let N = 0;
let U = 0;
for (let i=0, n=x.length; i<n; ++i) {
U += x[i] * (y[i] - B);
N += y[i];
}
U /= N;

let V = 0;
for (let i=0, n=x.length; i<n; ++i) {
V += (y[i] - B) * Math.pow(x[i] - U, 2.0);
}
V /= N;

const A = ymax * Math.pow(V * 2 * Math.PI, -2.0);

return { mean: U, variance: V, amplitude: A, offset: B };
}
Insert cell
PEAKS = MAKE_PEAKS(DATA);
Insert cell
function MAKE_PEAKS({ x, y }) {
const peaks = [];
const k = 2;
for (let i=k, n=x.length; i<n-k; ++i) {
let isPeak = true;
for (let j=0; j<k; ++j) {
if (y[i-j] <= y[i]) isPeak = false;
if (y[i-j] <= y[i-j+1])

}
}
peaks.push({ i, x: x[i], y: y[i] });
}
}

return peaks;

function VALID(
}
Insert cell
Insert cell
function ndiv(
Insert cell
function norm(y1, y2, l, w=null) {
let norm = 0.;
if (w === null) {
for (let i=0, n=y1.length; i<n; ++i) {
norm += Math.pow(1e-6 + Math.abs(y1[i] - y2[i]), l);
}
} else {
for (let i=0, n=y1.length; i<n; ++i) {
norm += w[i] * Math.pow(1e-6 + Math.abs(y1[i] - y2[i]), l);
}
}
return Math.pow(norm, 1./l);
}
Insert cell
function gauss([mult, mu, sigma], x, y=null) {
const a = mult * 1./ (1e-6 + sigma * Math.sqrt(2 * Math.PI));
const b = -1./2. / (1e-6 + Math.pow(sigma, 2.));
if (!Array.isArray(x)) {
y = a * Math.exp(b * Math.pow(x - mu, 2.));
} else {
if (y === null) {
y = new Array(x.length);
}
for (let i=0, n=x.length; i<n; ++i) {
y[i] = a * Math.exp(b * Math.pow(x[i] - mu, 2.));
}
}
return y;
}
Insert cell
function diffnorm(x, x0, l, y=null) {
if (y === null) {
y = new Array(x.length);
}

for (let i=0, n=x.length; i<n; ++i) {
y[i] = 1. / (1e-6 + Math.pow(Math.abs(x[i] - x0), l));
}

return y;
}
Insert cell
import { embed } from '@vega/hello-vega-embed'
Insert cell
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