# Why is there a difference between MATLAB's FFT of a zero-padded signal compared to the non-padded case

I created a data signal and calculated the spectrum via the fft-command in MATLAB. The data-signal itself is not periodic but it is created in a way that it can be treated as one period of a periodic signal. The last sample smoothly transitions to the first one. I observe that the absolute of the spectrum strongly depends on the zero-padding before calculating the fft. If I do zero-pad the signal such that its length is a power of two, the spectrum differs severely from the one where I kept the signal unchanged. In the following pictures you can see the results.

It can be clearly seent that there is a kind of gap at the center of the channels without zero-padding. I can't explain this. I would expect the shape of both transforms to be the same. Why is that not the case here, does anyone have an idea?

• You may be observing spectral leakage in the first plot, although without seeing more details of your simulation it's hard to say. Mar 24 '14 at 12:52

If you write

FFT(data)


you basically sample a continous "spectral curve" at length(data) many points. If you do zero padding

FFT(data,len)


where length(data)<len you effectively sample the curve at more points (len) that are more closely spaced. But it is still is the same curve. It was just sampled at different frequencies.

If this is really the only difference you made, then you're probably witnessing a "sampling issue". By that I mean the curves appear to be different because you've sampled it at different points and there is some oscillations going on. Chances are that you havn't applied any windowing which would smooth out the curve a bit and make this effect (otherwise known as spectral leakage) less likely.

You could try one of the following things:

• do even more zero padding for an increased sample rate (samples/Hz) and check out the shape
• apply a window function to smooth out the curve before the FFT
• use PWELCH

PWELCH does more than simply an FFT. It includes windowing and averaging of overlapping but smaller windows. This is probably what you should be doing given your "non-periodic" hint.

• And if the end really smoothly joins the start ("locally periodic"), maybe you don't really want to do zero padding nor windowing. It depends on your goal. Mar 24 '14 at 13:05
• Thank you very much for your answer, sellibitze. Yes, the only difference between the plots is what you wrote. One is fft(data), the other one fft(data, NFFT). If I apply more zero padding, the shape remains the same as with the normal zero padding above, no gap arises between the channels. I tried out the pwelch command and it gives me also no frequency gaps. That is all very nice but I still don't really understand where those gaps come from. Where does the leakage come from? I just calculate the fft of a signal that fulfills periodic boundary conditions, I don't get it :( Mar 24 '14 at 13:19
• My goal is just to see the real PSD of the signal. I want to verify if the signal I created is what I expect it to be. The zero-padded version meets my expectations. That is exactly how I want the PSD of the signal to be. But the other version of the PSD without zero-padding irritates me. Those gaps are not desired and I don't know why they are there. Mar 24 '14 at 13:33
• As I said, it is a sampling issue. You just happen to sample the curve at some positions where the curve is lower near the center of your peaks. and if you shift the sampling point slightly to the left or right, you get a very different value. Leakage makes your spectral curve oscillate. Windowing smoothes the spectral curve and you get more consistent results. Mar 24 '14 at 13:39
• @Sebastian: So, if you want a smooth curve, do windowing or even PWELCH. PWELCH tends to give you a very smooth curve because it does windowing + averaging over smaller overlapping blocks. Mar 24 '14 at 13:41