nmrlink 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +161 -0
- package/README.md +70 -42
- package/package.json +2 -1
- package/src/baseline.js +5 -4
- package/src/browser.js +15 -11
- package/src/controller.js +8 -6
- package/src/device.js +14 -12
- package/src/errors.js +187 -0
- package/src/fft.js +6 -5
- package/src/frequency-record.js +8 -7
- package/src/frequency-sweep.js +3 -2
- package/src/graph-labels.js +88 -0
- package/src/graphs.js +51 -43
- package/src/index.js +1 -0
- package/src/line-decoder.js +7 -6
- package/src/protocol.js +7 -6
- package/src/pulse-fit.js +13 -12
- package/src/pulse-record.js +10 -9
- package/src/pulse-sweep.js +10 -9
- package/src/record.js +10 -9
- package/src/relaxation-fit.js +10 -9
- package/src/sample-validation.js +3 -2
- package/src/signed-signal.js +10 -9
- package/src/sweep.js +4 -3
- package/src/t1-record.js +10 -9
- package/src/t1-sweep.js +12 -11
- package/src/t2-record.js +10 -9
- package/src/t2-sweep.js +15 -14
- package/src/timing.js +2 -1
- package/src/web-serial-transport.js +19 -17
package/src/line-decoder.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
/**
|
|
2
3
|
* Assemble ASCII protocol lines from arbitrary serial byte chunks.
|
|
3
4
|
* Keeps only the incomplete line; complete lines are returned to the caller.
|
|
@@ -5,7 +6,7 @@
|
|
|
5
6
|
export class LineDecoder {
|
|
6
7
|
constructor({ maxLineBytes = 65536 } = {}) {
|
|
7
8
|
if (!Number.isSafeInteger(maxLineBytes) || maxLineBytes < 1) {
|
|
8
|
-
throw
|
|
9
|
+
throw createError(RangeError, "INVALID_MAX_LINE_BYTES");
|
|
9
10
|
}
|
|
10
11
|
this.maxLineBytes = maxLineBytes;
|
|
11
12
|
this.partial = "";
|
|
@@ -14,17 +15,17 @@ export class LineDecoder {
|
|
|
14
15
|
|
|
15
16
|
push(bytes) {
|
|
16
17
|
if (this.failed) {
|
|
17
|
-
throw
|
|
18
|
+
throw createError(Error, "DECODER_FAILED");
|
|
18
19
|
}
|
|
19
20
|
if (!(bytes instanceof Uint8Array)) {
|
|
20
|
-
throw
|
|
21
|
+
throw createError(TypeError, "INVALID_SERIAL_BYTES");
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
const lines = [];
|
|
24
25
|
for (const byte of bytes) {
|
|
25
26
|
if (byte > 0x7f) {
|
|
26
27
|
this.failed = true;
|
|
27
|
-
throw
|
|
28
|
+
throw createError(Error, "NON_ASCII_RESPONSE");
|
|
28
29
|
}
|
|
29
30
|
if (byte === 0x0a) {
|
|
30
31
|
lines.push(this.partial.replace(/\r$/, ""));
|
|
@@ -33,7 +34,7 @@ export class LineDecoder {
|
|
|
33
34
|
this.partial += String.fromCharCode(byte);
|
|
34
35
|
if (this.partial.length > this.maxLineBytes) {
|
|
35
36
|
this.failed = true;
|
|
36
|
-
throw
|
|
37
|
+
throw createError(Error, "RESPONSE_LINE_TOO_LONG");
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
}
|
|
@@ -44,7 +45,7 @@ export class LineDecoder {
|
|
|
44
45
|
finish() {
|
|
45
46
|
if (this.failed || this.partial.length !== 0) {
|
|
46
47
|
this.failed = true;
|
|
47
|
-
throw
|
|
48
|
+
throw createError(Error, "INCOMPLETE_RESPONSE");
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
|
package/src/protocol.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
/** Wire format used by the prototype controller. See docs/PROTOCOL.md. */
|
|
2
3
|
export const SAMPLE_COUNT = 2048;
|
|
3
4
|
export const SAMPLE_INTERVAL_MS = 0.02;
|
|
@@ -36,21 +37,21 @@ const INTEGER_RANGES = {
|
|
|
36
37
|
/** Validate and copy only the supported fields. @param {MeasurementParameters} input */
|
|
37
38
|
export function validateParameters(input) {
|
|
38
39
|
if (input === null || typeof input !== "object" || Array.isArray(input)) {
|
|
39
|
-
throw
|
|
40
|
+
throw createError(TypeError, "INVALID_PARAMETERS");
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
const parameters = {};
|
|
43
44
|
for (const [name, [minimum, maximum]] of Object.entries(INTEGER_RANGES)) {
|
|
44
45
|
const value = input[name];
|
|
45
46
|
if (!Number.isInteger(value) || value < minimum || value > maximum) {
|
|
46
|
-
throw
|
|
47
|
+
throw createError(RangeError, "INVALID_PARAMETER_RANGE", { name, minimum, maximum });
|
|
47
48
|
}
|
|
48
49
|
parameters[name] = value;
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
for (const name of ["phase1Degrees", "phase2Degrees"]) {
|
|
52
53
|
if (![0, 90, 180, 270].includes(input[name])) {
|
|
53
|
-
throw
|
|
54
|
+
throw createError(RangeError, "INVALID_PARAMETER_PHASE", { name });
|
|
54
55
|
}
|
|
55
56
|
parameters[name] = input[name];
|
|
56
57
|
}
|
|
@@ -89,7 +90,7 @@ export function encodeCommand(command) {
|
|
|
89
90
|
read: [0x72, 0x0d, 0x0a],
|
|
90
91
|
};
|
|
91
92
|
if (!Object.hasOwn(commands, command)) {
|
|
92
|
-
throw
|
|
93
|
+
throw createError(RangeError, "UNSUPPORTED_COMMAND", { command });
|
|
93
94
|
}
|
|
94
95
|
return new Uint8Array(commands[command]);
|
|
95
96
|
}
|
|
@@ -97,11 +98,11 @@ export function encodeCommand(command) {
|
|
|
97
98
|
/** Decode one complete channel-pair line. Invalid data is never replaced by zero. */
|
|
98
99
|
export function parseSampleLine(line) {
|
|
99
100
|
if (typeof line !== "string" || !/^-?\d+,-?\d+$/.test(line.trim())) {
|
|
100
|
-
throw
|
|
101
|
+
throw createError(TypeError, "INVALID_SAMPLE_LINE");
|
|
101
102
|
}
|
|
102
103
|
const sample = line.trim().split(",").map(Number);
|
|
103
104
|
if (!sample.every(Number.isSafeInteger)) {
|
|
104
|
-
throw
|
|
105
|
+
throw createError(RangeError, "SAMPLE_OUT_OF_RANGE");
|
|
105
106
|
}
|
|
106
107
|
return sample;
|
|
107
108
|
}
|
package/src/pulse-fit.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
// The original PoC fit is retained as the first choice for well-resolved signed data.
|
|
2
3
|
function signedPulseCandidates(points) {
|
|
3
4
|
const p = [...points].sort((a2, b2) => a2.length - b2.length);
|
|
4
|
-
if (p.length < 5 || new Set(p.map((q) => q.length)).size !== p.length || !p.every((q) => Number.isFinite(q.amplitude) && Number.isFinite(q.length) && q.length > 0)) throw
|
|
5
|
+
if (p.length < 5 || new Set(p.map((q) => q.length)).size !== p.length || !p.every((q) => Number.isFinite(q.amplitude) && Number.isFinite(q.length) && q.length > 0)) throw createError(Error, "INVALID_SIGNED_PULSE_POINTS");
|
|
5
6
|
const scale = Math.max(...p.map((q) => Math.abs(q.amplitude)));
|
|
6
|
-
if (scale < 1e-9) throw
|
|
7
|
+
if (scale < 1e-9) throw createError(Error, "ZERO_SIGNAL");
|
|
7
8
|
const peak = p.findIndex((q, i) => i > 0 && i < p.length - 1 && q.amplitude >= p[i - 1].amplitude && q.amplitude > p[i + 1].amplitude && q.amplitude > 0.35 * scale);
|
|
8
|
-
if (peak < 0) throw
|
|
9
|
-
if (p[0].length >= p[peak].length * 0.65) throw
|
|
9
|
+
if (peak < 0) throw createError(Error, "PULSE_PEAK_NOT_FOUND");
|
|
10
|
+
if (p[0].length >= p[peak].length * 0.65) throw createError(Error, "INSUFFICIENT_SHORT_PULSES");
|
|
10
11
|
let crossing = -1;
|
|
11
12
|
for (let i = peak; i < p.length - 1; i++) if (p[i].amplitude >= 0 && p[i + 1].amplitude < 0) {
|
|
12
13
|
crossing = i;
|
|
13
14
|
break;
|
|
14
15
|
}
|
|
15
|
-
if (crossing < 0) throw
|
|
16
|
+
if (crossing < 0) throw createError(Error, "PULSE_ZERO_CROSSING_NOT_FOUND");
|
|
16
17
|
const a = p[crossing], b = p[crossing + 1], p180 = a.length + (b.length - a.length) * a.amplitude / (a.amplitude - b.amplitude);
|
|
17
18
|
return { p90: p[peak].length, p180, peakIndex: peak, crossingIndex: crossing, points: p, zeroBracket: [a.length, b.length] };
|
|
18
19
|
}
|
|
@@ -34,7 +35,7 @@ function strictPulseFit(points) {
|
|
|
34
35
|
index = i;
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
|
-
if (index === 0 || index === steps) throw
|
|
38
|
+
if (index === 0 || index === steps) throw createError(Error, "PULSE_FIT_BOUNDARY");
|
|
38
39
|
let l = lower + (upper - lower) * (index - 1) / steps, h = lower + (upper - lower) * (index + 1) / steps;
|
|
39
40
|
for (let i = 0; i < 45; i++) {
|
|
40
41
|
const a2 = l + (h - l) / 3, b2 = h - (h - l) / 3;
|
|
@@ -43,10 +44,10 @@ function strictPulseFit(points) {
|
|
|
43
44
|
}
|
|
44
45
|
best = evaluate((l + h) / 2);
|
|
45
46
|
const mean = p.reduce((s, q) => s + q.amplitude, 0) / p.length, sst = p.reduce((s, q) => s + (q.amplitude - mean) ** 2, 0), r2 = 1 - best.sse / sst, p180 = 2 * best.p90;
|
|
46
|
-
if (!Number.isFinite(r2) || r2 < 0.85) throw
|
|
47
|
-
if (Math.abs(best.B) > 0.2 * best.A) throw
|
|
47
|
+
if (!Number.isFinite(r2) || r2 < 0.85) throw createError(Error, "PULSE_FIT_LOW_R2");
|
|
48
|
+
if (Math.abs(best.B) > 0.2 * best.A) throw createError(Error, "PULSE_BACKGROUND_TOO_LARGE");
|
|
48
49
|
const [a, b] = measured.zeroBracket, margin = Math.max(b - a, 1);
|
|
49
|
-
if (p180 < p[0].length || p180 > p.at(-1).length || p180 < a - margin || p180 > b + margin) throw
|
|
50
|
+
if (p180 < p[0].length || p180 > p.at(-1).length || p180 < a - margin || p180 > b + margin) throw createError(Error, "PULSE_CANDIDATE_MISMATCH");
|
|
50
51
|
return { p90: best.p90, p180, A: best.A, B: best.B, r2, rmse: Math.sqrt(best.sse / p.length), from: p[0].length, to: p.at(-1).length, n: p.length, measured: { p90: measured.p90, p180: measured.p180 }, model: "A*sin(pi*length/(2*p90))+B" };
|
|
51
52
|
}
|
|
52
53
|
/** Least-squares sine estimate even when a peak or zero crossing was not measured. */
|
|
@@ -56,10 +57,10 @@ function fitPulseCalibration(points, { magnitude = false } = {}) {
|
|
|
56
57
|
}
|
|
57
58
|
const p = [...points].sort((a, b) => a.length - b.length);
|
|
58
59
|
if (p.length < 3 || !p.every(q => Number.isFinite(q.length) && q.length > 0 && Number.isFinite(q.amplitude)) || new Set(p.map(q => q.length)).size !== p.length) {
|
|
59
|
-
throw
|
|
60
|
+
throw createError(Error, "INSUFFICIENT_PULSE_FIT_POINTS");
|
|
60
61
|
}
|
|
61
62
|
const scale = Math.max(...p.map(q => Math.abs(q.amplitude)));
|
|
62
|
-
if (scale < 1e-9) throw
|
|
63
|
+
if (scale < 1e-9) throw createError(Error, "ZERO_PULSE_FIT_SIGNAL");
|
|
63
64
|
const y = p.map(q => q.amplitude / scale), n = p.length, sy = y.reduce((s,v) => s+v,0);
|
|
64
65
|
const mean = sy / n, sst = y.reduce((s,v) => s+(v-mean)**2,0);
|
|
65
66
|
function solve(logLength) {
|
|
@@ -76,7 +77,7 @@ function fitPulseCalibration(points, { magnitude = false } = {}) {
|
|
|
76
77
|
const lo = Math.log(step/2), hi = Math.log(p.at(-1).length*10), steps = 4000;
|
|
77
78
|
let best = {sse:Infinity}, index = 0;
|
|
78
79
|
for (let i=0;i<=steps;i++) { const value=solve(lo+(hi-lo)*i/steps); if(value.sse<best.sse){best=value;index=i;} }
|
|
79
|
-
if (!Number.isFinite(best.sse)) throw
|
|
80
|
+
if (!Number.isFinite(best.sse)) throw createError(Error, "PULSE_FIT_FAILED");
|
|
80
81
|
let l=lo+(hi-lo)*Math.max(0,index-1)/steps, h=lo+(hi-lo)*Math.min(steps,index+1)/steps;
|
|
81
82
|
for(let i=0;i<45;i++){const a=l+(h-l)/3,b=h-(h-l)/3;if(solve(a).sse<solve(b).sse)h=b;else l=a;}
|
|
82
83
|
best=solve((l+h)/2);
|
package/src/pulse-record.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
import { parseMeasurement } from "./record.js";
|
|
2
3
|
import { pulseSteps, evaluatePulse, analyzePulseCalibration } from "./pulse-sweep.js";
|
|
3
4
|
import { validateParameters } from "./protocol.js";
|
|
@@ -7,21 +8,21 @@ export const MAX_PULSE_JSON_BYTES = 32 * 1024 * 1024;
|
|
|
7
8
|
/** Restore raw records and recompute all derived values before replacing a UI's results. */
|
|
8
9
|
export function parsePulseCalibration(text) {
|
|
9
10
|
if (typeof text !== "string" || new TextEncoder().encode(text).length > MAX_PULSE_JSON_BYTES) {
|
|
10
|
-
throw
|
|
11
|
+
throw createError(TypeError, "ARCHIVE_TOO_LARGE");
|
|
11
12
|
}
|
|
12
13
|
let value;
|
|
13
14
|
try { value = JSON.parse(text.replace(/^\uFEFF/, "")); }
|
|
14
|
-
catch { throw
|
|
15
|
+
catch (cause) { throw createError(TypeError, "INVALID_JSON", {}, { cause }); }
|
|
15
16
|
if (value?.kind !== "pulse-calibration" || !Array.isArray(value.points) || !value.points.length || value.points.length > 201) {
|
|
16
|
-
throw
|
|
17
|
+
throw createError(TypeError, "INVALID_PULSE_ARCHIVE");
|
|
17
18
|
}
|
|
18
19
|
const saved = value.settings;
|
|
19
|
-
if (!saved) throw
|
|
20
|
+
if (!saved) throw createError(TypeError, "MISSING_SWEEP_SETTINGS");
|
|
20
21
|
const lengths = pulseSteps(saved.startCycles, saved.endCycles, saved.stepCycles);
|
|
21
22
|
const parameters = validateParameters(saved.parameters);
|
|
22
23
|
const a = saved.analysis;
|
|
23
24
|
if (!a || ![a.startMs, a.endMs, a.tailPoints, a.minHz, a.maxHz].every(Number.isFinite) || !["hann", "rect"].includes(a.window)) {
|
|
24
|
-
throw
|
|
25
|
+
throw createError(TypeError, "INVALID_ANALYSIS_OPTIONS");
|
|
25
26
|
}
|
|
26
27
|
const analysis = { startMs: a.startMs, endMs: a.endMs, tailPoints: a.tailPoints, window: a.window, minHz: a.minHz, maxHz: a.maxHz };
|
|
27
28
|
const points = value.points.map((point, index) => {
|
|
@@ -29,21 +30,21 @@ export function parsePulseCalibration(text) {
|
|
|
29
30
|
if (!record.parameters || record.source !== value.source || record.mode !== "single" ||
|
|
30
31
|
point.length !== lengths[index] || record.parameters.pulse1Cycles !== lengths[index] ||
|
|
31
32
|
Object.keys(parameters).some(key => key !== "pulse1Cycles" && parameters[key] !== record.parameters[key])) {
|
|
32
|
-
throw
|
|
33
|
+
throw createError(TypeError, "SWEEP_RECORD_MISMATCH");
|
|
33
34
|
}
|
|
34
35
|
return evaluatePulse(record, analysis);
|
|
35
36
|
});
|
|
36
37
|
const referenceIndex = value.calibration?.reference?.referenceIndex ?? null;
|
|
37
38
|
const phaseDegrees = value.calibration?.reference?.phaseDegrees ?? 0;
|
|
38
|
-
if (referenceIndex !== null && (!Number.isInteger(referenceIndex) || referenceIndex < 0 || referenceIndex >= points.length)) throw
|
|
39
|
-
if (!Number.isFinite(phaseDegrees)) throw
|
|
39
|
+
if (referenceIndex !== null && (!Number.isInteger(referenceIndex) || referenceIndex < 0 || referenceIndex >= points.length)) throw createError(TypeError, "INVALID_REFERENCE_INDEX");
|
|
40
|
+
if (!Number.isFinite(phaseDegrees)) throw createError(TypeError, "INVALID_PHASE");
|
|
40
41
|
let calibration = null;
|
|
41
42
|
try { calibration = analyzePulseCalibration(points.map(point => point.record), { analysis, referenceIndex, phaseDegrees }); }
|
|
42
43
|
catch (error) {
|
|
43
44
|
// A valid zero-signal scan is still useful for inspecting raw waveforms.
|
|
44
45
|
if (error.code !== "NO_REFERENCE_SIGNAL") throw error;
|
|
45
46
|
}
|
|
46
|
-
if (typeof value.acquiredAt !== "string" || !Number.isFinite(Date.parse(value.acquiredAt))) throw
|
|
47
|
+
if (typeof value.acquiredAt !== "string" || !Number.isFinite(Date.parse(value.acquiredAt))) throw createError(TypeError, "INVALID_SWEEP_TIME");
|
|
47
48
|
return { source: value.source, acquiredAt: value.acquiredAt,
|
|
48
49
|
settings: { startCycles: saved.startCycles, endCycles: saved.endCycles, stepCycles: saved.stepCycles, lengths, parameters, analysis },
|
|
49
50
|
points, calibration, referenceIndex, phaseDegrees };
|
package/src/pulse-sweep.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createWarning, warningFromError } from "./errors.js";
|
|
1
2
|
import { integerSteps, runSingleSweep } from "./sweep.js";
|
|
2
3
|
import { evaluateFrequency } from "./frequency-sweep.js";
|
|
3
4
|
import { analyzeSignedRecords } from "./signed-signal.js";
|
|
@@ -26,15 +27,15 @@ export function analyzePulseCalibration(records, options = {}) {
|
|
|
26
27
|
const { points, reference } = result;
|
|
27
28
|
const coherence = reference.coherence;
|
|
28
29
|
let measured = null, fit = null;
|
|
29
|
-
const
|
|
30
|
-
if (coherence < 0.85)
|
|
31
|
-
try { measured = signedPulseCandidates(points); } catch (error) {
|
|
32
|
-
try { fit = fitPulseCalibration(points); } catch (error) {
|
|
30
|
+
const warningDetails = [];
|
|
31
|
+
if (coherence < 0.85) warningDetails.push(createWarning("PHASE_INCOHERENT"));
|
|
32
|
+
try { measured = signedPulseCandidates(points); } catch (error) { warningDetails.push(warningFromError(error)); }
|
|
33
|
+
try { fit = fitPulseCalibration(points); } catch (error) { warningDetails.push(warningFromError(error)); }
|
|
33
34
|
let magnitudeFit = null;
|
|
34
35
|
try { magnitudeFit = fitPulseCalibration(points.map(point => ({length:point.length,amplitude:point.magnitude})), {magnitude:true}); }
|
|
35
|
-
catch (error) {
|
|
36
|
-
if (fit && (fit.p180 > Math.max(...points.map(point => point.length)) || fit.p90 < Math.min(...points.map(point => point.length))))
|
|
37
|
-
if (fit && fit.r2 < 0.85)
|
|
38
|
-
if (magnitudeFit && magnitudeFit.r2 < 0.85)
|
|
39
|
-
return { ...result, measured, fit, magnitudeFit, warnings };
|
|
36
|
+
catch (error) { warningDetails.push(warningFromError(error)); }
|
|
37
|
+
if (fit && (fit.p180 > Math.max(...points.map(point => point.length)) || fit.p90 < Math.min(...points.map(point => point.length)))) warningDetails.push(createWarning("PULSE_FIT_EXTRAPOLATED"));
|
|
38
|
+
if (fit && fit.r2 < 0.85) warningDetails.push(createWarning("PULSE_FIT_LOW_AGREEMENT"));
|
|
39
|
+
if (magnitudeFit && magnitudeFit.r2 < 0.85) warningDetails.push(createWarning("MAGNITUDE_FIT_LOW_AGREEMENT"));
|
|
40
|
+
return { ...result, measured, fit, magnitudeFit, warnings: warningDetails.map(warning => warning.message), warningDetails };
|
|
40
41
|
}
|
package/src/record.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
import { SAMPLE_COUNT, SAMPLE_INTERVAL_MS, validateParameters } from "./protocol.js";
|
|
2
3
|
import { validateSamples } from "./sample-validation.js";
|
|
3
4
|
|
|
@@ -5,33 +6,33 @@ export const MAX_MEASUREMENT_JSON_BYTES = 2 * 1024 * 1024;
|
|
|
5
6
|
|
|
6
7
|
/** Parse a saved NMRLink record; discard derived fields and never coerce sample values. */
|
|
7
8
|
export function parseMeasurement(text) {
|
|
8
|
-
if (typeof text !== "string") throw
|
|
9
|
+
if (typeof text !== "string") throw createError(TypeError, "INVALID_RECORD_TEXT");
|
|
9
10
|
if (new TextEncoder().encode(text).length > MAX_MEASUREMENT_JSON_BYTES) {
|
|
10
|
-
throw
|
|
11
|
+
throw createError(RangeError, "RECORD_TOO_LARGE");
|
|
11
12
|
}
|
|
12
13
|
let value;
|
|
13
14
|
try { value = JSON.parse(text.replace(/^\uFEFF/, "")); }
|
|
14
|
-
catch { throw
|
|
15
|
+
catch (cause) { throw createError(TypeError, "INVALID_JSON", {}, { cause }); }
|
|
15
16
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
16
|
-
throw
|
|
17
|
+
throw createError(TypeError, "INVALID_RECORD");
|
|
17
18
|
}
|
|
18
19
|
if (!["instrument", "demo"].includes(value.source) || !["single", "double"].includes(value.mode)) {
|
|
19
|
-
throw
|
|
20
|
+
throw createError(TypeError, "INVALID_RECORD_KIND");
|
|
20
21
|
}
|
|
21
22
|
const validTime = typeof value.acquiredAt === "string" && /^\d{4}-\d{2}-\d{2}T/.test(value.acquiredAt) && Number.isFinite(Date.parse(value.acquiredAt));
|
|
22
23
|
// Date.parse normalizes dates such as February 30; reject that normalization.
|
|
23
24
|
if (!validTime || new Date(value.acquiredAt.slice(0, 10) + "T00:00:00Z").toISOString().slice(0, 10) !== value.acquiredAt.slice(0, 10)) {
|
|
24
|
-
throw
|
|
25
|
+
throw createError(TypeError, "INVALID_ACQUISITION_TIME");
|
|
25
26
|
}
|
|
26
27
|
if (value.points !== SAMPLE_COUNT || value.raw?.length !== SAMPLE_COUNT || value.sampleIntervalMs !== SAMPLE_INTERVAL_MS) {
|
|
27
|
-
throw
|
|
28
|
+
throw createError(RangeError, "INVALID_RECORD_SAMPLING");
|
|
28
29
|
}
|
|
29
30
|
try { validateSamples(value.raw); }
|
|
30
|
-
catch { throw
|
|
31
|
+
catch (cause) { throw createError(TypeError, "INVALID_RECORD_CHANNELS", {}, { cause }); }
|
|
31
32
|
let parameters = null;
|
|
32
33
|
if (value.source !== "demo" || value.parameters !== null) {
|
|
33
34
|
try { parameters = validateParameters(value.parameters); }
|
|
34
|
-
catch { throw
|
|
35
|
+
catch (cause) { throw createError(TypeError, "INVALID_RECORD_PARAMETERS", {}, { cause }); }
|
|
35
36
|
}
|
|
36
37
|
return {
|
|
37
38
|
source: value.source, acquiredAt: value.acquiredAt, mode: value.mode, parameters,
|
package/src/relaxation-fit.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
function meanRelaxationPoints(points) {
|
|
2
3
|
const groups = /* @__PURE__ */ new Map();
|
|
3
4
|
for (const p of points) {
|
|
4
|
-
if (!Number.isFinite(p.tau) || !Number.isFinite(p.signal)) throw
|
|
5
|
+
if (!Number.isFinite(p.tau) || !Number.isFinite(p.signal)) throw createError(Error, "INVALID_RELAXATION_SIGNAL");
|
|
5
6
|
const g = groups.get(p.tau) || { tau: p.tau, signal: 0, count: 0 };
|
|
6
7
|
g.signal += p.signal;
|
|
7
8
|
g.count++;
|
|
@@ -13,13 +14,13 @@ function relaxationValue(kind, fit, x) {
|
|
|
13
14
|
return kind === "t1" ? fit.A - fit.B * Math.exp(-x / fit.T) : fit.A * Math.exp(-x / fit.T);
|
|
14
15
|
}
|
|
15
16
|
function fitRelaxation(kind, points, from, to, { relaxed = false } = {}) {
|
|
16
|
-
if (!["t1", "t2"].includes(kind) || ![from, to].every(Number.isFinite) || from >= to) throw
|
|
17
|
+
if (!["t1", "t2"].includes(kind) || ![from, to].every(Number.isFinite) || from >= to) throw createError(Error, "INVALID_FIT_RANGE");
|
|
17
18
|
const p = meanRelaxationPoints(points).map((q) => ({ ...q, x: kind === "t1" ? q.tau : 2 * q.tau })).filter((q) => q.x >= from && q.x <= to);
|
|
18
|
-
if (p.length < (relaxed ? 3 : 5)) throw
|
|
19
|
+
if (p.length < (relaxed ? 3 : 5)) throw createError(Error, "INSUFFICIENT_RELAXATION_POINTS");
|
|
19
20
|
const scale = Math.max(...p.map((q) => Math.abs(q.signal))), low = Math.min(...p.map((q) => q.signal)), high = Math.max(...p.map((q) => q.signal));
|
|
20
|
-
if (scale < 1e-9 || high - low < scale * 1e-4) throw
|
|
21
|
-
if (kind === "t1" && !(low < 0 && high > 0)) throw
|
|
22
|
-
if (kind === "t2" && low < 0) throw
|
|
21
|
+
if (scale < 1e-9 || high - low < scale * 1e-4) throw createError(Error, "FLAT_RELAXATION_SIGNAL");
|
|
22
|
+
if (kind === "t1" && !(low < 0 && high > 0)) throw createError(Error, "T1_SIGN_CHANGE_REQUIRED");
|
|
23
|
+
if (kind === "t2" && low < 0) throw createError(Error, "NEGATIVE_ECHO_SIGNAL");
|
|
23
24
|
const n = p.length, sy = p.reduce((s, q) => s + q.signal / scale, 0);
|
|
24
25
|
function solve(logT) {
|
|
25
26
|
const T = Math.exp(logT), g = p.map((q) => Math.exp(-q.x / T)), sg = g.reduce((s, q) => s + q, 0), sgg = g.reduce((s, q) => s + q * q, 0), sgy = g.reduce((s, q, i) => s + q * p[i].signal / scale, 0), den = n * sgg - sg * sg;
|
|
@@ -43,7 +44,7 @@ function fitRelaxation(kind, points, from, to, { relaxed = false } = {}) {
|
|
|
43
44
|
index = i;
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
|
-
if (index <= 0 || index >= steps) throw
|
|
47
|
+
if (index <= 0 || index >= steps) throw createError(Error, "RELAXATION_FIT_BOUNDARY");
|
|
47
48
|
let l = lo + (hi - lo) * (index - 1) / steps, h = lo + (hi - lo) * (index + 1) / steps;
|
|
48
49
|
for (let i = 0; i < 40; i++) {
|
|
49
50
|
const a = l + (h - l) / 3, b = h - (h - l) / 3;
|
|
@@ -52,8 +53,8 @@ function fitRelaxation(kind, points, from, to, { relaxed = false } = {}) {
|
|
|
52
53
|
}
|
|
53
54
|
best = solve((l + h) / 2);
|
|
54
55
|
const mean = sy / n, sst = p.reduce((s, q) => s + (q.signal / scale - mean) ** 2, 0), r2 = 1 - best.sse / sst;
|
|
55
|
-
if (!Number.isFinite(r2) || (!relaxed && r2 < 0.95)) throw
|
|
56
|
-
if (!relaxed && span / best.T < 0.8) throw
|
|
56
|
+
if (!Number.isFinite(r2) || (!relaxed && r2 < 0.95)) throw createError(Error, "RELAXATION_FIT_LOW_R2");
|
|
57
|
+
if (!relaxed && span / best.T < 0.8) throw createError(Error, "RELAXATION_RANGE_TOO_SHORT");
|
|
57
58
|
return { ...best, sse: best.sse * scale * scale, r2, rmse: Math.sqrt(best.sse / n) * scale, n, from: p[0].x, to: p.at(-1).x, model: kind === "t1" ? "A-B*exp(-tau/T1)" : "A*exp(-2*tau/T2)" };
|
|
58
59
|
}
|
|
59
60
|
function fitT1(points, { fromMs, toMs }) {
|
package/src/sample-validation.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
/** Validate plain two-channel sample arrays without coercing corrupt values. */
|
|
2
3
|
export function validateSamples(samples) {
|
|
3
4
|
if (!Array.isArray(samples) || samples.length === 0) {
|
|
4
|
-
throw
|
|
5
|
+
throw createError(TypeError, "INVALID_SAMPLES");
|
|
5
6
|
}
|
|
6
7
|
for (let index = 0; index < samples.length; index++) {
|
|
7
8
|
const pair = samples[index];
|
|
8
9
|
if (!Array.isArray(pair) || pair.length !== 2 ||
|
|
9
10
|
!Number.isFinite(pair[0]) || !Number.isFinite(pair[1])) {
|
|
10
|
-
throw
|
|
11
|
+
throw createError(TypeError, "INVALID_SAMPLE", { index });
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
}
|
package/src/signed-signal.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
import { analyzeMeasurement } from "./analysis.js";
|
|
2
3
|
|
|
3
4
|
/** Complex projection at one common frequency, with the selected window's time origin. */
|
|
@@ -19,33 +20,33 @@ export function complexAt(data, spectrum, intervalMs, hz) {
|
|
|
19
20
|
export function analyzeSignedRecords(records, {
|
|
20
21
|
analysis = {}, referenceIndex = null, phaseDegrees = 0, field = "pulse1Cycles", mode = "single",
|
|
21
22
|
} = {}) {
|
|
22
|
-
if (!Array.isArray(records) || !records.length) throw
|
|
23
|
-
if (!Number.isFinite(phaseDegrees)) throw
|
|
23
|
+
if (!Array.isArray(records) || !records.length) throw createError(Error, "NO_RECORDS");
|
|
24
|
+
if (!Number.isFinite(phaseDegrees)) throw createError(Error, "INVALID_PHASE");
|
|
24
25
|
const first = records[0];
|
|
25
26
|
for (const record of records) {
|
|
26
27
|
if (record.mode !== mode || record.source !== first.source || record.sampleIntervalMs !== first.sampleIntervalMs ||
|
|
27
28
|
Object.keys(first.parameters).some(key => key !== field && record.parameters[key] !== first.parameters[key])) {
|
|
28
|
-
throw
|
|
29
|
+
throw createError(Error, "INCONSISTENT_RECORDS");
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
const minHz = analysis.minHz ?? -3000, maxHz = analysis.maxHz ?? 3000;
|
|
32
|
-
if (![minHz, maxHz].every(Number.isFinite) || minHz >= maxHz) throw
|
|
33
|
+
if (![minHz, maxHz].every(Number.isFinite) || minHz >= maxHz) throw createError(Error, "INVALID_SEARCH_BAND");
|
|
33
34
|
const prepared = records.map(record => ({ record, ...analyzeMeasurement(record, analysis) }));
|
|
34
35
|
const grid = prepared[0].spectrum.bins.filter(bin => bin.hz >= minHz && bin.hz <= maxHz).map(bin => bin.hz);
|
|
35
|
-
if (!grid.length) throw
|
|
36
|
+
if (!grid.length) throw createError(Error, "NO_FFT_BINS");
|
|
36
37
|
const counts = new Map();
|
|
37
38
|
for (const record of records) counts.set(record.parameters[field], (counts.get(record.parameters[field]) ?? 0) + 1);
|
|
38
39
|
const power = new Float64Array(grid.length);
|
|
39
40
|
for (const item of prepared) {
|
|
40
41
|
const bins = item.spectrum.bins.filter(bin => bin.hz >= minHz && bin.hz <= maxHz);
|
|
41
|
-
if (bins.length !== grid.length || bins.some((bin, index) => bin.hz !== grid[index])) throw
|
|
42
|
+
if (bins.length !== grid.length || bins.some((bin, index) => bin.hz !== grid[index])) throw createError(Error, "INCONSISTENT_FFT_GRID");
|
|
42
43
|
bins.forEach((bin, index) => { power[index] += bin.amplitude ** 2 / counts.get(item.record.parameters[field]); });
|
|
43
44
|
}
|
|
44
45
|
const strongestBin = power.reduce((best, value, index) => value > power[best] ? index : best, 0);
|
|
45
46
|
const hz = grid[strongestBin];
|
|
46
47
|
const values = prepared.map(item => ({ ...complexAt(item.data, item.spectrum, item.record.sampleIntervalMs, hz), record: item.record }));
|
|
47
48
|
const maximum = Math.max(...values.map(value => Math.hypot(value.real, value.imaginary)));
|
|
48
|
-
if (maximum < 1e-9) throw
|
|
49
|
+
if (maximum < 1e-9) throw createError(Error, "NO_REFERENCE_SIGNAL");
|
|
49
50
|
const strong = values.filter(value => Math.hypot(value.real, value.imaginary) >= maximum * 0.25);
|
|
50
51
|
let c = 0, s = 0, total = 0;
|
|
51
52
|
for (const value of strong) {
|
|
@@ -56,9 +57,9 @@ export function analyzeSignedRecords(records, {
|
|
|
56
57
|
}
|
|
57
58
|
let phaseRad = 0.5 * Math.atan2(s, c);
|
|
58
59
|
if (referenceIndex !== null) {
|
|
59
|
-
if (!Number.isInteger(referenceIndex) || !values[referenceIndex]) throw
|
|
60
|
+
if (!Number.isInteger(referenceIndex) || !values[referenceIndex]) throw createError(Error, "INVALID_REFERENCE_INDEX");
|
|
60
61
|
const ref = values[referenceIndex];
|
|
61
|
-
if (Math.hypot(ref.real, ref.imaginary) < 1e-9) throw
|
|
62
|
+
if (Math.hypot(ref.real, ref.imaginary) < 1e-9) throw createError(Error, "ZERO_REFERENCE_SIGNAL");
|
|
62
63
|
phaseRad = Math.atan2(ref.imaginary, ref.real);
|
|
63
64
|
} else {
|
|
64
65
|
const earliest = strong.reduce((best, value) => value.record.parameters[field] < best.record.parameters[field] ? value : best);
|
package/src/sweep.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
import { validateParameters, SAMPLE_COUNT, SAMPLE_INTERVAL_MS } from "./protocol.js";
|
|
2
3
|
|
|
3
4
|
/** Shared integer grid for frequency and pulse scans. */
|
|
4
5
|
export function integerSteps(start, end, step, maximum) {
|
|
5
6
|
if (![start, end, step].every(Number.isInteger) || start < 1 || end > maximum || end < start || step < 1) {
|
|
6
|
-
throw
|
|
7
|
+
throw createError(RangeError, "INVALID_SWEEP_RANGE", { maximum });
|
|
7
8
|
}
|
|
8
9
|
const count = Math.floor((end - start) / step) + 1;
|
|
9
|
-
if (count > 201) throw
|
|
10
|
+
if (count > 201) throw createError(RangeError, "SWEEP_TOO_MANY_POINTS");
|
|
10
11
|
return Array.from({ length: count }, (_, index) => start + index * step);
|
|
11
12
|
}
|
|
12
13
|
|
|
@@ -16,7 +17,7 @@ export async function runSingleSweep(device, parameters, {
|
|
|
16
17
|
}) {
|
|
17
18
|
const common = validateParameters(parameters);
|
|
18
19
|
const planned = values.map(value => validateParameters({ ...common, [field]: value }));
|
|
19
|
-
if (!["single", "double"].includes(mode)) throw
|
|
20
|
+
if (!["single", "double"].includes(mode)) throw createError(TypeError, "INVALID_MODE");
|
|
20
21
|
planned.forEach(validateMeasurement);
|
|
21
22
|
const settings = { ...analysis };
|
|
22
23
|
evaluate({ parameters: common, sampleIntervalMs: SAMPLE_INTERVAL_MS, raw: Array.from({ length: SAMPLE_COUNT }, () => [0, 0]) }, settings);
|
package/src/t1-record.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createError } from "./errors.js";
|
|
1
2
|
import { parseMeasurement } from "./record.js";
|
|
2
3
|
import { validateParameters } from "./protocol.js";
|
|
3
4
|
import { t1Delays, evaluateT1, analyzeT1 } from "./t1-sweep.js";
|
|
@@ -6,37 +7,37 @@ export const MAX_T1_JSON_BYTES = 32 * 1024 * 1024;
|
|
|
6
7
|
|
|
7
8
|
/** Restore a complete or stopped T1 scan; recompute derived data from raw samples. */
|
|
8
9
|
export function parseT1Measurement(text) {
|
|
9
|
-
if (typeof text !== "string" || new TextEncoder().encode(text).length > MAX_T1_JSON_BYTES) throw
|
|
10
|
+
if (typeof text !== "string" || new TextEncoder().encode(text).length > MAX_T1_JSON_BYTES) throw createError(TypeError, "ARCHIVE_TOO_LARGE");
|
|
10
11
|
let saved;
|
|
11
12
|
try { saved = JSON.parse(text.replace(/^\uFEFF/, "")); }
|
|
12
|
-
catch { throw
|
|
13
|
+
catch (cause) { throw createError(TypeError, "INVALID_JSON", {}, { cause }); }
|
|
13
14
|
if (saved?.kind !== "t1-measurement" || !Array.isArray(saved.points) || !saved.points.length || saved.points.length > 201) {
|
|
14
|
-
throw
|
|
15
|
+
throw createError(TypeError, "INVALID_T1_ARCHIVE");
|
|
15
16
|
}
|
|
16
17
|
const s = saved.settings;
|
|
17
|
-
if (!s) throw
|
|
18
|
+
if (!s) throw createError(TypeError, "MISSING_SWEEP_SETTINGS");
|
|
18
19
|
const delays = t1Delays(s.startMs, s.endMs, s.stepMs);
|
|
19
20
|
const parameters = validateParameters(s.parameters);
|
|
20
21
|
const a = s.analysis;
|
|
21
|
-
if (!a || ![a.startMs, a.endMs, a.tailPoints, a.minHz, a.maxHz].every(Number.isFinite) || !["hann", "rect"].includes(a.window)) throw
|
|
22
|
+
if (!a || ![a.startMs, a.endMs, a.tailPoints, a.minHz, a.maxHz].every(Number.isFinite) || !["hann", "rect"].includes(a.window)) throw createError(TypeError, "INVALID_ANALYSIS_OPTIONS");
|
|
22
23
|
const analysis = { startMs: a.startMs, endMs: a.endMs, tailPoints: a.tailPoints, minHz: a.minHz, maxHz: a.maxHz, window: a.window };
|
|
23
24
|
const points = saved.points.map((point, index) => {
|
|
24
25
|
const record = parseMeasurement(JSON.stringify(point?.record));
|
|
25
26
|
if (!record.parameters || record.mode !== "double" || record.source !== saved.source || point.tau !== delays[index] || record.parameters.pulseIntervalMs !== delays[index] ||
|
|
26
|
-
Object.keys(parameters).some(key => key !== "pulseIntervalMs" && parameters[key] !== record.parameters[key])) throw
|
|
27
|
+
Object.keys(parameters).some(key => key !== "pulseIntervalMs" && parameters[key] !== record.parameters[key])) throw createError(TypeError, "SWEEP_RECORD_MISMATCH");
|
|
27
28
|
return evaluateT1(record, analysis);
|
|
28
29
|
});
|
|
29
30
|
const referenceIndex = saved.result?.reference?.referenceIndex ?? null;
|
|
30
31
|
const phaseDegrees = saved.result?.reference?.phaseDegrees ?? 0;
|
|
31
32
|
const fromMs = saved.result?.fromMs ?? s.startMs, toMs = saved.result?.toMs ?? s.endMs;
|
|
32
|
-
if (![fromMs, toMs, phaseDegrees].every(Number.isFinite) || fromMs >= toMs) throw
|
|
33
|
-
if (referenceIndex !== null && (!Number.isInteger(referenceIndex) || referenceIndex < 0 || referenceIndex >= points.length)) throw
|
|
33
|
+
if (![fromMs, toMs, phaseDegrees].every(Number.isFinite) || fromMs >= toMs) throw createError(TypeError, "INVALID_FIT_PHASE");
|
|
34
|
+
if (referenceIndex !== null && (!Number.isInteger(referenceIndex) || referenceIndex < 0 || referenceIndex >= points.length)) throw createError(TypeError, "INVALID_REFERENCE_INDEX");
|
|
34
35
|
let result = null;
|
|
35
36
|
try { result = analyzeT1(points.map(point => point.record), { analysis, referenceIndex, phaseDegrees, fromMs, toMs }); }
|
|
36
37
|
catch (error) {
|
|
37
38
|
// A valid zero-signal scan is still useful for inspecting raw waveforms.
|
|
38
39
|
if (error.code !== "NO_REFERENCE_SIGNAL") throw error;
|
|
39
40
|
}
|
|
40
|
-
if (typeof saved.acquiredAt !== "string" || !Number.isFinite(Date.parse(saved.acquiredAt))) throw
|
|
41
|
+
if (typeof saved.acquiredAt !== "string" || !Number.isFinite(Date.parse(saved.acquiredAt))) throw createError(TypeError, "INVALID_SWEEP_TIME");
|
|
41
42
|
return { source: saved.source, acquiredAt: saved.acquiredAt, settings: { startMs: s.startMs, endMs: s.endMs, stepMs: s.stepMs, delays, parameters, analysis }, points, result, referenceIndex, phaseDegrees, fromMs, toMs };
|
|
42
43
|
}
|
package/src/t1-sweep.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createError, createWarning, warningFromError } from "./errors.js";
|
|
1
2
|
import { integerSteps, runSingleSweep } from "./sweep.js";
|
|
2
3
|
import { evaluateFrequency } from "./frequency-sweep.js";
|
|
3
4
|
import { validateParameters, SAMPLE_COUNT, SAMPLE_INTERVAL_MS } from "./protocol.js";
|
|
@@ -11,7 +12,7 @@ export function t1Delays(startMs, endMs, stepMs) {
|
|
|
11
12
|
/** PoC inversion recovery: 180°(0°), tau, 90°(0°), then acquire FID. */
|
|
12
13
|
export function t1Parameters({ frequencyHz, p90Cycles, p180Cycles, measurementIntervalMs, scans }, tauMs) {
|
|
13
14
|
if (!Number.isInteger(p90Cycles) || !Number.isInteger(p180Cycles) || p90Cycles < 1 || p180Cycles <= p90Cycles) {
|
|
14
|
-
throw
|
|
15
|
+
throw createError(RangeError, "INVALID_CALIBRATED_PULSES");
|
|
15
16
|
}
|
|
16
17
|
const parameters = validateParameters({ frequencyHz, pulse1Cycles: p180Cycles, phase1Degrees: 0,
|
|
17
18
|
pulse2Cycles: p90Cycles, phase2Degrees: 0, pulseIntervalMs: tauMs, measurementIntervalMs, scans });
|
|
@@ -20,12 +21,12 @@ export function t1Parameters({ frequencyHz, p90Cycles, p180Cycles, measurementIn
|
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
function validateT1Parameters(parameters) {
|
|
23
|
-
if (!Number.isInteger(parameters.pulseIntervalMs) || parameters.pulseIntervalMs < 1) throw
|
|
24
|
+
if (!Number.isInteger(parameters.pulseIntervalMs) || parameters.pulseIntervalMs < 1) throw createError(RangeError, "INVALID_TAU");
|
|
24
25
|
if (parameters.phase1Degrees !== 0 || parameters.phase2Degrees !== 0 || parameters.pulse2Cycles < 1 || parameters.pulse1Cycles <= parameters.pulse2Cycles) {
|
|
25
|
-
throw
|
|
26
|
+
throw createError(RangeError, "INVALID_T1_SEQUENCE");
|
|
26
27
|
}
|
|
27
28
|
if (parameters.measurementIntervalMs <= parameters.pulseIntervalMs + SAMPLE_COUNT * SAMPLE_INTERVAL_MS) {
|
|
28
|
-
throw
|
|
29
|
+
throw createError(RangeError, "REPETITION_TOO_SHORT");
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
|
|
@@ -33,7 +34,7 @@ export function evaluateT1(record, analysis = {}) {
|
|
|
33
34
|
const start = analysis.startMs ?? 1, end = analysis.endMs ?? 5;
|
|
34
35
|
if (![start, end].every(Number.isFinite) || end - start < 4 * SAMPLE_INTERVAL_MS - 1e-9 ||
|
|
35
36
|
(analysis.minHz ?? -3000) < -25000 || (analysis.maxHz ?? 3000) > 25000) {
|
|
36
|
-
throw
|
|
37
|
+
throw createError(RangeError, "INVALID_RELAXATION_ANALYSIS");
|
|
37
38
|
}
|
|
38
39
|
const point = evaluateFrequency(record, analysis);
|
|
39
40
|
return { tau: record.parameters.pulseIntervalMs, amplitude: point.amplitude, peakHz: point.peakHz, record };
|
|
@@ -47,20 +48,20 @@ export async function runT1Sweep(device, parameters, { startMs, endMs, stepMs, a
|
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
export function analyzeT1(records, { analysis = {}, referenceIndex = null, phaseDegrees = 0, fromMs, toMs } = {}) {
|
|
50
|
-
if (!Array.isArray(records) || !records.length) throw
|
|
51
|
+
if (!Array.isArray(records) || !records.length) throw createError(Error, "NO_RECORDS");
|
|
51
52
|
records.forEach(record => { validateT1Parameters(validateParameters(record.parameters)); evaluateT1(record, analysis); });
|
|
52
53
|
const result = analyzeSignedRecords(records, { analysis, referenceIndex, phaseDegrees, field: "pulseIntervalMs", mode: "double" });
|
|
53
54
|
const points = result.points.map(({ length, amplitude, ...point }) => ({ ...point, tau: length, signal: amplitude }));
|
|
54
55
|
const range = { fromMs: fromMs ?? Math.min(...points.map(point => point.tau)), toMs: toMs ?? Math.max(...points.map(point => point.tau)) };
|
|
55
|
-
const
|
|
56
|
+
const warningDetails = [];
|
|
56
57
|
let fit = null;
|
|
57
|
-
if (result.reference.coherence < 0.85)
|
|
58
|
+
if (result.reference.coherence < 0.85) warningDetails.push(createWarning("T1_PHASE_INCOHERENT"));
|
|
58
59
|
else {
|
|
59
60
|
try {
|
|
60
61
|
fit = fitT1(points, range);
|
|
61
|
-
if (fit.to < 3 * fit.T)
|
|
62
|
+
if (fit.to < 3 * fit.T) warningDetails.push(createWarning("T1_INCOMPLETE_RECOVERY"));
|
|
62
63
|
}
|
|
63
|
-
catch (error) {
|
|
64
|
+
catch (error) { warningDetails.push(warningFromError(error)); }
|
|
64
65
|
}
|
|
65
|
-
return { points, reference: result.reference, fit, warnings, ...range };
|
|
66
|
+
return { points, reference: result.reference, fit, warnings: warningDetails.map(warning => warning.message), warningDetails, ...range };
|
|
66
67
|
}
|