How it works

This page explains the calculations K-Space Paint performs behind the scenes, using the same definitions as the actual program.

K-Space Paint is a teaching tool for learning how the Fourier transform relates images to k-space. It does not reproduce how an MRI scanner acquires signals or reconstructs images, and it must not be used for medical decisions (see “Differences from real MRI” below).

1. Grid and coordinates

Both image space and k-space are N×N grids (N=256).

2. Fourier transform

Drawing an image (forward transform)

The app computes the 2D discrete Fourier transform of the image you draw, f(x,y) (white = 1, black = 0).

F(u,v)= ∑x=0N−1 ∑y=0N−1 f(x,y) e−i2π(ux+vy)/N

Drawing in k-space (inverse transform)

The app computes the 2D inverse discrete Fourier transform of the k-space you draw, F(u,v) (complex-valued).

f(x,y)= ∑u=−128127 ∑v=−128127 F(u,v) e+i2π(ux+vy)/N

The normalization factor 1/N2 is omitted. Because the display divides by the maximum value, the result looks the same on screen with or without it. The calculation uses a fast Fourier transform (radix-2 FFT), but the results match the definitions above (see “8. Verification”).

Because both transforms are discrete, the image wraps around periodically: the top and bottom edges, and the left and right edges, are treated as neighbors.

3. Display processing

Raw results are hard to see, so the following processing is applied before converting them to brightness values from 0 to 1. What you see is a processed version of the result, so brightness is not proportional to the magnitude of the values.

What is shownDisplayed value
Reconstructed image I=(|f(x,y)|max|f|)0.85
The absolute value (magnitude) is divided by its maximum and brightened slightly (gamma 0.85).
k-space (result of the Fourier transform, or k-space taken from an image) L=ln(1+1000⋅|F|/max|F|)ln(1001)
The values are compressed logarithmically so that the center (DC component) does not drown out everything else (the views labeled “log scale”). Phase is not displayed.
k-space you drew yourself |F(u,v)| is shown as is (0 to 1).
Tiny points of 1–4 cells in k-space A single cell of the 256×256 grid is only a few pixels on screen and easy to miss. So for any group of nonzero cells connected horizontally, vertically or diagonally that has 4 cells or fewer and no other points or lines within 2 cells, each cell is drawn as a 5×5 block in the display only (e.g. the “Center point”, “Symmetric pair” and “Random” presets). The data used in the calculation is unchanged, and the reconstructed image is not affected.

Example: why the stripes look finer with “Symmetric pair”

The “Symmetric pair” preset puts the value 1 at (u,v)=(10,4) and (−10,−4). This gives f=2cos(2π(10x+4y)/N). Since the screen shows the absolute value |f|, the negative parts are also bright, so you see twice as many bright stripes as there are cosine periods.

4. Brush

Each brush dab adds a smooth Gaussian-shaped value centered on your finger (or mouse). The amount added to a cell at offset (dx,dy) from the center is:

Δ=s⋅ exp(−dx2+dy22r2)
Where and which toolHow the value changes
Image space, pen / eraserf←min(1,max(0,f+Δ))
k-space, penΔ is added to the real part. If the magnitude exceeds 1, it is clamped to 1 while keeping the phase.
k-space, eraserF←F⋅max(0,|F|+Δ)|F|
Only the magnitude is reduced; the phase is kept.

Symmetry mode (Hermitian symmetry)

When “Symmetry” is on, whatever you draw at (u,v) is also drawn at the point mirrored through the center, (−u,−v). k-space drawn in symmetry mode (and k-space taken from an image) satisfies

F(−u,−v)=F(u,v)‾

(the complex conjugate), so the inverse transform f(x,y) is real-valued. This gives a natural look, closer to an image of a real object. Note that the column u=−128 and the row v=−128 (the left and top edges of the screen) pair with themselves, because their counterparts would lie outside the grid.

5. Loading images

6. Undersampling (parallel imaging and half Fourier)

With “Undersampling” in k-space mode, you can reconstruct as if some lines of k-space had not been acquired. The phase-encoding direction is vertical on screen (v), and each horizontal row of k-space (all cells with the same v) is treated as one line obtained in one acquisition. Lines that are not acquired are set to 0 before the inverse transform in section 2. The k-space you drew is not changed; the undersampling is applied only for display and reconstruction, so choosing “None” brings everything back.

UndersamplingLines acquired
1/2, 1/3, 1/4Lines where v is a multiple of R (128 lines for R=2, 85 for R=3, 64 for R=4)
HalfThe 160 lines (5/8) with v≥−32: all of the lower half plus 32 lines above the center

1/2, 1/3 and 1/4: aliasing (fold-over)

When only 1 of every R lines is acquired, the inverse transform gives the original image overlapped with copies of itself shifted vertically by N/R (because the image wraps around periodically, parts that stick out reappear on the opposite side).

fR(x,y)= 1R ∑k=0R−1 f(x,y+kNR)

For R=3, N/R is not an integer, so the shifted copies fall between grid points and appear interpolated, slightly smeared. In real parallel imaging, several receiver coils with different sensitivities acquire simultaneously, and those differences are used to unfold the overlapping copies (SENSE, GRAPPA, etc.). When “Unfold with SENSE” is off, K-Space Paint assumes a single receiver coil and shows the folded image before unfolding. The calculation when it is on is explained in section 7.

Half Fourier: fill with 0 / fill by symmetry

