Proper Gradients for MetaPost Pulse Sequences

In which I improve upon the previous post...

In yesterday's post I talked about using MetaPost to generate Pulse Sequence Diagrams for MRI documents. I have been using this to make nice diagrams for my Thesis. There is one problem, however, with the pulse sequence file provided on Mark's website - it generates square gradients.

Theoretically speaking a perfect MR sequence would use square gradients to generate a signal but this isn't practical in reality. Instead gradients are ramped over time, dependent on the limitations of the scanner, leading to trapezoidal gradient shapes.* [1]

By adding these the following code to pulses.mp. Under drawing functions:

def gradf(expr off, xs, ys) =
    draw ((0,0)--(0.25u,1u)--(0.75u,1u)--(1u,0)) xscaled xs yscaled ys shifted off; 
enddef;

Under progressive functions:

def xgrad(expr l, h) =
    gradf((Ox,Oy),l,h);
    Ox := Ox + l*u; 
enddef;

If you call xgrad in your MetaPost file it will now draw a trapezoidal gradient. The ramping will always be a quarter of the length of the gradient which you may find isn't suitable for longer gradients. In that case some simple edits to these functions will be necessary.

Here's an example sequence using the new gradients:

<EDIT>

The same thing can be achieved for the frequency encoding block.

Under drawing functions:

def tablegrf(expr off, xs, ys, gran, upd) =
    begingroup; 
    for i=-gran upto gran: 
        draw ((0,0)--(0.25u,1u)--(0.75u,1u)--(1u,0)) xscaled xs yscaled (ys*(i/gran)) shifted off withcolor 0.5white withpen pencircle scaled 0.5pt; 
    endfor; 
    endgroup; 
    gradf(off,xs,-upd*ys); drawarrow ((0.5u*xs,-0.9u*ys*upd)+off)--((0.5u*xs,0.9u*ys*upd)+off) withpen pencircle scaled 2pt withcolor 0.3white;
enddef;

Under progressive functions:

def xtablegr(expr l, h, grad, upd) =
    tablegrf((Ox,Oy),l,h,grad,upd);
    Ox := Ox + l*u; 
enddef;

Which will give you a nice version of that too:

</EDIT>

*Many other gradient shapes can be used to achieve other ends. For example, quieting the sequence noise.

Tom Out!

P.S. Now a Major Github repository! https://github.com/tawilkinson/pulses

References

[1] Bernstein, M. A., et al. (2004) Handbook of MRI Pulse Sequences. Academic Press.