views:

467

answers:

2

Currently I did a FFT of a set of data which gives me a plot with frequency at x axis and amplitude at y axis. I would like to calculate the area under the graph to give me the energy.

I am not sure how to determinate the area because I am without the equation and also I only want a certain area of the plot rather than whole area under the plot. Is there a way I can do it?

+6  A: 

There are many ways to do numerical integration with Matlab. Here is an example:

%# create some data
x = linspace(0,pi/2,100); %# 100 equally spaced points between 0 and pi/2
y = sin(x);

%# integrate using trapz, which calculates the area in the trapezoid defined by 
%# x(k),x(k+1),y(k),y(k+1) for k=1:length(x)
integral = trapz(x,y);

%# if you only want to integrate part of the data, do
partialIntegral = trapz(x(10:20),y(10:20));

%# show the integrated area
figure, 
area(x,y); 
hold on, 
area(x(10:20),y(10:20),'FaceColor','red')
Jonas
Hiya, thanks alot, but u have try ti intergrate part of the data using your suggestion. But Matlab keep telling me the this:LENGTH(X) must equal the length of the first non-singleton dimension of Y.I think trapz only accept equally length x and y. Is there a way round? Thank you
lytheone
Integration requires that every x-coordinate is matched with a y-coordinate. This is why I write `trapz(x(10:20),y(10:20))`, i.e. I take the same amount of points for x as for y. However, the x-points do not need to be equally spaced. If you want to eliminate some y-points, you should drop the corresponding x-points as well.
Jonas
+2  A: 

The FFT is discrete, not continuous - you just need to sum all the bin values. If you're looking at the power spectrum (magnitude squared) then the bin values are in W/Hz, so you would need to multiply each value (or alternatively just the sum), by the bin width in Hz to get power (and hence the total energy in your input sample).

Paul R
Actually, you need to sum the bin values and multiply them by the bin width if it's not 1. This is, approximately, what trapz does.
Jonas
@Jonas: good point, thank you - I'll update my answer.
Paul R

related questions