Photos and the head phantom are real-valued images, so filling by symmetry restores them almost perfectly. In real MRI images, the phase varies from pixel to pixel (due to field inhomogeneity and other causes), so this filling alone does not restore the image. Real scanners estimate the phase from the symmetrically acquired lines near the center and correct for it (e.g. the homodyne method).

Display: lines that are not acquired are black, and lines filled by symmetry are shown at half brightness.

7. SENSE (receiver coil model)

Turning on “Unfold with SENSE” in the “Acquisition” tab simulates reception with multiple receiver coils and unfolds the aliasing with SENSE. Real results depend on the number, shape and arrangement of the coils, so a fixed model case is used here. It is available only with “None”, “1/2” and “1/4” undersampling (when N=256 is divisible by R).

Coil model

In coordinates where the image width and height run from −1 to 1, C coils (4, 8 or 16) are placed at equal intervals on a circle of radius 1.2, starting at the top and going clockwise. The sensitivity of coil c is:

sc(x,y)= α1+dc2/w2 eiφc , w2=0.12×8C

w and the phase rotation were chosen so that the g-factor for 8 coils at R=4 is similar to that of a real head coil. Over the region covered by the head phantom, the g-factor is:

Coilsg-factor at 1/2 (mean, max)g-factor at 1/4 (mean, max)
4 ch1.06, 1.163.82, 8.88
8 ch1.05, 1.132.88, 5.59
16 ch1.04, 1.072.28, 3.00

Reception and undersampling

The image obtained by inverse-transforming the k-space you drew (before undersampling) is treated as the object f, scaled so that its largest magnitude is 1. The image each coil gives when only 1 of every R lines is acquired is, as in section 6, a sum of copies shifted vertically by N/R:

ac(x,y)= ∑k=0R−1 sc(x,yk) f(x,yk) +nc(x,y) , yk=y+kNR

To keep the calculation light, this sum is built directly in image space instead of going through k-space (it was confirmed to match undersampling in k-space and inverse-transforming; see section 8). The noise nc has the same properties as adding equal noise to every k-space point and then undersampling: it is independent for each pixel and coil, and Gaussian with standard deviation σR (σ=0.01) in both the real and imaginary parts. Because only 1/R of the lines are acquired, the noise grows by R. The same random numbers are used every time, so the noise does not flicker when the image is redrawn.

Unfolding (SENSE) and the g-factor

For each pixel, the R overlapping values ρ are found from the values a of the C coils by least squares. S is the C×R matrix of sc(x,yk).

ρ=(SHS)−1SHa , gk=[(SHS)−1]kk[SHS]kk

Without undersampling (R=1), this is the same as combining the coil images weighted by their sensitivities. The g-factor tells how much more the noise grows on top of the R caused by undersampling:

SNRR=SNRfullgR

The g-factor is larger where the coil sensitivities differ less between the overlapping positions, and when R approaches the number of coils. The small map in the sidebar shows the g-factor (black = 1, white = 4 or more), with blue dots marking the coil positions. Without noise and without regularization, the original image is restored exactly in theory.

Why the noise splits into bands at 1/4

At 1/4, four points 64 pixels apart vertically (for example y=8,72,136,200) overlap as one set. Within a set, the points near the top and bottom coils have clearly different sensitivities and a small g-factor, while the middle points, far from every coil, have a large one. With 8 coils, the g-factor along the center column of the image is:

Vertical position y0–63 (top 1/4)64–191 (middle half)192–255 (bottom 1/4)
g-factorabout 1.85about 2.7–3.0about 1.85

The sudden change at the boundaries happens because a partner jumps. The set for y=63 is {63, 127, 191, 255}, with one partner at the very bottom edge, but the set for y=64 is {0, 64, 128, 192}, and that partner moves to the very top edge. The image wraps around periodically, so aliasing treats the top and bottom edges as neighbors, yet the coil sensitivities at the two edges are completely different. For the point at y=64, the top-edge point is a hard-to-separate partner seen strongly by the same top coil, so the g-factor rises there. On real scanners, too, the g-factor map forms bands every N/R along the phase-encoding direction, but the steps are usually less distinct because many coils are arranged in 3D, regularization is used, and the field of view leaves some margin.

Regularization

Turning on “Regularization” adds λI to the unfolding equation (Tikhonov regularization), which holds back the large noise amplification where sensitivities differ little and unfolding is hard.

ρ=MSHa,M=(SHS+λI)−1,gk=[MSHSM]kk[SHS]kk

In exchange, some aliasing is left unfolded and the signal is slightly weakened (even without noise, the original image is not restored). λ=0.03 is the value that minimizes the total error including noise for the head phantom at 1/4, for 4, 8 and 16 coils alike. With regularization, the g-factor can drop below 1 in places (shown black in the map).

1/4, head phantomg-factor (mean, max)Error without noise (remaining aliasing)Error with noise
4 ch, not regularized3.82, 8.880%42.5%
4 ch, regularized2.13, 2.9620.6%25.9%
8 ch, not regularized2.88, 5.590%19.9%
8 ch, regularized2.17, 3.9810.2%17.4%
16 ch, not regularized2.28, 3.000%12.0%
16 ch, regularized1.94, 2.425.1%11.0%

The error is the root sum of squared differences from the original image, divided by the root sum of squares of the original image.

Limits of the model case

8. Verification

The program's FFT results were checked against values computed by directly summing the definitions above (October 3, 2026).

9. Differences from real MRI

The “k-space” in K-Space Paint is simply the 2D discrete Fourier transform of the image. Real MRI differs in the following ways.

Last updated: October 7, 2026