Solving DFT of Cos Function Issue with MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter amaresh92
  • Start date Start date
  • Tags Tags
    Cos Dft Function
Click For Summary

Discussion Overview

The discussion revolves around an issue with performing a discrete Fourier transform (DFT) of a cosine function using MATLAB. Participants explore the implications of the Nyquist sampling theorem in relation to the sampling frequency used in the provided code.

Discussion Character

  • Technical explanation, Debate/contested, Mathematical reasoning

Main Points Raised

  • One participant shares MATLAB code intended to compute the FFT of a cosine function at 100 Hz but reports unexpected results.
  • Another participant questions the original poster's understanding of the Nyquist sampling theorem, suggesting it is critical to the problem.
  • A participant points out that the sampling frequency of 4 Hz is insufficient for accurately capturing the 100 Hz signal, referencing the Nyquist theorem which states that sampling must occur at least twice the highest frequency component.
  • A different participant offers an alternative MATLAB code example with a higher sampling frequency of 500 Hz, suggesting it may yield more accurate results.
  • One participant reiterates the importance of sampling frequency and its relation to the Nyquist theorem, emphasizing the need for a smaller time spacing to avoid aliasing.

Areas of Agreement / Disagreement

Participants generally agree on the importance of the Nyquist sampling theorem and the need for an adequate sampling frequency to accurately analyze the signal. However, there is no consensus on the effectiveness of the original code provided by the poster.

Contextual Notes

The discussion highlights limitations related to the sampling frequency and its impact on the results of the FFT, but does not resolve the underlying issues in the original code.

amaresh92
Messages
163
Reaction score
0
greetings,
i have typed some code in MATLAB to find the 5 point fft of cos function of frequency 100hz but in answer i am not geting the answer at 100 hz.may i know where the things going wrong. the code is as this one

clc;
t=0:1/4:1;
x=cos(2*pi*100*t);
xm=abs(fft(x));
disp(xm)
p=0:length(xm)-1;
subplot(2,2,1);
stem(100*p,xm);



the answer is
5 0 0 0 0


thanks in advance
 
Physics news on Phys.org
Hi amaresh92. Tell me what you know about the Nyquist sampling theorem.
 
uart said:
Hi amaresh92. Tell me what you know about the Nyquist sampling theorem.
dont know why it has to be true
 
Ok. The problem is that your single is at 100Hz, but you are only sampling at at 4 Hz (4 samples per second). Sampling theory says that we have to sample at at-least twice the highest frequency component of the signal, so greater than 200 Hz in this case. What this means is that your "t" spacing must be less than 0.005.
 
Last edited:
Hi amaresh92, if you want a simple 5 point example then try this code and see if the results make more sense. :smile:

Code:
n = 5                    # Specify the number of points to sample.
fs = 500                 # Specify the sample frequency.
dt=1/fs                  # Calculate the inter-sample interval.

t = [0:n-1]*dt           # Create the sample vector.
x = cos(2*pi*100*t)      # Create the signal vector.
xm = abs(fft(x))         # Compute the fft magnitude.

df = fs/n                # Calculate the frequency increment of the fft.
ascale = n/2             # Amplitude scale factor.

                         # Plot fft magnetude. Note "reflection" about fs/2.
plot([0:n-1]*df,xm/ascale,'*')
 
Last edited:
uart said:
Ok. The problem is that your single is at 100Hz, but you are only sampling at at 4 Hz (4 samples per second). Sampling theory says that we have to sample at at-least twice the highest frequency component of the signal, so greater than 200 Hz in this case. What this means is that your "t" spacing must be less than 0.005.

thanks a lot
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K