torchspin 0.4.0__tar.gz → 0.6.0__tar.gz

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.
Files changed (127) hide show
  1. {torchspin-0.4.0 → torchspin-0.6.0}/CHANGELOG.md +403 -1
  2. {torchspin-0.4.0 → torchspin-0.6.0}/CITATION.cff +11 -4
  3. {torchspin-0.4.0 → torchspin-0.6.0}/PKG-INFO +9 -4
  4. {torchspin-0.4.0 → torchspin-0.6.0}/README.md +8 -3
  5. {torchspin-0.4.0 → torchspin-0.6.0}/pyproject.toml +1 -1
  6. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/__init__.py +1 -1
  7. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/rfmixer.py +8 -4
  8. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/saffron.py +151 -24
  9. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/saffron_peaks.py +5 -5
  10. torchspin-0.6.0/torchspin/saffron_thyme.py +605 -0
  11. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/signalprocessing.py +35 -14
  12. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/spidyan.py +155 -61
  13. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin.egg-info/PKG-INFO +9 -4
  14. torchspin-0.4.0/torchspin/saffron_thyme.py +0 -472
  15. {torchspin-0.4.0 → torchspin-0.6.0}/LICENSE.md +0 -0
  16. {torchspin-0.4.0 → torchspin-0.6.0}/MANIFEST.in +0 -0
  17. {torchspin-0.4.0 → torchspin-0.6.0}/setup.cfg +0 -0
  18. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/_cardamom_istos.py +0 -0
  19. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/_cardamom_propagatedm.py +0 -0
  20. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/_cardamom_utils.py +0 -0
  21. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/_compile.py +0 -0
  22. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/_linalg.py +0 -0
  23. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/angmom.py +0 -0
  24. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/autograd.py +0 -0
  25. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/autoguess.py +0 -0
  26. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/batch.py +0 -0
  27. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/blochsteady.py +0 -0
  28. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/cardamom.py +0 -0
  29. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/chili.py +0 -0
  30. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/chili_sle.py +0 -0
  31. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/constants.py +0 -0
  32. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/convspec.py +0 -0
  33. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ctafft.py +0 -0
  34. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/curry.py +0 -0
  35. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/data/FourierSeriesCoefficients.txt +0 -0
  36. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/data/GaussianCascadeCoefficients.txt +0 -0
  37. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/data/isotopedata.txt +0 -0
  38. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/data/spacegroups.txt +0 -0
  39. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/dataproc.py +0 -0
  40. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/dipbackground.py +0 -0
  41. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/dipkernel.py +0 -0
  42. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/diptensor.py +0 -0
  43. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/endorfrq.py +0 -0
  44. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/endorfrq_perturb.py +0 -0
  45. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/eprload.py +0 -0
  46. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/eprsave.py +0 -0
  47. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/esfit.py +0 -0
  48. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/evolve.py +0 -0
  49. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ewrls.py +0 -0
  50. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/excitation.py +0 -0
  51. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/exciteprofile.py +0 -0
  52. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/experiment.py +0 -0
  53. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/exponfit.py +0 -0
  54. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/fastmotion.py +0 -0
  55. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/fdaxis.py +0 -0
  56. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/fitgui.py +0 -0
  57. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/garlic.py +0 -0
  58. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/gridcheck.py +0 -0
  59. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham.py +0 -0
  60. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_cf.py +0 -0
  61. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_ee.py +0 -0
  62. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_ez.py +0 -0
  63. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_ezho.py +0 -0
  64. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_hf.py +0 -0
  65. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_nn.py +0 -0
  66. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_nq.py +0 -0
  67. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_nz.py +0 -0
  68. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_oz.py +0 -0
  69. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_so.py +0 -0
  70. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ham_zf.py +0 -0
  71. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/hamsymm.py +0 -0
  72. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/initstate.py +0 -0
  73. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/isotopologues.py +0 -0
  74. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/levels.py +0 -0
  75. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/levelsplot.py +0 -0
  76. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/lineshape.py +0 -0
  77. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/lpsvd.py +0 -0
  78. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/makespec.py +0 -0
  79. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/mdhmm.py +0 -0
  80. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/mdload.py +0 -0
  81. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/mdtraj2oripot.py +0 -0
  82. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ml.py +0 -0
  83. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/mlpsvd.py +0 -0
  84. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/nucdata.py +0 -0
  85. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/nucfrq2d.py +0 -0
  86. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/orca2torchspin.py +0 -0
  87. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/ordering.py +0 -0
  88. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/oripotentialplot.py +0 -0
  89. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/orisel.py +0 -0
  90. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/pepper.py +0 -0
  91. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/pepper_autograd.py +0 -0
  92. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/photoselect.py +0 -0
  93. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/plegendre.py +0 -0
  94. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/propint.py +0 -0
  95. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/pulse.py +0 -0
  96. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/py.typed +0 -0
  97. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/rapidscan2spc.py +0 -0
  98. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resfields.py +0 -0
  99. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resfields_batch.py +0 -0
  100. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resfields_eig.py +0 -0
  101. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resfields_hybrid.py +0 -0
  102. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resfields_perturb.py +0 -0
  103. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resfreqs_matrix.py +0 -0
  104. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resfreqs_perturb.py +0 -0
  105. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resonator.py +0 -0
  106. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/resonatorprofile.py +0 -0
  107. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/rotations.py +0 -0
  108. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/rotutils.py +0 -0
  109. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/saffron_pathways.py +0 -0
  110. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/salt.py +0 -0
  111. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/sigeq.py +0 -0
  112. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/sitetransforms.py +0 -0
  113. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/sphgrid.py +0 -0
  114. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/spinladder.py +0 -0
  115. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/spinops.py +0 -0
  116. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/spinsystem.py +0 -0
  117. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/stackplot.py +0 -0
  118. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/stev.py +0 -0
  119. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/stochtraj_diffusion.py +0 -0
  120. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/stochtraj_jump.py +0 -0
  121. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/strainwidth.py +0 -0
  122. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/transmitter.py +0 -0
  123. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin/utils.py +0 -0
  124. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin.egg-info/SOURCES.txt +0 -0
  125. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin.egg-info/dependency_links.txt +0 -0
  126. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin.egg-info/requires.txt +0 -0
  127. {torchspin-0.4.0 → torchspin-0.6.0}/torchspin.egg-info/top_level.txt +0 -0
