Question about fft

4 views (last 30 days)
Esteban Zegpi
Esteban Zegpi on 8 May 2012
I was doing a small program to graph the fft of a discrete signal, and to validate it started trying some functions whose Fourier transforms are known, I initially tried "sin(2*pi*w*t)" and "cos(2*pi*w*t)" and the result was, as expected, a "spike" at -w and w (when plotting the absolute value of the fft), then I tried the rectangle function (I think it's also known as the top hat function), and i expected to get something similar to "sinc(x)" as an answer, but instead i get the following graph: Not_Sinc, the original signal is Rect. If I zoom the image around 0 I can see something that looks like sinc, but there seems to be a delta in 0 that should not be there.
I tried increasing the sampling frequency and the results get worse.
Thanks in advance for your answers!
The code I used is here:
clear all
close all
clc
Fs=5000; %[Hz] Sampling frequency
x=(-1:1/Fs:1)'; %Time
y=zeros(size(x)); %Preallocate vector
for i=1:length(x) %Awfully ineficcient way to create rect function
if x(i)>=-0.5 && x(i)<=.5
y(i)=1;
end
end
%Plot of the signal
plot(x,y)
xlabel('time(s)');
ylabel('Amplitude');
axis([-Inf Inf -0.1 1.1]);
Y=fft(y);
Y=fftshift(Y);
d=Fs/length(Y(:,1)); %delta for the frequency vector
F=-Fs/2:d:Fs/2-d; %limits due to Nyquist
figure
plot(F,(sqrt(Y.*conj(Y))))
xlabel('Frequency [Hz]')
ylabel('Power')

Answers (3)

Dr. Seis
Dr. Seis on 8 May 2012
The way it is implemented above, the value at 0 frequency is just the sum of your signal from -1 seconds to 1 second (you have about 5000 samples equal to 1 and about 5000 samples equal to 0)... so the expected amplitude should be about 5000.
A couple of suggestions that won't change much:
y = zeros(size(x));
y(x(i) >= -0.5 & x(i) <= 0.5) = 1;
and
Y = fft(y)/Fs; % Normalize FFT output
% to take into account that Fs is 5000, not 1
and
abs(Y) is equivalent to sqrt(Y.*conj(Y))
EDIT May 09, 2012
To plot something that looks more like a sinc, change your line:
plot(F,(sqrt(Y.*conj(Y))))
to
plot(F,real(Y))
and narrow your x-limits to +/- 6 (i.e., "xlim([-6 6])")
To confirm it is what you expect, create the sinc-like function you expect. From the "DFT pairs" link below, this would look something sort of like:
Z = zeros(size(Y));
Z(5001)=1;
Z(5002:5051) = -1*sin(pi*10000*(1:50)/20001)./(10000*sin(pi*(1:50)/20001));
Not sure why I needed the -1, but the positive frequency amplitudes seem to line up pretty well with what the expected shape is when you plot:
plot(F,real(Y),F,Z);
Just make sure you normalize the amplitudes in "Y" by Fs as I indicate above!
Also, to get an even time series change:
x=(-1:1/Fs:1)';
to
x=(-1:1/Fs:1-1/Fs)';

Esteban Zegpi
Esteban Zegpi on 9 May 2012
I know the way to create the vector isn't very elegant, I didn't care because I will read the input from files later on, so it was a non issue. And about the normalization, that will not change the fact that the value at f=0 should be the area of the square and not the amount of samples, as you can see here DFT pairs, the "discrete sinc" should be the Discrete Fourier Pair of the rect function.
Regards
  3 Comments
Esteban Zegpi
Esteban Zegpi on 9 May 2012
What I ment to say was that normalizing by the sampling frequency would not change the fact that the "sinc" function would still have a spike in f=0, and look nothing like a sinc. I realize that the normalization you proposed is correct.
Dr. Seis
Dr. Seis on 9 May 2012
It doesn't look like a sinc function because you are effectively plotting the "abs(Y)" (no negative amplitudes)... try plotting "real(Y)". It doesn't look exactly the same (but close) as the example here:
http://en.wikipedia.org/wiki/Fourier_transform
Scroll down about 1/5 from the top is an example of a Top-hat function and its' Fourier Transform. Also stick an "xlim([-6 6])" below where you plot the frequency domain stuff.

Sign in to comment.


Daniel Shub
Daniel Shub on 9 May 2012
  1 Comment
Esteban Zegpi
Esteban Zegpi on 9 May 2012
Thank you, I'll check this out!

Sign in to comment.

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!