Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fourier transfom using matlab help

  1. Aug 1, 2005 #1
    Fourier transfom using matlab...help
    where can i get files .m about this?
  2. jcsd
  3. Aug 1, 2005 #2
    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)')

    this should produce a plot with two overlapping curves. hope this helps
    Last edited: Aug 1, 2005
  4. Aug 1, 2005 #3


    User Avatar
    Science Advisor

    I believe that fft and ifft are inbuilt commands and are not implemented as "m files".
  5. Oct 2, 2008 #4

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook