原创 Lesson 4 4:MATLAB - FFT and Zero Padding

2012-11-28 14:24 4688 12 13 分类: 测试测量

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

This is the fourth post in the blinkdagger signal processing series.
Introduction

Matlab Logo In the previous tutorial, we showed you how correctly scale your x-axis so that your FFT results were meaningful. In this post, we will be discussing zero-padding, a method that can help you better visualize/interpret your fft results. Zero-padding means that you append an array of zeros to the end of your input signal before you fft it. Luckily, the fft command within Matlab makes it very easy to zero-pad.
Why Zero Pad?

Two reasons that you might want to zero pad is to increase the number of data points to a power of 2. Traditionally, the FFT algorithm is more efficient when it is dealing with signals that contain 2^N data points. Nowadays, this isn’t as important with the modern algorithms. Another reason for zero-padding is for a “better” resolution in the frequency spectrum. We’ll discuss why quotes are used around better in a bit. In general, zero-padding can prove quite useful and should be used when using the fft command. In practice, it is helpful to zero pad a signal to 4 times it’s original length, giving you a 4-fold increase in the frequency resolution.

A Simple Example

Let’s use the same signal as the previous tutorial as our example signal:

Sine Wave Equation
fo = 4; %frequency of the sine wave
Fs = 100; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:1-Ts; %sampling period
n = length(t); %number of samples
y = 2*sin(2*pi*fo*t); %the sine curve
%plot the cosine curve in the time domain
sinePlot = figure;
plot(t,y)
xlabel('time (seconds)')
ylabel('y(t)')
title('Sample Sine Wave')
grid

Sine Wave in Time Domain

But now, lets append some zeros to the end of this signal:

Case 1: Number of points = 128
Case 2: Number of points = 256
Case 3: Number of points = 512

This is what the signal would look like for these three cases:

Zero Padding for Input Signal
Taking the FFT of the Zero-Padded Signals

Luckily, the fft command within Matlab makes zero-padding extremely easy. The fft command has a second argument that allows you to specify how many data points the fft command will return. I modified the code I presented in the last tutorial to allow for zero padding and used it here:
%remember to save this function as an m-file!
function [X,freq]=centeredFFT(x,Fs,N)
% this is a custom function that helps in plotting the two-sided spectrum
% x is the signal that is to be transformed
% Fs is the sampling rate
% N is the number of points returned in the FFT result
%this part of the code generates that frequency axis
if mod(N,2)==0
    k=-N/2:N/2-1; % N even
else
    k=-(N-1)/2N-1)/2; % N odd
end
T=N/Fs;
freq=k/T; %creates the frequency axis
X=fft(x,N)/length(x); % normalizes the data
X=fftshift(X);%shifts the fft data so that it is centered

Now, lets take the fft of these three signals.
Why Does My Output Look Like a Sinc?

When we pad with zeros, we are effectively multiplying a rectangular box with the sinusoid in the time domain. In the frequency domain, this translates into convolving a sinc function with an impulse resulting in a sinc-like output!(You can download the script that I used in creating these plots at the end of this tutorial).
FFT Different Resolutions

As you can see, there are more sampled points as N gets larger, and it is easier to see the general shape of the spectrum. The larger your N is, the finer the sampling will be. If I keep on increasing the number of zeros, it will make my frequency spectrum more refined. So does this mean I can just zero-pad my signal and get a better FFT spectrum every time? Well, technically speaking, you get more frequency bins when you zero pad, so yes, you do get a higher resolution in the frequency domain. But the caveat is that there is no new information added when you zero-pad. (补零多,频率域的分辨率增加,但是并没有获得新的信息)

When you increase the size of N, all you are really doing is interpolating the data to obtain more sample points. There is NO new information added when zero-padding is applied. The resolution has increased, but I repeat, THERE IS NO NEW INFORMATION ADDED! This might sound confusing and paradoxical, but it is a very important point.