@@ -4,6 +4,407 @@ All notable changes to torchspin will be documented in this file.
4
4
  Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
5
5
  This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.6.0] — 2026-10-19
8
+
9
+ Follow-up to 0.5.0: the pulse-EPR test suites now assert the relative L2 error
10
+ against MATLAB rather than a cosine similarity, the one physics issue 0.5.0
11
+ left open turned out not to be a physics issue, and the real-pulse
12
+ (`saffron_thyme`) route — the last pulse-EPR route with no cross-validation at
13
+ all — is now EasySpin-equivalent and MATLAB-validated on 28 cases. Every
14
+ `saffron` route, ideal-pulse and real-pulse, rectangular and shaped, is now
15
+ cross-validated against MATLAB EasySpin.
16
+
17
+ Minor rather than patch: the real-pulse route's output changes (it was not
18
+ reproducing EasySpin before), and one input that used to be accepted now
19
+ raises. A predefined sequence with a nonzero `Exp.tp` was answered with a
20
+ silent *ideal*-pulse result; it now raises, as in `saffron.m` 351. Scripts
21
+ relying on that fallback were getting a wrong answer, and need either
22
+ `Exp.tp = 0` to keep the ideal-pulse result or a list-valued `Exp.Sequence`
23
+ to get the real-pulse one.
24
+
25
+ ### Fixed — the real-pulse `saffron` route now reproduces EasySpin
26
+
27
+ Two new oracles, 28 MATLAB cases in all (MATLAB R2024b), agreeing on both the
28
+ raw and the mean-centered signal at 3e-13..8e-12:
29
+
30
+ - `tests/data/generate_saffron_thyme_refs.m` →
31
+ `torchspin/tests/test_saffron_thyme_matlab_validation.py` (18 cases,
32
+ rectangular pulses): powder and single-crystal samples, transient and
33
+ single-point detection, echo integration, detection phase, explicit
34
+ simulation frame, relaxation, one and two indirect dimensions, and
35
+ predefined 2pESEEM / 3pESEEM / HYSCORE run through the real-pulse engine.
36
+ - `tests/data/generate_saffron_thyme_shaped_refs.m` →
37
+ `torchspin/tests/test_saffron_thyme_shaped_matlab_validation.py` (10 cases,
38
+ transcribed from EasySpin's own `tests/saffron_thyme_*.m`): `quartersin/
39
+ linear` chirps with `Pulse.trise` and a swept `Pulse.Frequency`, relaxation
40
+ during a chirp, `Opt.SimFreq = 15` against the automatic 32 GHz, a delay
41
+ sweep crossed with a pulse-parameter sweep (`Exp.Dim2 = {'p1.Flip' pi/3}`,
42
+ which regenerates the chirp at every point), `Sys.gFrame`/`Sys.AFrame`
43
+ tensor tilts against an equivalent sample tilt, and a predefined sequence
44
+ detected over a transient window.
45
+
46
+ Shaped pulses needed no new code: `torchspin/pulse.py` already built them for
47
+ `spidyan`, and a pulse dict in a list-valued `Exp.Sequence` passes through to
48
+ it unchanged. What had kept EasySpin's own thyme cases from running was the
49
+ propagation and detection, below.
50
+
51
+ 0.5.0 reported that this route "does not reproduce EasySpin and has a different
52
+ output contract", and that no reduction of its per-increment transients
53
+ recovered EasySpin's trace. That was correct, and the cause was not only the
54
+ missing detection stage: the propagation was wrong too. Six separate problems,
55
+ each of which alone makes the comparison meaningless:
56
+
57
+ - **The pulses were never given a carrier.** `Exp.mwFreq` was not passed to the
58
+ sequencer, so `FrameShift` was 0 and every pulse was built as a DC envelope
59
+ and applied in the lab frame, against an electron Zeeman term of ~9.5 GHz.
60
+ Nothing was excited. `Exp.mwFreq` is now required, as in EasySpin's
61
+ `s_sequencer`, and the simulation frame is entered by the same isotropic
62
+ g shift EasySpin uses (`saffron.m` 1838-1846).
63
+ - **The Hamiltonian and the operators were in different frames.** The
64
+ Hamiltonian was built in the molecular frame with the field rotated into it,
65
+ while the initial state (`-Sz`), excitation (`Sx`) and detection (`S+`)
66
+ operators stayed lab-frame. Any orientation with a nonzero phi angle was
67
+ therefore wrong, and rotating the operators instead is not an option because
68
+ `s_thyme` builds its pulse propagators from `real(xOp)` and
69
+ `1i*imag(xOp)`, which presumes `xOp = Sx`. The Hamiltonian is now
70
+ transformed into the lab-frame basis with the spin-rotation operator
71
+ `exp(-i*phi*Jz) exp(-i*theta*Jy) exp(-i*chi*Jz)` over all spins, which is
72
+ the same operator EasySpin's `rotatesystem` + `ham(Sys_L, [0 0 B])`
73
+ produces (agreement 5e-13) without needing per-interaction tensor rotation.
74
+ - **The orientation sum was renormalized.** EasySpin sums over orientations
75
+ weighted by `Exp.OriWeights` and does not divide by the total weight, which
76
+ is `4*pi`. Dividing by it scaled a powder down by ~12.6 and turned a
77
+ multi-site crystal stack into a mean instead of a sum.
78
+ - **The whole detection stage was missing** (`saffron.m` 1884-1940):
79
+ `Exp.DetPhase`, `Exp.DetIntegrate` echo integration, down-conversion through
80
+ `signalprocessing.m` and the `Exp.DetWindow` time axis. `Exp.DetWindow` is
81
+ applied the way EasySpin applies it, by shifting the last sequence element
82
+ by `DetWindow(1)` and appending the detected event — a zero-length one for
83
+ single-point detection — rather than by slicing the transient afterwards,
84
+ which would move the echo rather than the window. The output contract is now
85
+ EasySpin's: one detected point per increment for single-point detection,
86
+ and the transient axis appended to the indirect axes otherwise.
87
+ - **`Opt.IntTimeStep`'s default did not match.** EasySpin takes the Nyquist
88
+ time of the highest simulation-frame frequency, divides by 50 and rounds to
89
+ two significant digits; torchspin divided by 10 and did not round. The two
90
+ codes therefore integrated with different steps whenever the step was left
91
+ implicit.
92
+ - **A zero-length detected delay produced no signal**, so EasySpin's way of
93
+ expressing single-point detection yielded an empty trace.
94
+
95
+ ### Fixed — relaxation propagated `conj(rho)` in Liouville space
96
+
97
+ `spidyan`'s Liouville-space commutator superoperator was written as EasySpin
98
+ writes it, `kron(I,H) - kron(H.',I)`, which is correct for MATLAB's
99
+ column-major `vec()`. numpy flattens row-major, where
100
+ `vec_r(A@rho@B) = kron(A, B.T) @ vec_r(rho)`, so the two Kronecker factors
101
+ swap. With EasySpin's order the relaxation path propagated `rho.T`, which for
102
+ a Hermitian density matrix is `conj(rho)`. The same applies to the transform
103
+ of the relaxation superoperator into the Hamiltonian eigenbasis.
104
+
105
+ This affects `spidyan` and the real-pulse `saffron` route whenever relaxation
106
+ is active. A real detection trace (`Sz`) is untouched, which is why the
107
+ existing `spidyan` relaxation reference — `Exp.DetOperator = {'z1'}`, compared
108
+ on its real part only — could not see it; a demodulated `S+` trace was wrong
109
+ by a factor of several. `generate_spidyan_refs.m` gains case 8,
110
+ `spidyan_relaxationcomplexdetection.mat`, which detects `S+` under relaxation
111
+ and is compared on its imaginary part as well as its real part. Regenerating
112
+ the other seven spidyan references reproduced the committed files bit for bit.
113
+
114
+ ### Added — `Exp.Det*` fields and `Opt.SimulationMode` on saffron
115
+
116
+ `PulseExperiment` gains `DetWindow`, `DetSequence`, `DetPhase`,
117
+ `DetFrequency`, `DetIntegrate` and `DetOperator`, plus `Dim1`, `Dim2` and
118
+ `PhaseCycle` and support for a list-valued `Exp.Sequence` (EasySpin's general
119
+ pulse syntax: pulse dicts and delays). `SaffronOptions` gains
120
+ `SimulationMode` (`'fast'` | `'thyme'`), `IntTimeStep` and `SimFreq`.
121
+
122
+ Engine selection now follows EasySpin: `Opt.SimulationMode` chooses
123
+ explicitly, and leaving it unset keeps torchspin's inference from nonzero
124
+ `Exp.tp`, a list-valued `Exp.Sequence` or an `Exp.DetWindow`. The fast
125
+ algorithm raises EasySpin's errors rather than ignoring the input: a
126
+ predefined sequence with nonzero `Exp.tp` is rejected (`saffron.m` 351) where
127
+ torchspin used to ignore `Exp.tp` and silently return an ideal-pulse result,
128
+ and `Exp.DetSequence`/`Exp.DetOperator` are rejected (`saffron.m` 303). The
129
+ real-pulse engine warns about the fast-only fields it ignores, as EasySpin's
130
+ `s_sequencer` reports them.
131
+
132
+ ### Fixed — misleading `NotImplementedError` for multi-electron saffron
133
+
134
+ `saffron.py` told users to set `Opt.SimulationMode='thyme'` for multi-electron
135
+ systems, an option `SaffronOptions` did not have. The option now exists, and
136
+ the message says what else is needed (a finite length for every pulse).
137
+
138
+ ### Not changed — the ideal-pulse and real-pulse routes do not converge
139
+
140
+ The plan this work followed expected that as `Exp.tp -> 0` the real-pulse
141
+ result would approach the ideal-pulse result for the same sequence, as a
142
+ reference-free check on the normalization. It does not, and that is EasySpin's
143
+ behavior too: at matched settings torchspin reproduces EasySpin's own
144
+ ideal-vs-real mismatch to 1e-7 in scale (measured at four pulse lengths from
145
+ 20 ns down to 2.5 ns), so there is nothing here for torchspin to fix. Three
146
+ reasons, none of them a defect:
147
+
148
+ - With single-point detection EasySpin does not down-convert, so the detected
149
+ point carries the simulation-frame carrier phase, which advances ~50 cycles
150
+ per 20 ns increment.
151
+ - A single detected point contains coherence pathways that the ideal route
152
+ excludes analytically; the two-step cycles EasySpin's own templates use do
153
+ not remove them for this observable.
154
+ - As `tp -> 0` at fixed flip angle the required B1 grows without bound and
155
+ eventually exceeds the carrier frequency, so the rotating-wave picture
156
+ breaks down and a rectangular real pulse does not approach an ideal one.
157
+
158
+ Putting the simulation frame on the carrier to remove the first effect is not
159
+ a neutral change: it removes the carrier from the pulse too, so the linearly
160
+ polarized field becomes a rotating-frame field of twice the strength.
161
+
162
+ What that check was reaching for is covered instead by three relations that
163
+ need no reference and do hold: a single-site P1 crystal equals `4*pi` times
164
+ the independently validated `spidyan` result (1e-12); an isotropic powder
165
+ equals a single crystal; and a nominal `Pulse.Flip` of `pi/2` delivers full
166
+ transverse magnetization while `pi` delivers none, which is the sharpest cheap
167
+ guard against the pulse amplitude slipping by a factor of two.
168
+
169
+ ### Changed — `spidyan`'s default detection, and `Exp.DetSequence` strings
170
+
171
+ With neither `Exp.DetWindow` nor `Exp.DetSequence` given, `_sequencer` used to
172
+ detect only the last free evolution event. EasySpin's documented default is
173
+ `Exp.DetSequence = ones(1, length(Exp.Sequence))` — detection active during
174
+ the entire sequence — and that is now what happens. Calls that state their
175
+ detection explicitly, which is every cross-validated case, are unaffected.
176
+ `Exp.DetSequence` also accepts EasySpin's `'last'` and `'all'` strings, and a
177
+ length that is neither 1 nor `length(Exp.Sequence)` now raises instead of
178
+ being silently truncated.
179
+
180
+ ### Fixed — `rfmixer` could drop the last sample
181
+
182
+ The output time axis was built with `np.arange(t[0], t[-1] + dt/2, dt)` and
183
+ then clipped to `t <= t[-1]`, so floating-point round-off in the last step
184
+ could discard the final point and return a signal one sample shorter than its
185
+ input. MATLAB's `tIn(1):Opt.dt:tIn(end)` does not, and the axis is now built
186
+ from a step count.
187
+
188
+ ### Fixed — the "spidyan multi-spin phase drift" was a test transcription error
189
+
190
+ 0.5.0's `KNOWN_LIMITATIONS` reported an undiagnosed residual in `spidyan` that
191
+ grew linearly through free evolution for multi-spin systems (4.7e-3 for two
192
+ coupled electrons, 6.4e-5 electron-nucleus, 8.8e-6 for S=3/2), and warned that
193
+ it would be worse for longer sequences. **There is no such defect.**
194
+
195
+ Fitting the accumulated phase showed torchspin reproducing the analytic Larmor
196
+ frequency exactly (1489.828541 MHz at 1195 mT, g = 2.00232, in the 32 GHz
197
+ simulation frame) while the MATLAB reference sat 11.6 kHz below it. That
198
+ offset is `(2.00232 - gfree)/2.00232 = 3.47e-7` of the full Larmor frequency:
199
+ `generate_spidyan_refs.m` sets `Sys.g = gfree` (2.0023193043...), and the Python
200
+ tests had transcribed it as a rounded `2.00232`. A 3.5e-7 error in g shifts the
201
+ precession frequency enough to accumulate visibly as phase over a 0.5 us delay.
202
+
203
+ The three affected tests are exactly the three whose generator uses
204
+ `Sys.g = gfree`; the cases that set `Sys.ZeemanFreq` were unaffected and had
205
+ always agreed to 1e-13. With `GFREE` used in place of the rounded value, every
206
+ `spidyan` case agrees to 1e-14..1e-12. No library code changed.
207
+
208
+ ### Changed — pulse-EPR tests assert relative error, not cosine
209
+
210
+ `test_spidyan_matlab_validation.py`, `test_spidyan_tp_matlab_validation.py` and
211
+ `test_saffron_matlab_validation.py` now assert the relative L2 error against the
212
+ MATLAB reference (`STRICT_TOL = 1e-8` and `1e-6` respectively) instead of
213
+ `cosine > 0.85..0.99`. A cosine is scale-invariant and dominated by a pulse-EPR
214
+ trace's DC offset, so it could see neither the eight defects fixed in 0.5.0 nor
215
+ the `gfree` rounding above.
216
+
217
+ Making them strict required fixing three kinds of harness fault that a cosine
218
+ had been hiding. **None was a library fault** -- checked against matched
219
+ references, agreement is 1e-16..1e-7 in every case:
220
+
221
+ - **Accumulation path.** No saffron reference generator sets `Opt.TimeDomain`,
222
+ so every reference comes from the frequency-binned path, but twelve tests ran
223
+ with `TimeDomain=True`. That compares direct time evolution against
224
+ EasySpin's own binned approximation -- a 7-60% difference in the modulation
225
+ that is real, expected, and nothing to do with correctness.
226
+ - **Normalization.** Six generators write `y = y/max(abs(y))`; the tests
227
+ compared torchspin's unnormalized output against them, leaving fixed scale
228
+ factors of ~4*pi and similar.
229
+ - **Orientation grid.** `saffron_hyscore14n` and `saffron_hyscore_ganiso` are
230
+ generated at `Opt.GridSize = 181` but were tested at 91, and
231
+ `saffron_2psimple` is generated at EasySpin's default 31 but was tested at 20.
232
+ `GridSize` is now stated explicitly in every case rather than inherited from a
233
+ default that could drift apart between the two codes.
234
+
235
+ `test_orientation_selective` documents a measured case where the **committed
236
+ fixture** is stale: regenerating it from the local EasySpin gives a reference
237
+ torchspin matches to 4.4e-15, and that fresh reference differs from the stored
238
+ one by 1.4e-6 in overall scale. Its amplitude is therefore checked at 1e-5 with
239
+ the peak-normalized shape checked strictly, rather than silently loosening
240
+ everything.
241
+
242
+ The dead cosine helpers are removed, and the tests no longer carry a `_cosine`
243
+ suffix that would misdescribe what they assert.
244
+
245
+ ## [0.5.0] — 2026-10-19
246
+
247
+ Found while cross-validating `saffron` against MATLAB EasySpin on a 13C
248
+ HYSCORE model, where the modulation came out wrong although the raw cosine
249
+ stayed at 0.99999. The existing `saffron` suite scored
250
+ agreement with a cosine similarity, which for a pulse-EPR time trace is
251
+ dominated by the DC offset: a HYSCORE trace can have a ~100% wrong modulation
252
+ and still score >0.999. Every defect below lived in that blind spot.
253
+
254
+ New oracle: `tests/data/generate_saffron_phase_refs.m` →
255
+ `torchspin/tests/test_saffron_phase_matlab_validation.py` (25 cases, MATLAB
256
+ R2024b), plus a strict `spidyan` amplitude case. It asserts the **mean-centered** relative L2 error — the modulation
257
+ — as well as the raw error, at tolerances near double-precision roundoff. All
258
+ 26 strict cases, 25 of them fail on 0.4.0 and pass here (the 26th, two-nucleus
259
+ Mims ENDOR, is the one case 0.4.0's hardcoded `EndorMethod=1` got right). Agreement across the predefined
260
+ 2p/3p/4pESEEM, HYSCORE and Mims-ENDOR routes, direct and frequency-binned,
261
+ powder and explicit single crystal, S=1/2 and S=3/2, with and without
262
+ orientation selection and nuclear quadrupole coupling, is now 1e-16 to 1e-14
263
+ rather than "cosine ≥ 0.98".
264
+
265
+ ### Fixed — `saffron` tau phase factor in all four predefined ESEEM/HYSCORE routes
266
+
267
+ `saffron.m` builds the tau-dependent phase factor as
268
+ `Q_ = exp(-2i*pi*Ea*tau)*exp(-2i*pi*Eb*tau)'`, and MATLAB's `'` is a
269
+ **conjugate** transpose, so element *(i,j)* is `exp(-2i*pi*(Ea_i - Eb_j)*tau)`
270
+ — a *difference* of the two manifold energies. torchspin formed the outer
271
+ product without conjugating the second factor, giving their *sum*. The error
272
+ vanishes at `tau = 0`, which is why it survived.
273
+
274
+ Fixed for `exp_id` 1, 2, 3 and 4 (2pESEEM, 3pESEEM, 4pESEEM, HYSCORE); the
275
+ Mims-ENDOR branch already conjugated correctly. On the in-repo
276
+ `saffron_HYSCORE.mat` case (tau = 0.08 µs) the mean-centered error against
277
+ MATLAB drops from **112% to roundoff**, while the raw cosine moves only from
278
+ 0.999991 to 1.000000.
279
+
280
+ ### Fixed — `saffron` alpha/beta manifold assignment for S=1/2
281
+
282
+ EasySpin's `Transitions = [1 2]` makes `Ea` the **upper** manifold
283
+ (`<S> = +1/2·n`) and `Eb` the lower. The S=1/2 fast path had them swapped,
284
+ while torchspin's own S>1/2 path assigned them correctly — an internal
285
+ inconsistency. Invisible while the nuclear Hamiltonian is linear in **I**
286
+ (the two HYSCORE coherence-transfer pathways then symmetrize the swap away),
287
+ it becomes a 7–33% error as soon as a nuclear quadrupole makes the manifold
288
+ eigenvectors non-trivial.
289
+
290
+ ### Fixed — `saffron` dimension order of the 2D frequency-bin accumulation
291
+
292
+ `sf_peaks.c` bins a 2D spectrum at `idx = nu1 + nu2*nPoints1`, i.e. dimension
293
+ 1 carries `nu1`. `saffron_peaks.py` computed `idx2 * n_points1 + idx1` into a
294
+ row-major `(n_points1, n_points2)` buffer, which **transposes** the spectrum
295
+ and mis-strides it when the two dimensions differ in length. Fixed at all
296
+ five 2D increment schemes (11–15, 17). The binned and direct paths of
297
+ torchspin itself disagreed by 2% before this; a new test asserts they agree,
298
+ which catches this class of bug without any MATLAB reference.
299
+
300
+ ### Fixed — `saffron` `Opt.OriThreshold` reference level under orientation selection
301
+
302
+ EasySpin takes the pruning threshold from the **unweighted**
303
+ `|<i|SyLab|j>|` maximum and only then multiplies in the excitation amplitude,
304
+ so a narrow `Exp.ExciteWidth` discards more transitions. torchspin took the
305
+ maximum after excitation weighting and so kept transitions EasySpin drops,
306
+ giving a 75% modulation error for S=3/2 + 14N at `ExciteWidth = 100` MHz.
307
+ Affects S>1/2 with `Exp.ExciteWidth` only.
308
+
309
+ ### Fixed — `saffron` `Opt.Expand` and `Opt.EndorMethod` defaults are conditional in EasySpin
310
+
311
+ Both were hardcoded to a single value, so the default-argument path silently
312
+ ran a different algorithm than EasySpin:
313
+
314
+ - `Opt.Expand` defaults to **4 for 1D and 2 for 2D** sequences
315
+ (`saffron.m:751`); torchspin always used 4, changing the zero-fill of every
316
+ binned HYSCORE.
317
+ - `Opt.EndorMethod` defaults to **0 for a single SHF nucleus or under
318
+ `ProductRule`, else 1** (`saffron.m:729`); torchspin always used 1, which
319
+ selects a different RF-pulse model. For one nucleus this put the ENDOR
320
+ amplitude 40% out and changed the lineshape, at cosine 0.996.
321
+
322
+ Both now accept `None` (the default) to mean EasySpin's rule; an explicit
323
+ integer still overrides it.
324
+
325
+ ### Fixed — `saffron`/`saffron_thyme` treated `Exp.SampleFrame` alone as a single crystal
326
+
327
+ EasySpin decides sample type from `CrystalSymmetry` or `MolFrame` only
328
+ (`p_sampletype.m:26`): `Exp.SampleFrame` rotates the sample, but a disordered
329
+ powder stays a powder. `saffron` and `saffron_thyme` also keyed on
330
+ `SampleFrame`, so `SampleFrame` alone silently simulated a **single crystal
331
+ where EasySpin simulates a powder** — a different physical sample, not a
332
+ numerical discrepancy. `pepper` already had this right; `saffron` now matches
333
+ it. Code that relied on `SampleFrame` alone to select a crystal must now pass
334
+ `CrystalSymmetry` (e.g. `'P1'`) or `MolFrame`, as EasySpin requires.
335
+
336
+ ### Fixed — `saffron` custom sequences were not divided by their pathway prefactor
337
+
338
+ For a user-defined sequence (`Exp.Sequence` given as pulses and delays, not a
339
+ predefined name) `saffron.m:1669` divides the signal by the largest surviving
340
+ pathway prefactor. torchspin omitted this, leaving a custom HYSCORE-style
341
+ sequence a factor of **8** too small. Shape was unaffected, so the cosine was
342
+ 1.000000 and the existing `cosine > 0.95` test passed. Predefined sequences
343
+ are deliberately *not* renormalized this way, and are unchanged.
344
+
345
+ ### Fixed — `spidyan` complex detection operators were half amplitude
346
+
347
+ `signalprocessing.m` multiplies a **complex** detection trace (`S+`/`S-`) by
348
+ two when it is demodulated, because the analytic-signal normalization is
349
+ otherwise off by that factor; a purely real or purely imaginary trace (`Sz`) is
350
+ left alone. torchspin applied the demodulation phase but not the factor, so a
351
+ demodulated `<S+>` channel came out at exactly half EasySpin's amplitude — at
352
+ cosine 1.000000, under a `cosine >= 0.99` assertion. The `<Sz>` channel in the
353
+ same simulation was already correct, which is why it went unnoticed.
354
+
355
+ ### Audit of the remaining pulse-EPR routes
356
+
357
+ Prompted by the above, the routes that were scored only by cosine were
358
+ re-checked against fresh MATLAB references with strict metrics. Now at
359
+ roundoff and covered by tests: custom user sequences (direct and binned),
360
+ `ProductRule` x `TimeDomain` for two nuclei (all four combinations),
361
+ multi-component input with `Sys.weight`, and S=1 systems.
362
+
363
+ Several apparent discrepancies turned out to be **test-harness** faults rather
364
+ than library faults, and are worth knowing about before trusting an old
365
+ number: `test_3psimple`, `test_4psimple`, `test_hyscore14n`,
366
+ `test_hyscore_ganiso`, `test_mimssimple` and `test_twocomponents` compare
367
+ torchspin's unnormalized output against a MATLAB reference its generator had
368
+ normalized (`y = y/max(abs(y))`), so they sat at a fixed scale factor of 4*pi
369
+ or similar; and `test_saffron_2pESEEM`, `test_saffron_3pESEEM`,
370
+ `test_saffron_HYSCORE_cosine_1d`, `test_S1_*`, `test_S32_HYSCORE`,
371
+ `test_orientation_selective` and `test_customsequence` run with
372
+ `TimeDomain=True` against references generated *without* `Opt.TimeDomain`,
373
+ i.e. they compare direct time evolution against EasySpin's own
374
+ frequency-binned approximation. Both faults are invisible to a cosine.
375
+ Checked against matched references, the library agrees to roundoff in every
376
+ one of these cases.
377
+
378
+ Still open, and recorded in `KNOWN_LIMITATIONS.md`:
379
+
380
+ - ~~`spidyan` multi-spin systems show a small residual that grows linearly
381
+ through free evolution — a phase drift of order 1e-5 relative — for two
382
+ coupled electrons (4.7e-3 overall), an electron-nucleus pair (6.4e-5) and
383
+ S=3/2 (8.8e-6). Single-electron cases are exact to 1e-13. Not diagnosed.~~
384
+ **Retracted — there is no such defect.** This was a test transcription error
385
+ (the tests rounded EasySpin's `gfree` to 2.00232); torchspin reproduces the
386
+ analytic Larmor frequency exactly. See [Unreleased]. The claim is struck
387
+ through rather than deleted because 0.5.0 shipped with it.
388
+ - `saffron` with real pulses returns per-increment transients
389
+ (`nPoints x nDet x nTime`) where EasySpin returns one detected point per
390
+ increment, and no simple reduction of those transients recovers EasySpin's
391
+ trace — the closest is ~3% off after fitting an arbitrary scale. Treat the
392
+ real-pulse route as unvalidated; `saffron_thyme` remains without MATLAB
393
+ cross-validation.
394
+ - `saffron` accepts a predefined `Exp.Sequence` together with real pulses
395
+ (`Exp.tp`) and silently routes it to the thyme engine; EasySpin rejects that
396
+ combination outright. The output is therefore unvalidated.
397
+ - EasySpin's shaped/chirped pulses (`Type`, `trise`, `Frequency`),
398
+ `Exp.DetWindow`/`DetPhase` and `Opt.SimulationMode` are not implemented in
399
+ `saffron_thyme`, so EasySpin's own thyme test cases cannot be run against it.
400
+
401
+ ### Note
402
+
403
+ `saffron` gradients are unaffected in form: a finite-difference check against
404
+ the corrected simulator at nonsymmetric hyperfine values agrees to 1e-10.
405
+ Gradients taken before this release were consistent with the *old*, incorrect
406
+ forward model.
407
+
7
408
  ## [0.4.0] — 2026-10-09
8
409
 
9
410
  Found by fitting a Cu(II) phthalocyanine spectrum (CuPc in ZnPc: S=1/2,
@@ -733,6 +1134,7 @@ ported to Python/PyTorch with 1820+ passing tests.
733
1134
 
734
1135
  <!-- 0.3.0 was the first public release and the history before it was squashed,
735
1136
  so v0.1.0-v0.2.2 have no tags in this repository to link to. -->
736
- [Unreleased]: https://github.com/follmerlab/torchspin/compare/v0.4.0...HEAD
1137
+ [0.6.0]: https://github.com/follmerlab/torchspin/compare/v0.5.0...v0.6.0
1138
+ [0.5.0]: https://github.com/follmerlab/torchspin/compare/v0.4.0...v0.5.0
737
1139
  [0.4.0]: https://github.com/follmerlab/torchspin/compare/v0.3.0...v0.4.0
738
1140
  [0.3.0]: https://github.com/follmerlab/torchspin/releases/tag/v0.3.0
@@ -2,15 +2,22 @@ cff-version: 1.2.0
2
2
  message: "If you use torchspin in your research, please cite both torchspin and the upstream EasySpin paper."
3
3
  title: "torchspin: Differentiable PyTorch port of EasySpin for EPR spectroscopy"
4
4
  type: software
5
- version: "0.4.0"
6
- date-released: "2026-10-09"
5
+ version: "0.5.0"
6
+ date-released: "2026-10-19"
7
7
  license: MIT
8
8
  authors:
9
9
  - family-names: "Follmer"
10
10
  given-names: "Alec"
11
11
  email: "ahfollmer@ucdavis.edu"
12
12
  repository-code: "https://github.com/follmerlab/torchspin"
13
- url: "https://github.com/follmerlab/torchspin"
13
+ preferred-citation:
14
+ type: article
15
+ authors:
16
+ - family-names: "Follmer"
17
+ given-names: "A. H."
18
+ title: "TorchSpin: Differentiable Electron Paramagnetic Resonance Simulation in PyTorch"
19
+ journal: "arXiv preprint arXiv:2610.18924"
20
+ year: 2026
14
21
  keywords:
15
22
  - "EPR"
16
23
  - "ESR"
@@ -18,9 +25,9 @@ keywords:
18
25
  - "spin-Hamiltonian"
19
26
  - "pytorch"
20
27
  - "autograd"
28
+ - "differentiable simulation"
21
29
  - "magnetic resonance"
22
30
  - "EasySpin"
23
-
24
31
  references:
25
32
  - type: article
26
33
  title: "EasySpin, a comprehensive software package for spectral simulation and analysis in EPR"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: torchspin
3
- Version: 0.4.0
3
+ Version: 0.6.0
4
4
  Summary: Differentiable PyTorch port of EasySpin - EPR/ESR spin Hamiltonian and spectrum simulation
5
5
  Author-email: Alec Follmer <ahfollmer@ucdavis.edu>
6
6
  Maintainer-email: Alec Follmer <ahfollmer@ucdavis.edu>
@@ -226,8 +226,12 @@ example collection in `examples/{solidstate,liquids,slowmotion,endor,fitting,mag
226
226
 
227
227
  Every simulator with an EasySpin counterpart is checked against stored MATLAB
228
228
  reference output on each commit. Agreement is cosine ≥ 0.999 (most
229
- 0.9999–1.0000) for `pepper`, `garlic`, `salt`, `saffron`, `spidyan` and
230
- `curry`, with absolute intensities matched where EasySpin defines them.
229
+ 0.9999–1.0000) for `pepper`, `garlic`, `salt` and `curry`, with absolute
230
+ intensities matched where EasySpin defines them. The pulse-EPR routes
231
+ (`saffron`, `saffron_thyme`, `spidyan`) are held to a stricter metric — the
232
+ relative L2 error on the raw *and* the mean-centered signal, 1e-16..1e-7 —
233
+ because a cosine is scale-invariant and dominated by a pulse-EPR trace's DC
234
+ offset.
231
235
  `chili` and the stochastic `cardamom` have documented caveats. Details per
232
236
  simulator, with the evidence: [KNOWN_LIMITATIONS.md](KNOWN_LIMITATIONS.md).
233
237
 
@@ -245,9 +249,10 @@ replicates and a per-workload device audit:
245
249
 
246
250
  If you use torchspin in published work, please cite both:
247
251
 
252
+ - **TorchSpin** — Follmer, A.H. *arXiv:2610.18924* (2026) https://arxiv.org/abs/2610.18924.
248
253
  - **EasySpin**, the MATLAB toolbox this port is based on —
249
254
  Stoll, S. & Schweiger, A. *J. Magn. Reson.* **178** (2006) 42–55.
250
- - **torchspin** — see [CITATION.cff](CITATION.cff).
255
+
251
256
 
252
257
  ---
253
258
 
@@ -171,8 +171,12 @@ example collection in `examples/{solidstate,liquids,slowmotion,endor,fitting,mag
171
171
 
172
172
  Every simulator with an EasySpin counterpart is checked against stored MATLAB
173
173
  reference output on each commit. Agreement is cosine ≥ 0.999 (most
174
- 0.9999–1.0000) for `pepper`, `garlic`, `salt`, `saffron`, `spidyan` and
175
- `curry`, with absolute intensities matched where EasySpin defines them.
174
+ 0.9999–1.0000) for `pepper`, `garlic`, `salt` and `curry`, with absolute
175
+ intensities matched where EasySpin defines them. The pulse-EPR routes
176
+ (`saffron`, `saffron_thyme`, `spidyan`) are held to a stricter metric — the
177
+ relative L2 error on the raw *and* the mean-centered signal, 1e-16..1e-7 —
178
+ because a cosine is scale-invariant and dominated by a pulse-EPR trace's DC
179
+ offset.
176
180
  `chili` and the stochastic `cardamom` have documented caveats. Details per
177
181
  simulator, with the evidence: [KNOWN_LIMITATIONS.md](KNOWN_LIMITATIONS.md).
178
182
 
@@ -190,9 +194,10 @@ replicates and a per-workload device audit:
190
194
 
191
195
  If you use torchspin in published work, please cite both:
192
196
 
197
+ - **TorchSpin** — Follmer, A.H. *arXiv:2610.18924* (2026) https://arxiv.org/abs/2610.18924.
193
198
  - **EasySpin**, the MATLAB toolbox this port is based on —
194
199
  Stoll, S. & Schweiger, A. *J. Magn. Reson.* **178** (2006) 42–55.
195
- - **torchspin** — see [CITATION.cff](CITATION.cff).
200
+
196
201
 
197
202
  ---
198
203
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "torchspin"
7
- version = "0.4.0"
7
+ version = "0.6.0"
8
8
  description = "Differentiable PyTorch port of EasySpin - EPR/ESR spin Hamiltonian and spectrum simulation"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -25,7 +25,7 @@ try:
25
25
  __version__ = _pkg_version("torchspin")
26
26
  except _PkgNotFound:
27
27
  # Editable install during development, or version metadata unavailable.
28
- __version__ = "0.4.0+dev"
28
+ __version__ = "0.6.0+dev"
29
29
 
30
30
  from torchspin._compile import maybe_compile, set_compile_enabled, is_compile_available
31
31
  from torchspin.autograd import differentiable_spectrum
@@ -16,6 +16,8 @@ Example
16
16
  """
17
17
  from __future__ import annotations
18
18
 
19
+ import math
20
+
19
21
  import numpy as np
20
22
  from scipy.interpolate import interp1d
21
23
 
@@ -123,10 +125,12 @@ def rfmixer(
123
125
  # Mix: multiply by exp(j*2π*LO*t)
124
126
  signal_mixed = signal * np.exp(2j * np.pi * LO * 1e3 * t)
125
127
 
126
- # Resample to output time grid
127
- t_out = np.arange(t[0], t[-1] + dt * 0.5, dt)
128
- # Clip to not exceed original range
129
- t_out = t_out[t_out <= t[-1]]
128
+ # Resample to output time grid. MATLAB's `tOut = tIn(1):Opt.dt:tIn(end)`
129
+ # reproduces tIn exactly when Opt.dt equals the input step; building the
130
+ # axis from a step count rather than np.arange's endpoint test avoids
131
+ # dropping the last sample to floating-point round-off.
132
+ n_out = int(math.floor((t[-1] - t[0]) / dt + 1e-9))
133
+ t_out = t[0] + np.arange(n_out + 1) * dt
130
134
 
131
135
  if len(t_out) == len(t) and np.allclose(t_out, t):
132
136
  signal_out = signal_mixed