The previous zero-padding example used the causal Hamming window, and the appended zeros all went to the right of the window in the FFT input buffer (see Fig.2.4a). When using zero-phase FFT windows (usually the best choice), the zero-padding goes in the middle of the FFT buffer, as we now illustrate.
Figure 2.6a shows a windowed segment of some sinusoidal data, with the window also shown as an envelope. Figure 2.6b shows the same data loaded into an FFT input buffer with a factor of 2 zero-phase zero padding. Note that all time is ``modulo '' for a length FFT. As a result, negative times map to in the FFT input buffer.
Figure 2.7a shows the result of performing an FFT on the data of Fig.2.6b. Since frequency indices are also modulo , the negative-frequency bins appear in the right half of the buffer. Figure 2.6b shows the same data ``rotated'' so that bin number is in order of physical frequency from to . If is the bin number, then the frequency in Hz is given by , where denotes the sampling rate and is the FFT size.
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.
Having looked at zero-phase zero-padding ``pictorially'' in matlab buffers, let's now specify the index-ranges mathematically. Denote the window length by (an odd integer) and the FFT length by (a power of 2). Then the windowed data will occupy indices 0 to (positive-time segment), and to (negative-time segment). Here we are assuming a 0-based indexing scheme as used in C or C++. We add 1 to all indices for matlab indexing to obtain 1:(M-1)/2+1 and N-(M-1)/2+1:N, respectively. The zero-padding zeros go in between these ranges, i.e., from to .
To summarize, zero-padding is used for
- padding out to the next higher power of 2 so a Cooley-Tukey FFT can be used with any window length,
- improving the quality of spectral displays, and
- oversampling spectral peaks so that some simple final interpolation will be accurate.
Some examples of interpolated spectral display by means of zero-padding may be seen in §3.4.
Rectangular Window Side-Lobes
Zero Padding in the Time Domain