Signal processing · index arithmetic
Circular Shift of a Matrix (Shift Matrix)
A circular shift of a matrix slides its rows, its columns, or both by a fixed number of positions, and whatever leaves one edge re-enters at the opposite edge. Nothing is lost and nothing is invented, so the operation is a pure permutation of the entries. Shifting a matrix this way is common when data is periodic: an image treated as a tile, a spectrum whose ends are the same frequency, or a sequence of states that cycles. The operation turns up repeatedly in signal processing.
Definition
For an M×N matrix A and a row shift s, the shifted matrix B is
B(i, j) = A((i − s) mod M, j)
so that the row that was at position i moves to position i + s. A positive s moves content downward; a negative s moves it upward. Column shifts act on the second index in the same way, and shifting both indices gives a diagonal movement. The modulo makes the index wrap, and it is the reason the term circular is used.
Worked examples
Take A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]].
A row shift by 1 sends row 0 to position 1, row 1 to position 2 and row 2, which falls off the bottom, to position 0. The result is [[7, 8, 9], [1, 2, 3], [4, 5, 6]].
A column shift by 1 moves each column one place right, and the last column wraps to the front: [[3, 1, 2], [6, 4, 5], [9, 7, 8]].
Shifting both by 1 gives [[9, 7, 8], [3, 1, 2], [6, 4, 5]]. Check one entry: the 1, originally at row 0 and column 0, ends up at row 1 and column 1, which is the middle of the result. Repeating the shift three times in all returns every entry to its place, since the shifts are taken modulo the size 3.
Properties
Each shift is a permutation, so the sum, the set of values, and the matrix norm are all unchanged. A row shift can be written as multiplication from the left by a permutation matrix P with ones on a wrapped diagonal, and a column shift as multiplication from the right by another one. For a 3×3 matrix the left shift matrix by 1 has rows [0, 0, 1], [1, 0, 0], [0, 1, 0]. Multiplying P·A reproduces the row shift shown above, which is a handy check. P is orthogonal, so its inverse is its transpose and equals a shift by −1. Powers of P give shifts of larger amounts, and PM is the identity.
When a negative shift is requested or the amount exceeds the size, reduce it first with the modulo; shifting an M-row matrix by M + 2 is the same as shifting by 2.
Link to the Fourier transform
Circular shifts are the shifts that the discrete Fourier transform understands. Shifting a sequence by s positions multiplies bin k of its spectrum by the phase factor e−j2πks/N. The magnitude of the spectrum is unchanged and only the phase turns, linearly in k.
A well-known special case is fftshift, a circular shift by half the length that moves zero frequency to the center. Applied to a two-dimensional spectrum, it shifts both rows and columns by half. Seen through the inverse discrete Fourier transform, a shift of the data is nothing more than a phase change in the spectrum.
Where it shows up in two dimensions
When an image is convolved with a kernel by means of the Fourier transform, the result is circular: edges wrap around, so the left border of the output is contaminated by the right edge of the input. A shift of the kernel is sometimes needed to center it. Two-dimensional convolution describes the direct, non-circular alternatives and how padding avoids the wrap. The same index arithmetic sits behind periodic boundary conditions in grid simulations.
Pseudocode
function circshift_rows(A, s):
M = rows(A)
for i in 0 .. M-1:
B[(i + s) mod M, :] = A[i, :]
return B
Take care that the modulo of a negative number returns a negative result in some languages; add M before reducing.