Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[Urgent] Matlab DFT Strange result

Status
Not open for further replies.

AdvaRes

Advanced Member level 4
Advanced Member level 4
Joined
Feb 14, 2008
Messages
1,163
Helped
113
Reputation
220
Reaction score
51
Trophy points
1,328
Location
At home
Activity points
7,442
Hi members,

In order to recongnize the differentes harmonics of a function I wrote a matlab script to calculate the DFT. In order to verfy that may script is correct I applyed this function to a simple sinus function.

Here is the code
Fe=8E9 %Sampling frequency
N=8 %8 samples
df=Fe/N %step

t=(1:N)/Fe %the range
f0=1E9 %Frequency
x=2*sin(2*pi*f0*t)+2

y=fft(x)
y2=fftshift(y)
m=abs(y2)

Freq=(0:df:Fe-df)

figure(1),plot(t,x) %plot sinwave
figure(2),plot(y,m)%plot DFT

I was expecting something like shown in the following. Unfortunately matlab gives strange plots.
Is there something incorrect in the script ?
 

Why in your 2nd plot statement are you plotting the magnitude vs the output of the fft? The magnitude should be plotted against frequency.
 

    AdvaRes

    Points: 2
    Helpful Answer Positive Rating
RBB said:
Why in your 2nd plot statement are you plotting the magnitude vs the output of the fft? The magnitude should be plotted against frequency.

Hi,
In the second plot I presented the DFT that I'm expecting to obtain. Isn't it DFT by definition? The plot doesn't show represent the the magnitude vs the output of the fft but It shows the magnitutes of the harmonics vs frequency.
In the exemple the sin function has two harmonics the firts is a sin wave of 1GHz and the second is a constant.

Please look at this

**broken link removed**
 

I think you want to do something like:
plot(linspace(-pi, pi, length(m)), m);
This will plot the magnitude vs radians from -pi to pi.
 

Ok, this the corrected version of your program:

Fe=8E9; %Sampling frequency
N=8; %8 samples
df=Fe/N; %step

t=(0:N-1)/Fe; %the range
f0=1E9; %Frequency
x=2*sin(2*pi*f0*t)+2;

y=fft(x);
y2=fftshift(y);
m=abs(y);

Freq=(0:df:Fe-df);

figure(1),plot(t,x); %plot sinwave
figure(2),plot(Freq, m);%plot DFT

And the plots that are generated are:

sine


fft


----------------

Now, if you increase the number of points to N= 16, these are the results:

sine


fft


----------------

If you increase the number of points to N= 64, these are the results:

sine


fft


----------------

Now, let's comment:

1. In your program, the second plot command is modified and it is plotted m vs. Freq

2. The number of N=8 points gives the frequency resolution of Fs/N=1GHz. This is too large since in reality you have two components in spectrum separated by 1GHz range, and FFT can not separate them (as you can see on the first FFT plot, you don't see two peaks in the 0-4GHz range, just one)

3. If you increase the number of points to N=16, it gives the frequency resolution of Fs/N=0,5GHz, and now the two components are separated in FFT, but still touching each other.

4. If you increase the number of points to N=64, it gives the frequency resolution of Fs/N=0,125GHz, and now the two components are completely separated in FFT (and this picture looks much like the one that you posted).

Conclusions:

1. If the number of points N in FFT is increased, you get the finer resolution and better separation of close components.

2. The larger the number of points N is, the more computational operations is needed for calculating FFT.

---

So, the number of point for FFT really depends on the application, and is the result of compromice between Conclusion_1 and Conclusion_2.

Hope that this clarifies a little.

Regards
 

    AdvaRes

    Points: 2
    Helpful Answer Positive Rating
Thank you very much zorx for you detailed reply.

My function is a sin wave with 1GHz frequency plus a constant part K. So if look at it in the frequency domain, the DFT plot will be two segments one at 0 Hz with an amplitude K and the second at 1 GHz with the an amplitude equal to the amplitude of the sin wave.

There are two things that I couln't understand however.


The firt Is that, looking at y_64.jpg, We see that we have three segment, one at 0Hz the second at 1GHz and the Third at 7 GHz. From where the third harmonic come from ?
The second is the amplitude itself. At 1 GHz I expect it to be equal to 2, but in the figure it is around 65.

Is there another thing wrong in the matlab script ?
 

If you change the line:
m=abs(y);
to
m=abs(y2);

then the fft plot will go from -Fs/2 to Fs/2. Thus you'll see the DC component in the middle of the plot, and you wanted signal on either side of the DC signal.
 

    AdvaRes

    Points: 2
    Helpful Answer Positive Rating
OK.
What about the amplitude ? What is it's unit in the figure y_64.jpg ?
 

You'll want to:
y=fft(x);
y = y / length(y);
to normalize the amplitude.

Also I'd recommend increasing N to something like 1024. It'll make things easier to see.
 


Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top