From: Andrew on
Hello,

I am trying to work through the Fast Fourier Transform tutorial at http://www.ele.uri.edu/~hansenj/projects/ele436/fft.pdf which I found using Google.

The code that I am trying to run is the following (also given on page 3 of the above tutorial). I have tried my best to reproduce it accurately from the code given in the tutorial.
---
n = [0:29];
x = cos(2*pi*n/10);

% Define 3 different values for N
N1 = 64;
N2 = 128;
N3 = 256;

% Find the FFT of X1, X2, and X3
X1 = abs(fft(x,N1));
X2 = abs(fft(x,N2));
X3 = abs(fft(x,N3));

% Normalize the scale
F1 = [0 : N1-1]/N1;
F2 = [0 : N2-1]/N2;
F3 = [0 : N3-1]/N3;

% Plot the transforms
subplot(3,1,1)
plot(F1,X1,'-x',title('N = 64'),axis([0 1 0 20]))
subplot(3,1,2)
plot(F2,X1,'-x',title('N = 128'),axis([0 1 0 20]))
subplot(3,1,3)
plot(F3,X3,'-x',title('N = 256'),axis([0 1 0 20]))
---
Unfortunately, Matlab gives me error messages:
??? Error using ==> axis at 190
Too many output arguments.
Error in ==> fft_tutorial_august5 at 21
plot(F1,X1,'-x',title('N = 64'),axis([0 1 0 20]))
---
When I look at the documentation for plot, it appears that the x data (horizontal axis) should be given first and the y data (vertical axis) should be given second. Do you know why the tutorial is suggesting plot(F1,X1,...) rather than plot(X1,F1,...)? Also, do you know what the '-x' in the plot command is for? I can't seem to find '-x' in the Matlab documentation; I'm sure that it is there, I am just not, unfortunately, seeing it.

Many thanks in advance for any advice you can offer. Thank you kindly!

Andrew
Carnegie Mellon University
From: Wayne King on
"Andrew " <andrewdaviddeyoung(a)gmail.com> wrote in message <i3en2c$e31$1(a)fred.mathworks.com>...
> Hello,
>
> I am trying to work through the Fast Fourier Transform tutorial at http://www.ele.uri.edu/~hansenj/projects/ele436/fft.pdf which I found using Google.
>
> The code that I am trying to run is the following (also given on page 3 of the above tutorial). I have tried my best to reproduce it accurately from the code given in the tutorial.
> ---
> n = [0:29];
> x = cos(2*pi*n/10);
>
> % Define 3 different values for N
> N1 = 64;
> N2 = 128;
> N3 = 256;
>
> % Find the FFT of X1, X2, and X3
> X1 = abs(fft(x,N1));
> X2 = abs(fft(x,N2));
> X3 = abs(fft(x,N3));
>
> % Normalize the scale
> F1 = [0 : N1-1]/N1;
> F2 = [0 : N2-1]/N2;
> F3 = [0 : N3-1]/N3;
>
> % Plot the transforms
> subplot(3,1,1)
> plot(F1,X1,'-x',title('N = 64'),axis([0 1 0 20]))
> subplot(3,1,2)
> plot(F2,X1,'-x',title('N = 128'),axis([0 1 0 20]))
> subplot(3,1,3)
> plot(F3,X3,'-x',title('N = 256'),axis([0 1 0 20]))
> ---
> Unfortunately, Matlab gives me error messages:
> ??? Error using ==> axis at 190
> Too many output arguments.
> Error in ==> fft_tutorial_august5 at 21
> plot(F1,X1,'-x',title('N = 64'),axis([0 1 0 20]))
> ---
> When I look at the documentation for plot, it appears that the x data (horizontal axis) should be given first and the y data (vertical axis) should be given second. Do you know why the tutorial is suggesting plot(F1,X1,...) rather than plot(X1,F1,...)? Also, do you know what the '-x' in the plot command is for? I can't seem to find '-x' in the Matlab documentation; I'm sure that it is there, I am just not, unfortunately, seeing it.
>
> Many thanks in advance for any advice you can offer. Thank you kindly!
>
> Andrew
> Carnegie Mellon University

Hi Andrew, You are making a syntax error here:

plot(F1,X1,'-x',title('N = 64'),axis([0 1 0 20]))

without looking at the tutorial, there should be right parenthesis and a comma, or a semicolon to use plot()

plot(F1,X1,'-x'),

Also, you should not be closing with a parenthesis after axis([0 1 0 20])
The way you written the code, you have plot() taking title() and axis() as inputs.

Look carefully at your tutorial again to make sure that you have not made a typo.
Wayne
From: Andrew on
Thanks! That helps, I had the parentheses in the incorrect places.

But, it still seems strange that "plot(F1,X1,..." appears to plot X1 on the horizontal axis and F1 on the vertical axis. It seems from the Matlab documentation that to plot X1 on the horizontal axis and F1 on the vertical axis, one should actually use "plot(X1,F1,...".

In the Matlab documentation, it says that "plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. " Does this mean that X1 will be on the horizontal axis (as is the typical math convention in the United States), or will X1 actually be on the vertical axis?

Thank you!
From: Wayne King on
"Andrew " <andrewdaviddeyoung(a)gmail.com> wrote in message <i3eq9g$d3k$1(a)fred.mathworks.com>...
> Thanks! That helps, I had the parentheses in the incorrect places.
>
> But, it still seems strange that "plot(F1,X1,..." appears to plot X1 on the horizontal axis and F1 on the vertical axis. It seems from the Matlab documentation that to plot X1 on the horizontal axis and F1 on the vertical axis, one should actually use "plot(X1,F1,...".
>
> In the Matlab documentation, it says that "plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. " Does this mean that X1 will be on the horizontal axis (as is the typical math convention in the United States), or will X1 actually be on the vertical axis?
>
> Thank you!

Hi Andrew, F1 is the "x" variable and X1 is the "y" variable. There's nothing set in stone about what you call the variables. You are correct that the first input is plotted as the "x" variable and the second variable as the "y" variable. It just so happens that in this tutorial, F1 is the "x". Presumably because you are learning about Fourier analysis and the x-variable is frequency, so the author has used F.

Wayne