# MATLAB Fourier transfom using matlab help

#### gustavo_a1986

Fourier transfom using matlab...help
where can i get files .m about this?

Related MATLAB, Maple, Mathematica, LaTeX, Etc News on Phys.org

#### abercrombiems02

The command you need to use is fft(V,n)

Let's look at example...

Let's suppose we want to plot the function f(t) = 3 + cos(2t) - 4sin(6t)
using the discrete fourier transform

first we need to define a vector for the time interval

t = linspace(0,2*pi,4096); should suffice

(that is we will create a vector of 4096 evenly spaced number between 0 and 2*pi)

now we will write your function

alrighty, in our vector, V, that is to be used in the fft command

the first element corresponds to any constants (or items of zero order frequency)

thus the first element of V will be 3.

The next frequency, 1, does not show up, so the second element of V is zero

The next frequency, 2, appears in a cosine and the amplitude of this frequency is 1 (the leading coefficient in front of cos(2t)).

So our vector V is now [3 0 1].

But how do we differentiate between a cosine and a sine? Well the cosine is the real part of the decomposition of exp(i*theta). So what we really need to take is the real part of this vector. So the first part of this function can be written as

real(fft([3 0 1],4096)) (need same # of elements both t and the fft)

We also need to add in the sine term

sin corresponds to the imaginary parts of the decomposition of exp(i*theta)
The only sine term has a frequency of 6 with amplitude -4. Thus,

V = [0 0 0 0 0 0 4];

Because exp(i*theta) = cos(theta*t) - i*sin(theta*t)

we must actually take the opposite of any amplitudes with imaginary functions associated with them. Thus if we had the function

h(t) = i + 3sin(2t) - 5sin(3t) our vector V must be [-1 0 -3 5];
make sense?

remember, the first element corresponds to constant
the second will correspond to frequency of order 1
thus the seventh element will correspond to frequencies of order 6.

to incorporate the sine term its fft is given by

imag(fft([0 0 0 0 0 0 4],4096))

so the entire code could be written as

t = linspace(0,2*pi,4096);

V1 = [3 0 1];
V2 = [0 0 0 0 0 0 4];

ft = real(fft(V1,4096)) + imag(fft(V2,4096));
gt = 3 + cos(2*t) - 4*sin(6*t);

if we wanted to plot to make sure....

plot(t,ft,t,gt,'g-'); grid on
title('Plot of 3 + cos(2t) - 4sin(6t)')
ylabel('f(t)')
xlabel('t')

this should produce a plot with two overlapping curves. hope this helps

Last edited:

#### uart

Science Advisor
gustavo_a1986 said:
Fourier transfom using matlab...help
where can i get files .m about this?
I believe that fft and ifft are inbuilt commands and are not implemented as "m files".

#### crazylove

Help

How to compute and plot sin(2*pi*t/P) and cos(2*pi*t/P) using [re,im}=plot_pair(t,P) where t is a vector of time samples to be computed and plotted and P is the period of the sin/cosine pair to compute.

Return: re-vector of same length as t contains sin(2*pi*t/P) values
im-vector of same length as t containing cos(2*pi*t/P) values

this makes use of the complex numbers somehow where.

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving