Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
FFT pad or no pad

Subject: FFT pad or no pad

From: shinchan

Date: 20 Aug, 2009 20:40:27

Message: 1 of 4

I have 300 data points collected at 1Hz. I want to do FFT on this time series, then square it to get to power spectrum. Should I pad it with zero to make it 512? fft function lets me do it either way, but the fft results are different. So I don't quite know what's going on.

Subject: FFT pad or no pad

From: Rune Allnor

Date: 20 Aug, 2009 20:52:29

Message: 2 of 4

On 20 Aug, 22:40, "shinchan " <shinchan75...@gmail.com> wrote:
> I have 300 data points collected at 1Hz. I want to do FFT on this time series, then square it to get to power spectrum. Should I pad it with zero to make it 512?

Why would you want to do that?

> fft function lets me do it either way, but the fft results are different.

Of course they are. But maybe not as different as you think:

x= sin((0:299)*19/299*pi);
X = fft(x);
Y = fft(x,512);
plot((0:299)/300,abs(X),'b',(0:511)/512,abs(Y),'r')

> So I don't quite know what's going on.

Then find out. Read a book on DSP, e.g.

Lyons: "Understanding DSP", Prentice Hall, 2006.

Rune

Subject: FFT pad or no pad

From: Cheryl

Date: 20 Aug, 2009 21:24:19

Message: 3 of 4

On Aug 20, 1:40 pm, "shinchan " <shinchan75...@gmail.com> wrote:
> I have 300 data points collected at 1Hz. I want to do FFT on this time series, then square it to get to power spectrum. Should I pad it with zero to make it 512? fft function lets me do it either way, but the fft results are different. So I don't quite know what's going on.

I have recently done a bunch of FFTs in Matlab, and I agree the
documentation is really confusing. I went ahead and followed Numerical
Recipes and wrote my own code in 1D then figured out what Matlab was
doing, which is, well, confusing. Here's some code to get you started,
but I highly recommend you read up on it:

% My Fourier Transform

close all
clear all

% h(x) = ....
% N is number of gridpoints, resolution of h(x)

% make up data:
dx = 0.1;
x = 0:dx:5-0.1;

% N = 50; % has to be even
% n = -N/2:N/2;
% f = n./(N*dx); % from -5 to 5 in this case

N = 50;
n = 0:N-1;
f = n./(N*dx); % = n/Lx

% f = 1.6:
h = cos(2*pi*x*3) + sin(2*pi*x*1.6)+2;
% cos and constant go to real, sin goes to imag H
figure
plot(x,h,'-k.')
% plot(x(1:10),h(1:10))
% hold on
% plot(x(1:10),h(1:10),'k.')
xlabel('x')
ylabel('h(x)')


H = x;
H(:) = 0;
sum = 0;

count = 0;
%for c = -N/2:1:N/2
for c = 0:N-1
    count = count +1;
    for k = 0:N-1
        sum = sum + h(k+1)*exp(-2*pi*i*k*c/N);
    end
    %sum = sum*dx;
    H(count) = sum;
    sum =0;
end

figure;
subplot(2,1,1)
%plot(f,real(H))
plot(n,real(H))
ylabel('re(H)')
hold on
%plot(f(34),real(H(34)),'*')

subplot(2,1,2)
%plot(f,imag(H),'k')
plot(n,imag(H),'k')
xlabel('n')
ylabel('im(H)')
hold on
%plot(f(34),imag(H(34)),'k*')


% Inverse finite transform:
count = 0;
%for c = -N/2:1:N/2
for c = 0:N-1
    count = count +1;
    for k = 0:N-1
        sum = sum + H(k+1)*exp(2*pi*i*k*c/N);
    end
    %sum = sum/(N*dx);
    sum = sum/N;
    h_back(count) = sum;
    sum = 0;
end

figure(1)
hold on
plot(x,h_back,'-b.')
plot(x,imag(h_back),'g')
% reproduces the data perfectly


% now check Matlab:
H_m= fft(h);

figure;
subplot(2,1,1)
plot(n,real(H_m),'r--')
ylabel('re(H_m)')
hold on


subplot(2,1,2)
plot(n,imag(H_m),'r--')
xlabel('f')
ylabel('im(H_m)')
hold on

% now back w/ Matlab:

h_m = ifft(H_m);

figure
plot(x,h_m,'-k.')
% plot(x(1:10),h(1:10))
% hold on
% plot(x(1:10),h(1:10),'k.')
xlabel('x')
ylabel('h_m(x)')

hold on;
plot(x,h,'ro')

figure(1)
hold on
h_back_m = ifft(H);
plot(x,h_back_m, 'gs')

Subject: FFT pad or no pad

From: daniel

Date: 20 Aug, 2009 22:03:58

Message: 4 of 4

short answer: just use the 300 time points.

medium answer: padding to a power of 2 is required on some numerical
packages but it is not required on matlab. This is mainly a speed
issue. if the number of data points is a power of 2 the computation
can be much faster than if it is, say, a prime number of data points.
For 300 data points even doing the algorithm the slow way is going to
be so fast on any modern computer that you won't even notice. You
don't really need to worry about this until you get to maybe 100,000
data points.

long answer: remember that an FFT (fast fourier transform) is an
implementation of the discrete fourier transform (DFT). The DFT
applies to periodic discrete-time data. The non-periodic (infinite
sample length) discete equivalent to the DFT is the DTFT (discrete
time fourier transform). The DFT is a sampling of the DTFT. As you
pad your sample with zeros, your DFT becomes closer to the DTFT. So,
the zero padding can give you better frequency resolution (up to a
point). HOWEVER (and I suspect this is why report different answers
with the zero pad), you MUST window your data before you zero pad, or
else you'll introduce spectral leakage.

On Aug 20, 4:40 pm, "shinchan " <shinchan75...@gmail.com> wrote:
> I have 300 data points collected at 1Hz. I want to do FFT on this time series, then square it to get to power spectrum. Should I pad it with zero to make it 512? fft function lets me do it either way, but the fft results are different. So I don't quite know what's going on.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us