Area Under the Graph

조회 수: 5 (최근 30일)
Awais Yousaf
Awais Yousaf 2018년 11월 23일
댓글: Star Strider 2019년 1월 16일
Hi,
I have these multiple plots in a figure and I am trying to calculate the area under the curve (for different frequency ranges) for each of them for comparison pursposes.
For Instance,
I want to determine the area under the curve for 0-50Hz, 50-100Hz, 100-150Hz, and so on for all the flow rates. To get area under the curve, I thought of assigning the horizontal reference line at the lowest point (around -92dB) in this case, which would be constant for all the cases. I know I can use trapz function for this but I am confused with application of limits in my case. Any suggestions?
Best wishes,
Awais

답변 (2개)

Star Strider
Star Strider 2018년 11월 23일
I am not certain what you want.
I would instead use the cumtrapz function for the entire data set, then find the frequencies you want.
Example —
A = rand(4, 60); % Amplitude Array (Assumes Row Vectors)
F = linspace(0, 600, 60); % Frequency Vector
area_mtx = cumtrapz(F, A, 2); % Cumulative Integral
frqs = 0 : 50 : 600; % Frequency Bands
for k1 = 1:numel(frqs)-1
idxrng = [find(F >= frqs(k1),1,'first') find(F < frqs(k1+1),1,'last')]; % Index Range
freq_band(:,k1) = diff(area_mtx(:,idxrng),[],2); % Frequency Segment Integral
end
It would be best to subtract any constant offset area from the ‘area_mtx’ array first, before the loop.
Experiment to get the result you want.
  댓글 수: 7
Awais Yousaf
Awais Yousaf 2019년 1월 16일
편집: Awais Yousaf 2019년 1월 16일
The "freq_band" output that is generated by the loop for each flow rate (e.g. 50mlmin.txt file), is there a way to run a loop in a way that I can save that particular output for different flow rates within one place/matrix to plot later?
Example attached.
Star Strider
Star Strider 2019년 1월 16일
Try this (complete code, with the plots, with a loop to read and analyze each file):
filenames = {'50mlmin.txt'; '100mlmin.txt'};
for k2 = 1:numel(filenames)
D = dlmread(filenames{k2}, '\t', 1, 0);
A = D(:,2);
F = D(:,1);
% A = rand(4, 60); % Amplitude Array (Assumes Row Vectors)
% F = linspace(0, 600, 60); % Frequency Vector
area_mtx = cumtrapz(F, A, 1); % Cumulative Integral
frqs = 0 : 50 : 600; % Frequency Bands
for k1 = 1:numel(frqs)-1
idxrng = [find(F >= frqs(k1),1,'first') find(F < frqs(k1+1),1,'last')]; % Index Range
freq_band(k1,:) = diff(area_mtx(idxrng),[],1); % Frequency Segment Integral
end
freq_band_save{k2,:} = {filenames{k2} freq_band};
refl = -50; % Reference Line For Integration
figure
plot(F, A)
hold on
plot([frqs; frqs], ones(2, numel(frqs)).*ylim', '--k')
plot(xlim, [1 1]*refl, '-r')
hold off
grid
xlim([0 600])
cstr = compose('\\bf%6.1f', freq_band);
% cstr = sprintfc('\\bf%6.1f', freq_band); % Use This If You Do Not Have The ‘compose’ Function
text(frqs(1:end-1)+25, -98*ones(1,numel(frqs)-1), cstr, 'HorizontalAlignment','left', 'VerticalAlignment','middle', 'Rotation', 90)
end
The ‘freq_band_save’ cell array has the frequency band data for each file. The first column in each row is the file name, and the second is the ‘frequency_band’ vector for that file.
You can do the same for any of the other variables my code calculates. If you want to avoid recalculating them, consider using the save (link) function to store whatever variables you want in a .mat file. You can then create a table such as the one you attached.
It has been a while since I originally wrote this. I had to refresh my memory.

댓글을 달려면 로그인하십시오.


Awais Yousaf
Awais Yousaf 2018년 12월 6일
편집: Awais Yousaf 2018년 12월 6일
I tried to run the code for my case, it did not run.
I get "Index exceeds matrix dimensions" error. Before this, I tried it with random vectors and I am not sure about what the loop generates, and what I should be looking at from the loop output.
I have attached the text files for two of the series that I am plotting (figure attached).
I am trying to get the areas (A, B, C, D, E, F, ....) under each of the these series plots for 50Hz intervals, to a common baseline (let's say -100db).

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by