# Help Understanding FFT Plot?

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 :

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?

• 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 Jul 4, 2022 at 17:13

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.

• How can I get energy of the signal against frequencies as it can be viewed in spectrum analyzer or FFT signal on oscilloscope? Jul 4, 2022 at 10:59
• 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. Jul 5, 2022 at 2:27

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.

• How can I get energy of the signal against frequencies as it can be viewed in spectrum analyzer or FFT signal on oscilloscope? Jul 4, 2022 at 11:06
• 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?" Jul 4, 2022 at 11:28
• 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. Jul 4, 2022 at 11:48
• 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. Jul 5, 2022 at 12:11