Padding a Signal with Zeros

Discussion in 'Programmer's Corner' started by battlehands, Aug 17, 2011.

  1. battlehands

    Thread Starter New Member

    Jul 29, 2011
    13
    0
    I have a signal of length 38.7360 seconds. I need my signal to be 40 seconds in length. I need to pad the original signal with zeros so that my signal will be 40 seconds in length. My sampling frequency is 22050 Hz.

    I need 27872 zeros to pad the signal.

    I tried y = [y x], where x = zeros(1,27872); but it said my dimensions dont match.
     
  2. battlehands

    Thread Starter New Member

    Jul 29, 2011
    13
    0
    Figured it out. I needed to transpose my y matrix so that the dimensioned matched.
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Be careful when padding data with zeros. You are actually multiplying with a rectangle.
    Hence the frequency transform will be a spectrum convolved with a sinc function.
     
  4. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    That's not quite true.

    Actually, padding with zeros doesn't affect your windowing at all. You're multiplying by the same window despite how many zeros are added to the end. (recall a rectangular window is defined as zero outside the window width anyway). You are on the right track, though... when taking ANY FFT, you are actually multiplying by a rect function in the time domain. Thus why you have spectral leakage.

    Zero padding before transforming will actually increase your frequency resolution... you're really just interpolating.

    http://blinkdagger.com/matlab/matlab-fft-and-zero-padding/
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Sorry, total garbage.

    Your resolution in frequency space is 1/T where T is the total record length.
    By padding with zeros, you convolve with the sinc function and hence lose the resolution that you had gained by padding with zeros.

    Common sense would tell you that you cannot gain resolution by adding more zero data points!
     
    Last edited: Aug 18, 2011
  6. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    False.

    You certainly can. I am not saying you get more data. No free lunch here. You are interpolating in the frequency domain.

    Try it. Go to matlab and take the fft of a sine wave and the fft of a zero padded sine wave. You will get very similar spectrums. Oh, except the zero padded one will have better resolution.

    And no, padding zeros does not convolve with sinc. That implies you are changing your window function in the time domain. Which you are not. Yes, you are convolving with a sync the moment you take the FFT because it is a block processing algorithm... you must window by definition. Windowing with a rect => convolve with sync. But padding zeros does not change the window function, so to imply that padding with zeros makes the problem more pronounced is fallacious.

    More references:
    https://ccrma.stanford.edu/~jos/st/FFT_Zero_Padded_Sinusoid.html
     
    Last edited: Aug 19, 2011
  7. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    I think you are confusing the resolving power of your instrument with the resolution of your results.

    Here is an example.
    Suppose you had a voltmeter that can resolve to 0.1 volts.
    You take a voltage measurement of a signal of 3.5 volts but notice +/- 0.1V fluctuations.
    Hence your reading fluctuates from 3.4 to 3.6 volts.

    Now you take a better voltmeter that can resolve to 0.01 volts.
    The readings you get of the same signal will show about 3.40 to 3.60 volts.
    Your resolving power has increased by a factor of 10 but the variance of the observed data is still the same.

    Similarly, when you examine the frequency spectrum, your resolving power has increased but the width of the frequency peak is not improved.

    Read the article to the link you posted very carefully.

    "Conclusion

    It is a common misconception that zero-padding adds more information. Zero padding adds NO NEW information. The perceived benefit of zero-padding is increased spectral resolution. You are getting better resolution, but the key is to realize that there is NO NEW information added from the zero-padding. Zero-padding is useful, but it should not be a substitute for taking larger data samples. If you had to choose between taking twice as much data, or to zero pad your data, the answer is to ALWAYS take more data. "




    Again your resolving power has improved but you learn nothing new about the frequency of your dominant frequency.
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361

    More nonsense.

    Padding with zeros increases the total record length while the recorded data remains the same. How can you say that the window function has not changed?

    The more zeros you add, the wider your sinc function becomes. The window function has changed and so has the FFT of the window function.
     
  9. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    You've now switched sides and are now arguing my point. If you read my post, you will see that I said:
    I completely agree that taking more data is not the same as zero padding. That was never my point.
    True that the length of your signal changes. Again, wrong that the window function is changing. Look at the definition of the rect function (ref wolfram mathworld:

    Edit: Sorry, the picture wouldn't copy. Its right at the top of this page.
    [​IMG]http://mathworld.wolfram.com/RectangleFunction.html


    This signal is defined for ALL time. It is zero outside of the rectangle. It changes nothing to add a few zeros onto the end of it before you multiply with your signal... you are still multiplying by zero outside of the rect whether you pad them on or they are implicit in the algorithm! You are not changing your sinc function at all.

    Run this script:
    Code ( (Unknown Language)):
    1.  
    2. rect = [zeros(1, 10) ones(1, 10), zeros(1, 10)];
    3. rect_padded = [rect zeros(1, 100)];
    4.  
    5. figure
    6. plot(fftshift(abs(fft(rect))))
    7. figure
    8. plot(fftshift(abs(fft(rect_padded))))
    9.  
    10.  
    Sync function does not change with zero padding (except resolution). Period.

    P.S. the width of the sync function is defined by the width of the rectangle, not by how many zeros are at the end (which is infinite, by the way)
     
    Last edited: Aug 19, 2011
Loading...