NMR and MRI physics · Intermediate

Cartesian k-space filling, one phase-encode line per TR

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

In conventional spin-warp MRI, k-space is filled one horizontal line per repetition time (TR). Each line has a fixed phase-encode step set by a gradient pulse, and the readout gradient sweeps along kx while the signal is sampled. Total scan time is therefore the product of TR, the number of phase-encode lines, and the number of averages.

Intuition

Picture k-space as a grid, a map of spatial frequencies. The readout gradient moves you left to right along a row, like a pen drawing a line while the ADC samples. The phase-encode gradient decides which row you are on, by moving you up or down before the readout starts. After the readout the sequence must wait for the next TR, then it takes another step in ky and draws the next line. Rows near the center (ky near 0) carry most signal and contrast. Outer rows carry fine detail and edges. The slow part is that only one row can be acquired per excitation, so image height in pixels sets the number of TRs.

Key equations

kx(t)=γ2π∫0tGx(τ)dτ
(1)

Position along kx grows with the accumulated readout gradient area.

ky=γ2πGyTpe
(2)

Phase-encode gradient amplitude (or duration) selects the ky row.

Δky=1FOVy
(3)

Spacing between phase-encode lines sets the field of view in y.

Tscan=Npe⋅TR⋅NEX
(4)

Scan time for a 2D spin-warp acquisition with N_pe lines and NEX averages.

Derivation

  1. Spins in a gradient precess at a frequency that depends on position: ω(x)=γ(B0+Gxx).
  2. Phase accumulated is ϕ(x,t)=γx∫Gdt, which defines k=(γ2π)∫Gdt, so signal S(k)=∫ρ(x)e−i2πkxdx.
  3. Applying Gy for a fixed time Tpe before readout leaves each y position with phase 2π ky y, with ky set by Gy amplitude.
  4. Stepping Gy in equal increments from -Gmax to +Gmax gives Npe equally spaced ky values with spacing Δky.
  5. The Fourier relation says sample spacing Δk gives FOV=1/Δk, and the maximum extent kmax gives resolution Δy=1/(2kmax).
  6. Since one excitation yields one readout, one ky line is acquired per TR, so total time is Npe×TR×NEX.

Worked example

Problem

A 2D spin-echo scan has a 256 x 256 matrix, TR = 500 ms, and NEX = 2. Phase encoding is along y. Find the scan time and the ky spacing for FOV_y = 240 mm.

  1. Number of phase-encode lines Npe = 256.
  2. Scan time = 256 x 0.5 s x 2 = 256 s.
  3. Convert to minutes: 256/60 ≈ 4.27 min.
  4. Δky = 1/FOV = 1/0.24 m = 4.17 cycles/m (0.0417 cycles/cm).
  5. Maximum ky = (Npe/2)Δky = 128 x 4.17 ≈ 533 cycles/m, giving pixel size 240/256 = 0.94 mm.

Answer About 256 s (4 min 16 s); Δky ≈ 4.17 cycles/m.

Common mistakes

  • Thinking each TR fills the whole of k-space rather than a single line.
  • Confusing the frequency-encode direction (sampled continuously during readout) with phase-encode (stepped between TRs).
  • Believing that more readout samples along kx lengthens scan time; it does not, but more ky lines does.
  • Assuming the outer lines carry the most signal; the center of k-space dominates contrast and signal.
  • Forgetting that the FOV is set by line spacing, not by the maximum ky extent.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

scene.py(106 lines)
from manim import *
import numpy as np
import sys
from blocks import *
from mri_blocks import *

T_MAX = 100.0   # ms, one TR
NY = 8          # phase-encode lines
RO = (30.0, 60.0)
RO_AMP = 0.8
PRE = (16.0, 26.0)
PRE_AMP = -RO_AMP * (RO[1] - RO[0]) / 2 / (PRE[1] - PRE[0])  # -1.2


def cartesian_order(ny):
    """Record one snapshot per TR: TR index, k-space row, ky and Gy amplitude."""
    states = []
    for tr in range(ny):
        row = tr  # linear bottom-to-top ordering
        ky = -1 + (2 * row + 1) / ny
        states.append({"tr": tr, "row": row, "ky": ky, "gy_amp": ky})
    return states


def kspace_path(gy_amp, dt=0.05):
    """Integrate rectangular Gx/Gy lobes: k(t) = integral of G dt (normalised)."""
    t = np.arange(0, RO[1] + dt, dt)
    gx = np.where((t >= PRE[0]) & (t < PRE[1]), PRE_AMP, 0.0)
    gx = gx + np.where((t >= RO[0]) & (t < RO[1]), RO_AMP, 0.0)
    gy = np.where((t >= PRE[0]) & (t < PRE[1]), gy_amp, 0.0)
    norm_x = RO_AMP * (RO[1] - RO[0]) / 2
    norm_y = PRE[1] - PRE[0]
    kx = np.cumsum(gx) * dt / norm_x
    ky = np.cumsum(gy) * dt / norm_y
    return t, kx, ky


