Skip to content

Match make_trapezoid to Pulseq MATLAB output#411

Open
simonthalen wants to merge 1 commit into
pulseq:masterfrom
simonthalen:make-trapezoid-matlab-parity
Open

Match make_trapezoid to Pulseq MATLAB output#411
simonthalen wants to merge 1 commit into
pulseq:masterfrom
simonthalen:make-trapezoid-matlab-parity

Conversation

@simonthalen

Copy link
Copy Markdown

Summary

This PR updates make_trapezoid(...) to match MATLAB Pulseq makeTrapezoid(...) for equivalent inputs.

The main behavioral fix is the area + duration path. PyPulseq previously calculated shortest timing first, rounded ramp times, and then solved the requested duration. MATLAB Pulseq first solves the quadratic amplitude for the requested area/duration, rounds the ramp time from that amplitude, and finally recalculates amplitude after raster rounding. That produces different ramp/flat timing and amplitude for the same input.

Problem

For the same nominal input:

system = pp.Opts(max_grad=100, grad_unit='mT/m', max_slew=100, slew_unit='T/m/s')
gx = pp.make_trapezoid(channel='x', area=250, duration=1e-3, system=system)

PyPulseq and Pulseq produces different trapezoids.

trapezoid_comparison

As PyPulseq is described as a translation of Pulseq, this is a bug.

What Changed

  • Changes the area + duration branch to match Pulseq's quadratic solve plus post-raster amplitude recalculation.
  • Derives the rasterized rise and fall times from the solved amplitude, using PyPulseq's existing rise-time helper.
  • Keeps the existing make_trapezoid(...) API and all unrelated validation behavior unchanged.
  • Adds a MATLAB reference regression test and updates the existing affected expectation.

Behavior Compatibility

This is a bug-fix change limited to area + duration when ramp times are not supplied. That path now returns MATLAB-compatible timing and amplitude; other input combinations retain their existing PyPulseq behavior.

Reference Output

The regression test anchors this Pulseq-compatible output:

amplitude=268817.20430107525
rise_time=7.000000000000001e-05
flat_time=0.0008600000000000001
fall_time=7.000000000000001e-05
area=250.0
flat_area=231.18279569892474

Testing

PYTHONPATH=src python -m pytest tests/test_make_trapezoid.py
11 passed

python -m ruff check src/pypulseq/make_trapezoid.py tests/test_make_trapezoid.py
All checks passed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant