Calculate area under FFT graph in MATLAB

前端 未结 2 1315
终归单人心
终归单人心 2021-01-05 22:22

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

2条回答
  •  暖寄归人
    2021-01-05 23:09

    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')
    

提交回复
热议问题