Area under curve for dynamic points on x-axis

2 visualizzazioni (ultimi 30 giorni)
Hi, I am trying to find the area under the curve from the start of the volume increase to one second after the start (example below with red, sorry its crude!). This is sensor data, so the points will be constantly changing, i.e. sometimes volume will start increasing at 5 seconds, other tests it will be 1 second. All the examples I have seen in the question are static points, whereas this will not be. Code below. Thank you.
ii = 0;
%q = zeros(1,1);
%t = zeros(1,1);
tic
while toc < 10
ii = ii + 1;
v = readVoltage(a,'A0'); % Read voltage from sensor
if v < 0.22, v = 0; % This is the 0 pressure outpur of sensor
end
v = v - 0.21; % Subtracting 0 pressure output from voltage output
p = (v/5)/0.09 - 0.04; % Converting to Kpa as per data sheet
if p < 0, p = 0; %Zeroing pressure if there is none and not letting it be a negative
end
p = p * 1000; % Converting Kpa to pa
a1 = 0.000415476; % Cross sectional Area of the first part of the Tube
a2 = 0.000028274; % Cross sectional Area of the second part of the Tube
r = 1.225; %Density of air
c = a1/a2; % Ratio of Areas
vf = a1 * sqrt( (2/r) * p/((c^2 -1) )); %Calculating Volumetric flow rate in M^3/s
q(ii) = vf * 1000; %converting to Liters/s
t(ii) = toc;
end
q = q(1:ii);
t = t(1:ii);
Ar = trapz(t,q); %Calculating area under curve = total volume exhaled
figure %Plotting curve and graph
plot(t,q,'-o')
xlabel('Elapsed time(sec)')
ylabel('Volume Flow Rate (L/s)')
set(gca,'xlim',[t(1) t(ii)])
hold on
ha1 = area(t(t>0), q(t>0), 'FaceColor', 'g' )
Arstr = sprintf('Forced Volume = %0.5f Liters', Ar)
legend([ha1], Arstr)

Risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by