Averaging a curve with itself

3 vues (au cours des 30 derniers jours)
Alice Lam
Alice Lam le 8 Sep 2023
Déplacé(e) : Stephen23 le 11 Sep 2023
I'm trying to average this curve (pixel intensity of a selected object by distance from a point) with itself along the x-axis:
I've tried linear interpolation, which was unsuccessful because I do not have fully unique values in x. However, my x-values also do not always match perfectly between the upper and lower curves, which means that I cannot just average y-coordinate by matching x-values.
What would be the best way to go about doing this? I have attached my code and the relevant .mat file in case that helps.

Réponse acceptée

Star Strider
Star Strider le 8 Sep 2023
Try this —
% type('membrane_intensity.m')
LD = load('test.mat');
x = LD.cx;
y = LD.c(:,1,1);
[xs,x1] = min(x);
[xe,x2] = max(x);
Lv = ismember(x, x(x1:x2));
Sec{1,:} = [x(Lv) y(Lv)]
Sec = 1×1 cell array
{608×2 double}
Sec{2,:} = [x(~Lv) y(~Lv)]
Sec = 2×1 cell array
{608×2 double} {555×2 double}
Sec{2} = sortrows(Sec{2})
Sec = 2×1 cell array
{608×2 double} {555×2 double}
[Us1x,ia1,ic1] = unique(Sec{1}(:,1), 'stable');
Sec{1} = Sec{1}(ia1,:)
Sec = 2×1 cell array
{608×2 double} {555×2 double}
[Us2x,ia2,ic2] = unique(Sec{2}(:,1), 'stable');
Sec{2} = Sec{2}(ia2,:)
Sec = 2×1 cell array
{608×2 double} {554×2 double}
figure
plot(Sec{1}(:,1), Sec{1}(:,2))
hold on
plot(Sec{2}(:,1), Sec{2}(:,2))
hold off
title('Before Interpolation')
xv = linspace(xs, xe, 600).';
Seci{1,:} = [xv interp1(Sec{1}(:,1), Sec{1}(:,2), xv)]
Seci = 1×1 cell array
{600×2 double}
Seci{2,:} = [xv interp1(Sec{2}(:,1), Sec{2}(:,2), xv)]
Seci = 2×1 cell array
{600×2 double} {600×2 double}
ymean = mean([Seci{1}(:,2) Seci{2}(:,2)], 2);
figure
plot(Seci{1}(:,1), Seci{1}(:,2), 'DisplayName','Section #1')
hold on
plot(Seci{2}(:,1), Seci{2}(:,2), 'DisplayName','Section #1')
plot(xv, ymean, '-g', 'DisplayName','Mean')
hold off
title('After Interpolation')
legend('Location','best')
.
  1 commentaire
Stephen23
Stephen23 le 8 Sep 2023
Déplacé(e) : Stephen23 le 11 Sep 2023
Simpler without ISMEMBER and SORTROWS:
S = load('test.mat');
Xraw = S.cx;
Yraw = S.c;
[Xmin,Imin] = min(Xraw);
[Xmax,Imax] = max(Xraw);
I1 = Imin:Imax;
I2 = [Imax:numel(Xraw),1:Imin];
[X1,J1] = unique(Xraw(I1));
[X2,J2] = unique(Xraw(I2));
N = 654;
Xnew = linspace(Xmin,Xmax,N);
Ynew = mean([...
interp1(X1,Yraw(I1(J1)),Xnew);...
interp1(X2,Yraw(I2(J2)),Xnew)],1);
plot(Xraw,Yraw,Xnew,Ynew)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by