The need for shifting either the signal or the spectrum arises from constraints imposed by DFT/IDFT. These functions have expectations on a reference in their input, the sample corresponding to time t=0 (for the DFT), and to frequency f=0 (for the IDFT). Both functions also output their result using the same convention. Just before presenting shift / inverse shift functions, let's cover what DFT/IDFT expects.
DFT constraints
Periodicity constraint
The input sequence is assumed to be a complete period of the signal and can be juxtaposed to create a periodic signal, e.g. with a 100-sample:

Any 100-sample section of this signal could be used to take the DFT. Example with sections starting at magenta marks above:

The DFT magnitude doesn't change, but the time difference is taken into account in the phase (last row). For the DFT a delay in the signal results in the corresponding spectrum being multiplied by a complex exponential exp(j.2.π.f0.t). As the magnitude is 1, the product only adds 2.π.f0 to the spectral coefficient angles, hence the phase difference.
Ordering constraint
It means DFT evaluates the delay: It assumes the first sample is used for t=0, and computes phases relative to it. The output of the DFT is itself periodic with period N, N being the number of samples in a period:

(Only the magnitude is shown here, but the phase has the same periodicity.)
IDFT constraints
Like previously we could take any 100-sample section of the previous sequence and take an IDFT:

IDFT has the same input constraints than DFT: The input spectrum is assumed to be periodic, the first sample is for f=0 (DC). Any frequency shift in the spectrum corresponds to a multiplication of the signal by a complex exponential with magnitude 1. As for the DFT, this only changes the phase of the output.
Closer look to the horizontal scales
The time corresponding to sample k (k between 0 and N-1) is t=k/N.Ts, Ts being the sampling period, and frequency sample k corresponds to frequency k/N.Fs, fs being the sampling frequency.
Therefore there are no negative frequencies in the output of the DFT, and no negative frequencies are expected in the input of the IDFT.
However, due to the inherent periodicity of DFT and IDFT, we are allowed to translate the spectrum from range 0 to fs to range -1/2fs to +1/2fs. Now we have a spectrum with half of the frequencies negative, but still located after the positive frequencies in the sequence.
Function fftfreq
can be used to create the scale with positive and negative frequencies. This function has no direct connection with DFT/IDFT, this is just a helper which knows how to determine the required frequencies values.
The time range can be shifted from 0 to Ts to -1/2Ts to +1/2Ts the same way due to periodicity N.Ts.
Use of fftshift
and ifftshift
Now let's answer your question about shifting.
Scales with with half positive and half negative indices are convenient for plotting symmetrical views. But there is a slight problem, negative indices are placed after 0 and positive indices.
fftshift
and ifftshift
can be used to reorder elements: fftshift
prepares the sequence for plotting purpose, ifftshift
restores the native order used/expected by DFT/IDFT and described in the first part. Note these functions perform no other action than reordering elements, they are not directly related to FT in spite of their names contains 'fft', and prefix 'i' has no connection with IDFT.
This reordering operation can be seen as a rotation within the sequence, or as a linear shift along the periodic sequence (signal or spectrum).
One can be surprised there are two functions to do the same change. Sequences with an even length have the same number of positive (DC/0 included) and “negative” frequencies. For odd-length sequences, there is one additional negative frequency. Both “half” are not the same length, this must be taken into account. So fftshift
manage the size this way:
for k in axes:
n = tmp.shape[k]
p2 = (n+1)//2
mylist = concatenate((arange(p2, n), arange(p2)))
y = take(y, mylist, k)
return
Note how the location of the negative half is computed: p2 = (n+1)//2
. And ifftshift
is identical except it uses this location: p2 = n-(n+1)//2
.
As only the user knows whether the sequence has been already swapped, he/she is responsible for using the appropriate function.
Bottom line: There is usually no need to shift or unshift sequences. DFT and IDFT expect and output sequences with index 0 as the first sample. A possible case is when a sequence is created centered, and has to be processed by DFT/IDFT. It must be unshifted with ifftshift
before being used as input.
Shifting (fftshift
) maybe required to center the sample for 0, either the DC frequency or time t=0, for symmetrical display purpose. If a shifted sequence must be later reused with DFT/IDFT functions, then you can restore the native order (ifftshift
). Track carefully whether a sequence is currently shifted or not, as this determines which function to use.
