writing a code for DFT without using built-in functions

10 views (last 30 days)
Hello, I need to write a discrete fourier transform code without using any built-in functions.The code I am trying to get to work is:
f=1/2; //frequency
N=13; // total samples
n=0:1:N-1; //summation going from n=0 to n=12
x=cos(2*pi*f*n)*exp((-j*2*pi*n*k)/N); //I usually put k=1.
Xz= [(X) zeros(1,10)]; // padding with zeros
plot(abs(Xz)); //plotting the magnitude
the range is from 0 to 2*pi but I don't know how to code that as well? This is my first time using matlab so any help would be great.
The error I get is error using * Inner matrix dimensions must agree.

Answers (1)

dpb
dpb on 28 Jul 2013
doc times
Since you say this is your first experience w/ Matlab, I suggest starting at the beginning of the online doc's are read at least the sections through the "Language Fundamentals" topic
  2 Comments
steven
steven on 28 Jul 2013
Thank you for the help. I have been reading through all the topics just trying to understand how matlab works.
Another question I have is how do I set the range from 0 to 2pi on the x-axis? when I use
y= linspace(0,2*pi);
plot((abs(Xz),y);
I get the error vectors must be the same length. Thanks again for any help.
dpb
dpb on 28 Jul 2013
Why do you name 0:2*pi as 'y' instead of x? Not that it really changes anything, but still, it's conventional, surely.
doc plot
Input syntax is given as
plot(X,Y,'linespec')
If you want the x-axis to be 0:2pi then plot the response versus that vector instead of vice-versa. And, of course, the length(Y) and length(X) must be the same.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!