0
$\begingroup$

I want to analyze FFT and Harmonics of my signal.
As a proof of concept I tested FFT against standard signal.
I am unable to understand why the frequency output is going to -1. Shouldn't it be +1 at +pi and -pi.

Code :

x = -pi:0.01:pi;
sin_x = sin(x);
fft_x = fft(sin_x);

subplot(2,1,1);
plot(x, sin_x),
grid on;

subplot(2,1,2);
plot(x, fft_x),
grid on;

Plot :

enter image description here


Updated Question

How can I get energy of the signal against frequencies as it can be viewed in spectrum analyzer or FFT signal on oscilloscope?

$\endgroup$
1
  • 1
    $\begingroup$ The DFT (Discrete Fourier Transform) is mathematically challenging. I recommend starting with a good texts or grad level class. Here is an example: ccrma.stanford.edu/~jos/mdft $\endgroup$
    – Hilmar
    Jul 4, 2022 at 17:13

2 Answers 2

3
$\begingroup$

Because $\sin(x) = -\sin(x+\pi)$.

The definition of DFT is

$$ X(k) = \sum_{n=0}^{N-1} x(n) e^{-j2\pi kn/N} $$

You may notice that $n$ goes from $0$ to $N-1$. Although the time index of your signal is from $-\pi$ to $\pi$, DFT has no idea of it and assumes it starts from $0$. That means the signal send into fft is actually -sin([0:0.01:2*pi]) resulting in a negative frequency output.

$\endgroup$
2
  • $\begingroup$ How can I get energy of the signal against frequencies as it can be viewed in spectrum analyzer or FFT signal on oscilloscope? $\endgroup$ Jul 4, 2022 at 10:59
  • 1
    $\begingroup$ To get energy against frequency, just calculate $|X(k)|^2=X(k)X^*(k)$, or you can use pwelch to get a power spectral density (PSD) estimate. $\endgroup$
    – ZR Han
    Jul 5, 2022 at 2:27
2
$\begingroup$

Your command plot(x, fft_x) plots only the real part of the complex-valued fft_x sequence. Try the following MATLAB code to see if it makes more sense to you:

figure(1)
subplot(3,1,1), plot(real(fft_x)), grid on
subplot(3,1,2), plot(imag(fft_x)), grid on
subplot(3,1,3), plot(abs(fft_x)),  grid on

Your example has a subtle anomaly. The real part of the FFT of a single cycle of a sine wave should be all zeros. That's not your case because your sin_x sequence is not exactly a single cycle of a sine wave. For your sin_x sequence to be exactly a single cycle of a sine wave its last sample must be equal to the negative of the second sample of sin_x.

$\endgroup$
4
  • $\begingroup$ How can I get energy of the signal against frequencies as it can be viewed in spectrum analyzer or FFT signal on oscilloscope? $\endgroup$ Jul 4, 2022 at 11:06
  • $\begingroup$ Do your words "How can I get energy of the signal against frequencies" mean "How can I plot the spectrum of your time-domain signal?" $\endgroup$ Jul 4, 2022 at 11:28
  • $\begingroup$ I have been asked to plot all frequencies present in the signal and latter also analyze the Total harmonic Distortion. So one can say plot the spectrum of your time-domain signal and/or energy of each frequency component in the signal. $\endgroup$ Jul 4, 2022 at 11:48
  • 1
    $\begingroup$ Well, ...compute the DFT of your time signal, compute the magnitude of the complex-valued DFT samples, and plot the first half (positive-frequencies) of the magnitude samples over a frequency range of zero -to- Fs/2 Hz. Next, measure magnitude of the "fundamental" spectral component and the magnitude of all its visible spectral harmonics, and apply those measured magnitude values to some "total harmonic distortion" equation. $\endgroup$ Jul 5, 2022 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.