If you have the option to take more data, it is ALWAYS better to get more data than to zero pad. Zero-padding is NOT a substitute for taking more data!
Another Example on how Zero Padding can Help

    In this .example, we are going to use zero padding to help us distinguish between two peaks that would otherwise be difficult to distinguish.

    The example signal has f1 = 4 Hz and f2 = 4.5 Hz (meaning it will have a peak at 4 Hz and a peak at 4.5 Hz):

    [tex]y(t)=100sin(2\pi{f}_{1}t)+100sin(2\pi{f}_{2}t)[/tex]

    The following MATLAB code is used to generate the curve. Insert this into the MATLAB command prompt
    f1 = 4; %frequency of the first sine wave
    f2 = 4.5; %frequency of the second sine wave
    Fs = 100; %sampling rate
    Ts = 1/Fs; %sampling time interval
    t = 0:Ts:2-Ts; %sampling period
    y = 100*sin(2*pi*f1*t) + 100*sin(2*pi*f2*t); %sum of sine waves
    plot(t,y)
    title('Sample Signal', 'FontWeight','Bold')
    xlabel('Time (seconds)')
    ylabel('Amplitude')
    grid

    Sample Signal

    Now, lets take the FFT of the signal without zero padding. Theoretically, we expect two peaks. One peak at 4 Hz, and another at 4.5 Hz.

    No Zero Padding

    As you can see, it is difficult to tell that there are two peaks in this plot. It looks like the peaks got smashed together.

    Now, lets apply some zero padding up to N = 1024 points.

    The following function is used to perform this:
    %remember to save this function as an m-file!
    function [X,freq]=positiveFFT_zero_padding(x,Fs,N)  
    k=0:N-1; %create a vector from 0 to N-1
    T=N/Fs; %get the frequency interval
    freq=k/T; %create the frequency range
    X=fft(x,N)/length(x); % normalize the data  

    %only want the first half of the FFT, since it is redundant
    cutOff = ceil(N/2);  

    %take only the first half of the spectrum
    X = X(1:cutOff);
    freq = freq(1:cutOff);

    At the Matlab Command prompt, type the following to obtain the plot shown below:
    fftPlot2 = figure
    set(fftPlot2,'Position',[500,500,600,300])
    zeroPadFactor = nextpow2(length(y)) + 3;
    [a,b] = positiveFFT_zero_padding(y,Fs,2^zeroPadFactor);
    plot(b,abs(a))
    title('FFT of Sample Signal: Zero Padding up to N = 1024', 'FontWeight','Bold')
    xlabel('Freq (Hz)')
    ylabel('Magnitude')
    grid
    xlim([0 10])

    Zero Padded to 1024 Points

    But didn’t we just say that no new information is added? How were we able to distinguish these two peaks if no new information is added?

    The information was always there, but it was “hidden” in a way. When we took the FFT of the signal without zero padding, the frequency bins were not fine enough to differentiate the two peaks. By the sampling theorem, as long as you sample a bandlimited signal under the Nyquist rate, you know everything you need to know to perfectly reconstruct the signal. Therefore, at that point, you’ve done the best you can do; you can zero-pad all you want to get more points at the output of your FFT, but you don’t get any new information, because you already have all the information there is on the continuous-time signal.

    Inherently, there is nothing wrong with zero-padding in itself. You just have to be careful in its application. Whenever I use the fft command, I tend to use an N that is 4 times larger than the amount of data points within my signal. Zero padding cannot hurt your FFT result.

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.
Download Source Files

Download the source files that were used in creating the plots.
Sources and References

Zero Padding Does Not Buy Spectral Resolution

Spectrum Analysis Using the Discrete Fourier Transform

Getting to Know the FFT

文章评论1条评论)

登录后参与讨论

用户434397 2012-11-8 10:40

怎么没见具体的内容啊?
相关推荐阅读
xuyaosong 2012-11-28 13:54
C语言中 多个源文件之间函数如何调用
首先要建立一个头文件,以.h保存 这样 #include typedef struct { char name[100][60]; char number[100][8]; int m...
xuyaosong 2012-11-28 13:53
小波变换尺度相关性去噪程序
所实现的相关性去噪函数为function [s1 a d] = SSNF(s, n, h, g, g1),具体的实现步骤为: 1) 调用离散二进小波分解函数对信号进行分解,得到逼近系数a 和细节...
xuyaosong 2012-11-28 13:29
功率谱密度幅值的具体含义??
http://www.chinavib.com/forum/thread-17307-1-48.html 求信号功率谱时候用下面的不同方法,功率谱密度的幅值大小相差很大! 我的问题是,计算具体...
xuyaosong 2012-01-12 18:38
声明函数指针并实现回调
程序员常常需要实现回调。本文将讨论函数指针的基本原则并说明如何使用函数指针实现回调。注意这里针对的是普通的函数,不包括完全依赖于不同语法和语义规则的类成员函数(类成员指针将在另文中讨论)。 ...
xuyaosong 2012-01-12 18:36
如何不用访问地址的方式来编写并口程序 ★★★★★
兄弟我在精华发现了一些关于并口打印的文章,但是都是用inb outb操作的,不知道各路高人能否给小弟一个用open write ioctl read close控制的判断状态并读写的例子,尤...
我要评论
1
12
关闭 站长推荐上一条 /2 下一条