Area Under the Graph
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (2)
Star Strider
el 23 de Nov. de 2018
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 comentarios
Star Strider
el 16 de En. de 2019
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.
Ver también
Categorías
Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

