Matlab/Octave fftshift utility
Matlab and Octave have a simple utility called fftshift that performs this bin rotation. Consider the following example:
octave:4> fftshift([1 2 3 4]) ans = 3 4 1 2 octave:5>If the vector [1 2 3 4] is the output of a length 4 FFT, then the first element (1) is the dc term, and the third element (3) is the point at half the sampling rate ( ), which can be taken to be either plus or minus since they are the same point on the unit circle in the plane. Elements 2 and 4 are plus and minus , respectively. After fftshift, element (3) is first, which indicates that both Matlab and Octave regard the spectral sample at half the sampling rate as a negative frequency. The next element is 4, corresponding to frequency , followed by dc and .
Another reasonable result would be fftshift([1 2 3 4]) == [4 1 2 3], which defines half the sampling rate as a positive frequency. However, giving to the negative frequencies balances giving dc to the positive frequencies, and the number of samples on both sides is then the same. For an odd-length DFT, there is no point at , so the result
octave:4> fftshift([1 2 3]) ans = 3 1 2 octave:5>is the only reasonable answer, corresponding to frequencies , respectively.
Index Ranges for Zero-Phase Zero-Padding
Zero-Padding for Interpolating Spectral Peaks