Skip to content

analyze_scope

Kamil Brylew edited this page Sep 28, 2026 · 2 revisions

Scope Waveform Analysis

Native NLab Scope recordings are fixed-frame NDMA binaries. The application can also browse waveform-bearing CAEN CoMPASS files and can convert compatible NDMA v1 files to HDF5. Start with the GUI workbench unless a reproducible script is required.

Recommended GUI workflow

Open Tools → Waveform Analysis Workbench… and select a native Scope NDMA or waveform-bearing CAEN binary. The workbench:

  • memory-maps fixed-frame NDMA without loading the capture into RAM;
  • builds a bounded background offset index for variable-length CAEN records;
  • copies only the selected waveform for display;
  • supports baseline removal, polarity selection, draggable integration gates, PSD reconstruction, projections, and bounded batch processing;
  • never starts, stops, or reconfigures live Scope acquisition.

For CAEN data, set the ADC sample period explicitly because the file does not store it. For NDMA, retain the capture configuration: NDMA v1 contains frame length and timestamps but no explicit trigger-mode or decimation metadata.

Inspect a native file in Python

Use the project reader rather than hand-written byte offsets:

from pathlib import Path

from nlab.analysis.waveform_file import (
    MappedWaveformFile,
    build_waveform_file_index,
    inspect_waveform_file,
)

path = Path("measurements/scope.bin")
summary = inspect_waveform_file(path)
index = build_waveform_file_index(path)

print(summary.format_name, summary.channel)
print("frames:", index.frame_count)
print("stored sample period:", index.sample_period_ns)

mapped = MappedWaveformFile(index)
try:
    frame = mapped.frame(0)
    print("timestamp:", frame.timestamp)
    print("samples:", frame.samples.shape, frame.samples.dtype)
finally:
    mapped.close()

build_waveform_file_index() rejects unsupported files, ignores only a logged trailing incomplete record, and requires at least one complete frame.

Bounded waveform statistics

Do not materialize a multi-gigabyte capture as one NumPy array. Read bounded batches and accumulate statistics:

import numpy as np

mapped = MappedWaveformFile(index)
try:
    first = mapped.frame(0)
    sample_count = len(first.samples)
    selected = min(index.frame_count, 10_000)
    batch = mapped.batch(
        0,
        selected,
        sample_count=sample_count,
        stride=1,
    )
    waveforms = batch.samples[batch.complete]

    frame_mean = waveforms.mean(axis=1)
    frame_minimum = waveforms.min(axis=1)
    print("accepted frames:", len(waveforms))
    print("mean baseline:", float(frame_mean.mean()))
    print("minimum range:", int(frame_minimum.min()), int(frame_minimum.max()))
finally:
    mapped.close()

For large studies, repeat over disjoint batches and retain running sums, histograms, or selected examples rather than concatenating every waveform.

Convert NDMA to HDF5

Use File → Convert Binary to HDF5…. The converter validates the NDMA header and refuses a payload with a trailing partial frame. The result contains:

Object Meaning
/waveforms int16 waveform matrix, one row per complete frame
/timestamps frame timestamps in 8 ns ticks
/time_ns converter-provided waveform-relative axis
attributes source path, format version, channel, capture time, frame geometry, total frames

A compact HDF5 analysis looks like:

from pathlib import Path

import h5py
import numpy as np

path = Path("measurements/scope.h5")
with h5py.File(path, "r") as file:
    waveforms = file["waveforms"]
    timestamps = file["timestamps"][:]
    time_ns = file["time_ns"][:]

    # Read a bounded subset, not the complete dataset.
    count = min(len(waveforms), 10_000)
    block = np.asarray(waveforms[:count])
    baseline = block.mean(axis=1)

print("frames analysed:", len(block))
print("baseline mean/std:", float(baseline.mean()), float(baseline.std()))
print("time axis:", float(time_ns[0]), float(time_ns[-1]), "ns")

Timestamp spacing and coverage

Frame timestamps use 8 ns ticks. Check for duplicate/backwards transitions and summarize positive differences before estimating coverage:

delta_ticks = np.diff(timestamps.astype(np.uint64))
positive = delta_ticks > 0

print("non-forward transitions:", int((~positive).sum()))
if positive.any():
    delta_ns = delta_ticks[positive].astype(np.float64) * 8.0
    print("interval percentiles [ns]:", np.percentile(delta_ns, [0, 50, 95, 99, 100]))

Coverage is transported waveform duration divided by observed timestamp span. Use the capture’s qualified sample geometry/configuration; NDMA v1 does not encode trigger mode or decimation. Coverage is not the fraction of requested trigger opportunities accepted.

The repository’s notebooks/check_dma.py performs the native NDMA alignment, timestamp, and coverage checks used for capture diagnostics. Set its optional trigger label when comparing files because NDMA v1 does not store that mode.

PSD from waveforms

The GUI workbench calculates baseline-corrected Qshort, Qlong, and:

PSD = (Qlong - Qshort) / Qlong

Baseline range, polarity, and integration gates are analysis inputs, not file facts. Save or report them with every result. If stored CAEN gate sums exist, the workbench reports their scale relative to computed gates as a consistency check; it does not silently force them to agree.

Common mistakes

  • Treating frame_samples as bytes; it is an int16 word count.
  • Including the four timestamp words as waveform samples.
  • Accepting a trailing partial record.
  • Assuming NDMA v1 stores trigger mode or decimation.
  • Loading all frames into memory for a statistic that can be accumulated.
  • Interpreting waveform ADC codes as volts or current without calibration.

See Capture Files and Integrity and Analysis Tools for the broader workflow.

Clone this wiki locally