Calculating Discrete Fourier Transform Coefficient with Matlab

In summary, the function calculates the discrete Fourier transform coefficient for an arbitrary function x. It does this by firstocating vector space, then calculating the product a = x(m)*w at every step of the loop, and integrating the product. If you want to do it the traditional way then you want to calculate the product a = x(m)*w at every step of the loop and accumulate. You can do this by putting something like a(k) = a(k) + x(m)*w inside the innermost loop. Alternatively, you can compute the basis function w(k) = exp(...) once for each k and then integrate the product by using point wise multiplication (.*) and summing.
  • #1
drdizzard
18
0
I've been asked to write a function (.m file) in Matlab to calculate the discrete Fourier transform coefficient for an arbitrary function x. So far this is what I've done:
Code:
function a = mydft(x,N)
%MYDFT Calculates the discrete Fourier transform
%usage:
%[a]=mydft(x)
%x=[ x[0] x[1] ... x[N-1] ] - vector containing discrete time sequence
%a=[ a_0 a_1 ... a_N-1 ] - vector of discrete time Fourier Series
%coefficients, k=0,...,N-1
for k=0:N-1
    for m=0:N-1
    w=exp(-j*2*pi/N.*m.*k);
    end
    a=sum(x(m).*w);
end
end
when I use it on a signal given to me and then plot kw_hat vs. |a| I get a plot that looks nothing like what I do for the same signal by hand. I don't think I'm getting the correct DFT coefficients out of this function

This is the first time I've used Matlab so I'm not very familiar with it and would appreciate any advice.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I think you might have an index error. You should allocate vector space. So, for "a", you should write a(1), a(2), a(3), etc. Otherwise, your program might overwrite previous values that need to be there.
 
  • #3
The code seems to mix and match the traditional C/FORTRAN programming style (inner and outer loops with accumulation) with Matlab (vector products), so you may be confusing yourself there.If you want to do it the traditional way then you want to calculate the product a = x(m)*w at every step of the loop and accumulate. You can do this by putting something like a(k) = a(k) + x(m)*w inside the innermost loop.

You can also compute the basis function w(k) = exp(...) once for each k and then integrate the product by
using point wise multiplication (.*) and summing. This allows you to eliminate the innermost loop completely. Just set up a vector
Code:
m = 0:N-1;   % only need to define this once
then the loop can look like
Code:
for k = 0:N-1
    w = exp(-j*2*pi/N.*m*k);
    a(k+1) = sum(x.*w);  % watch the index: a(1) is the first array element
end
(note that you have to index on k as osilmag pointed out so that you retain the individual a's for each k)

Finally, you can eliminate both loops if you pass to exp() an NxN array which varies in m in one dimension and k in the other...
 
Last edited:

1. How do I calculate the discrete Fourier transform coefficient using Matlab?

To calculate the discrete Fourier transform coefficient using Matlab, you can use the built-in function fft. This function takes in a vector or matrix of data points and returns the discrete Fourier transform coefficients as another vector or matrix. You can also use the fft2 function for 2-dimensional data.

2. What is the difference between the discrete Fourier transform and the discrete cosine transform?

The discrete Fourier transform (DFT) and the discrete cosine transform (DCT) are both used for converting a signal from the time domain to the frequency domain. However, the DFT represents the signal using complex numbers, while the DCT represents it using only real numbers. Additionally, the DCT is often used for analyzing real-world signals that are symmetric or periodic, while the DFT can be used for any signal.

3. How do I interpret the results of a discrete Fourier transform in Matlab?

The results of a discrete Fourier transform in Matlab can be interpreted as the frequency components present in the original signal. The magnitude of each coefficient represents the amplitude of the corresponding frequency component, while the angle represents the phase. The first coefficient represents the DC component (or average value) of the signal, while the subsequent coefficients represent the different frequency components.

4. Can I calculate the inverse discrete Fourier transform using Matlab?

Yes, you can use the ifft function in Matlab to calculate the inverse discrete Fourier transform (IDFT). This function takes in the discrete Fourier transform coefficients and returns the original signal in the time domain. Similarly, you can use the ifft2 function for 2-dimensional data.

5. How can I visualize the results of a discrete Fourier transform in Matlab?

You can use the plot function in Matlab to plot the magnitude and phase of the discrete Fourier transform coefficients. You can also use the stem function to plot a discrete version of the coefficients. Additionally, you can use the fftshift function to shift the coefficients so that the DC component is in the center of the plot and visualize the frequency spectrum more clearly.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
5
Views
178
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Linear and Abstract Algebra
Replies
4
Views
782
  • Differential Equations
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Cosmology
Replies
2
Views
106
Back
Top