# How to calculate energy of the signal at a specific frequency after applying FFT

36 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Star Strider
on 18 Nov 2021

The energy is the amplitude (so with the fft function, since it produces peaks of half amplitude in the negative and positive frequencies after using fftshift with the two-sided fft), multiply the one-sided fft results by 2 to get the actual amplitude at that frequency, and then square that result to get the power, if desired.

.

##### 4 Comments

Star Strider
on 19 Nov 2021

My pleasure!

If my Answer helped you solve your problem, please Accept it!

.

### More Answers (1)

David Goodmanson
on 19 Nov 2021

Edited: David Goodmanson
on 30 Dec 2021

Hello Daniel,

To get the desired result you have to be careful about the number of points, so that an exact number of oscillations occurs in the time window. For y = sin(const*t), an exact number of oscillations means, for example, starting with sine = 0 and ending one point short of sine = 0, because that last sine = 0 belongs at the start of the next repetition of the signal. Anything other than an exact number of oscillations will cause the fft to spill over into adjacent frequencies. That emphatically applies to use of nextpow2 which truncates the signal and almost always results in a nonintegral number of oscillations.

Such spillover is NOT due to floating point error.

So:

n = 1000;

delt = .01;

t = (0:n-1)*delt;

s = sin(2*pi*4*t);

figure(1)

plot(t,s)

grid on

delf = 1/(n*delt);

f = (-n/2:n/2-1)*delf;

y = fftshift(fft(s)/n);

figure(2)

plot(f,abs(y))

grid on

This yeilds two peaks with amplitude 1/2 as should be the case, and no frequency content anywhere else.

For the power, you can square the absolute value of each peak and take the sum

(1/2)^2 + (1/2)^2 = 1/2.

For complex signals, squaring the heights separately has to be done since in general the two peaks do not have the same height. For real signals the abs values of the two peaks are the same, so you can double the positive frequency amplitude to account for negative frequencies. In this example the resulting ampliude is 1, which is the amplitude of the sine wave.

But simply squaring that amplitude to get the power is NOT correct.

You have to toss in a factor of 1/2 because the average value of sin^2 = 1/2.

1^2 * (1/2) = 1/2

All of this applies to a complcated signal if you are looking at just one frequency.

##### 0 Comments

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!