Find Area under the curve as a non negative

32 views (last 30 days)
The vector I am plotting is partially negative because the y axis is a zscore of my fiber photometry signal. I am trying to calculate the area under the curve, but it is returning negative numbers because my y-axis is partially 0. Using the absolue value inflates my actual area under the curve (my signal is a waveform, so making everything positive makes waht was outside of the ewaveform inside of the waveform when flipped bove x). I tried fliplr, but the resulting graph looks the exact same. How do I just get an absolute area under the curve without inflating the AUC?
Current code: raw_dff is a matrix where each column is a waveform from a previoujs pek detection I performed. I get the area under the curve for each column in that matrix with my AUC_counter, but againn, this is sometimes negative.
auc = cumtrapz(raw_dff);
AUC_counter = auc(81,:)';
  5 Comments
Torsten
Torsten on 1 Mar 2024
Edited: Torsten on 1 Mar 2024
for i = 1:size(raw_dff,2)
vector = raw_dff(:,i);
m = min(vector);
if m < 0
vector = vector - m;
end
auc(:,i) = cumtrapz(vector);
end

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 1 Mar 2024
It would help to have the data.
Perhaps selecting everything with both x and y coordinates greater than zero, and then integrating that result will do what you want.
Try this —
x = linspace(-1, 2, 500);
y = sin(2*pi*x);
Lv = x>0 & y>0;
inty = cumtrapz(x(Lv), y(Lv));
figure
plot(x, y, 'DisplayName','Original Data')
hold on
plot(x(Lv), y(Lv), '-r', 'DisplayName','Selected Data')
plot(x(Lv), inty, '-g', 'DisplayName','Integral')
hold off
legend('Location','best')
.
  2 Comments
Caroline Szujewski
Caroline Szujewski on 1 Mar 2024
hello, I left another comment above that I think will clarify what I want. Please let me know if it is still not clear.
Star Strider
Star Strider on 1 Mar 2024
Edited: Star Strider on 1 Mar 2024
Perhaps this —
y = readmatrix('vector_ex_data.xlsx');
L = numel(y);
x = linspace(0, L-1, L); % Corresponds To Indices Into 'y', Use A Different Vector As Desired (Used In 'cumtrapz' Call)
miny = min(y)
miny = -0.7757
ys = y-miny; % 'ys' —> y-shifted
figure
plot(x, y)
grid
xlabel('X')
ylabel('Y')
title('Original')
AUC = cumtrapz(x, ys);
figure
yyaxis left
plot(x, ys, 'DisplayName','y-shifted')
hold on
patch([x flip(x)], [zeros(size(ys)) flip(ys)], 'r', 'DisplayName','AUC')
hold off
text(45, 1.05, sprintf('Total Area = %.4f', AUC(end)))
Ax1 = gca;
ylabel('Y')
yyaxis right
plot(x, AUC, '-g', 'DisplayName','Cumulative Area', 'LineWidth',2)
Ax2 = gca;
grid
xlabel('X')
ylabel('Y')
title('Y-Shifted')
legend('Location','best')
EDIT — Corrected typographical errors.
.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 1 Mar 2024
You don't show your data, so I need to make something up, and I'm guessing what your problem is, because your question is confusing. But I think this is your question:
What is the area between my curve and the x axis? So negative regions of the curve, they will still have positive area. For example:
x = linspace(0,2*pi,8);
y = sin(x);
plot(x,y,'-o')
yline(0)
If we use trapz to integrate this curve, we will get zero.
trapz(x,y)
ans = 0
The positive and negative parts compensate exactly. So I think you want to find the are between the curve and the x axis. BUT, if you do this:
trapz(x,abs(y))
ans = 3.9326
it mistakes the area, because of the region near x==pi.
plot(x,abs(y),'o-')
The problem is, that part of the curve wants to hit zero between the sampled points, so right at x==pi.
Instead, you need to interpolate the data, then use integral on the absolute value of the interpolation function. For example...
spl = spline(x,y);
fplot(@(x) abs(fnval(spl,x)),[0,2*pi])
You should see it now does touch down to zero at x==pi. And integral gives a better estimate of the true area.
integral(@(x) abs(fnval(spl,x)),0,2*pi)
ans = 4.0094
Note that trapz will under-estimate that area because it does so on an always concave-down function, and it over-estimates the area because of the problem near x==pi.
  3 Comments
John D'Errico
John D'Errico on 1 Mar 2024
If you just shift the entire curve upwards, it does not do what you want. It just adds a linear bias to the cumulative area.
I explained how to do, what I still think you are asking.
Are you looking for the cumulative area between the curve and the x-axis, or the total area?
Caroline Szujewski
Caroline Szujewski on 1 Mar 2024
I ended up doing that, thnak you for your help!

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!