points_to_waveform.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import numpy as np
  2. def points_to_waveform(
  3. amplitudes: np.ndarray, grad_raster_time: float, times: np.ndarray
  4. ) -> np.ndarray:
  5. """
  6. 1D interpolate amplitude values `amplitudes` at time indices `times` as per the gradient raster time
  7. `grad_raster_time` to generate a gradient waveform.
  8. Parameters
  9. ----------
  10. amplitudes : numpy.ndarray
  11. Amplitude values at time indices `times`.
  12. grad_raster_time : float
  13. Gradient raster time.
  14. times : numpy.ndarray
  15. Time indices.
  16. Returns
  17. -------
  18. waveform : numpy.ndarray
  19. Gradient waveform.
  20. """
  21. amplitudes = np.asarray(amplitudes)
  22. times = np.asarray(times)
  23. if amplitudes.size == 0:
  24. return np.array([0])
  25. grd = (
  26. np.arange(
  27. start=np.round(np.min(times) / grad_raster_time),
  28. stop=np.round(np.max(times) / grad_raster_time),
  29. )
  30. * grad_raster_time
  31. )
  32. waveform = np.interp(x=grd + grad_raster_time / 2, xp=times, fp=amplitudes)
  33. return waveform