class Main(Scene):
    def construct(self):
        TitleCard(self, "Cartesian k-space filling", "one phase-encode line per TR")

        # --- timing diagram (top) ---
        d = PulseSequenceDiagram(rows=("RF", "Gz", "Gy", "Gx", "ADC"), t_max=T_MAX,
                                 width=12, height=3.0)
        d.shift(UP * 1.3)
        rf = d.add_rf(2, 12, kind="sinc")
        gz = VGroup(d.add_gradient("Gz", 2, 12, 0.8, shape="rect"),
                    d.add_gradient("Gz", 12, 16, -0.8, shape="rect"))
        gx = VGroup(d.add_gradient("Gx", PRE[0], PRE[1], PRE_AMP, shape="rect"),
                    d.add_gradient("Gx", RO[0], RO[1], RO_AMP, shape="rect"))
        adc = d.add_adc(RO[0], RO[1])
        # TE / TR markers with readable labels
        def marker(t, txt):
            top = d._pt(t, 0) + UP * 0.25
            bot = d._pt(t, 4) + DOWN * 0.25
            return VGroup(DashedLine(bot, top, color=C_DIM, stroke_width=2),
                          Text(txt, font_size=24, color=C_DIM).next_to(top, UP, buff=0.03))
        te = marker((RO[0] + RO[1]) / 2, "TE")
        tr_m = marker(T_MAX, "TR")
        self.play(Create(d), FadeIn(rf, gz, gx, adc), FadeIn(te, tr_m), run_time=1.5)

        states = cartesian_order(NY)
        gy = d.add_gradient("Gy", PRE[0], PRE[1], states[0]["gy_amp"], shape="rect")
        cursor = d.time_cursor()

        # --- k-space (bottom left) and info (bottom right) ---
        ks = KSpacePlot(nx=NY, ny=NY, size=2.0).move_to([-3, -2.1, 0])
        info = Text("TR 1/8", font_size=28).move_to([2.5, -1.7, 0])
        kyt = MathTex(r"k_y=%.2f" % states[0]["ky"], font_size=34).next_to(info, DOWN, buff=0.3)
        self.play(FadeIn(ks), FadeIn(gy), FadeIn(info, kyt), FadeIn(cursor))
        StepCaption(self, "Gy amplitude steps each TR; Gx readout sweeps kx")

        paths = []
        for s in states:
            d.tracker.set_value(0)
            new_gy = d.add_gradient("Gy", PRE[0], PRE[1], s["gy_amp"], shape="rect")
            new_info = Text("TR %d/%d" % (s["tr"] + 1, NY), font_size=28).move_to(info)
            new_ky = MathTex(r"k_y=%.2f" % s["ky"], font_size=34).move_to(kyt)
            self.play(Transform(gy, new_gy), Transform(info, new_info),
                      Transform(kyt, new_ky), d.cursor_to(PRE[0]), run_time=0.35)
            t, kx, kyv = kspace_path(s["gy_amp"])
            pts = list(zip(kx[::20], kyv[::20])) + [(kx[-1], kyv[-1])]
            traj = ks.trajectory(pts, run_time=0.8)
            self.play(traj, d.cursor_to(RO[1], rate_func=linear), run_time=0.8)
            paths.append(traj.mobject)
            self.play(d.cursor_to(T_MAX), traj.mobject.animate.set_stroke(opacity=0.45),
                      run_time=0.3)

        StepCaption(self, "N phase steps x TR = scan time: one k-space line per TR")
        self.wait(1)


if __name__ == "__main__" and "--selftest" in sys.argv:
    st = cartesian_order(NY)
    assert len(st) == NY
    assert sorted(s["row"] for s in st) == list(range(NY))
    kys = [s["ky"] for s in st]
    assert all(a < b for a, b in zip(kys, kys[1:]))
    assert abs(sum(kys)) < 1e-12 and abs(kys[0] + kys[-1]) < 1e-12
    for s in st:
        t, kx, ky = kspace_path(s["gy_amp"])
        i_pre = np.searchsorted(t, PRE[1])
        assert abs(kx[i_pre] + 1) < 0.02          # prephaser to -kx_max
        assert abs(kx[-1] - 1) < 0.02             # readout ends at +kx_max
        assert abs(ky[-1] - s["ky"]) < 0.02       # Gy area gives ky
    print("selftest ok")

The Manim scene code that draws this lesson's animation in the Signals AI app (Python, Manim Community).

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.