To get a better set of nodes arranged in space as a ‘curve’, how can I improve this code? Could you suggest?
load curve_1
cc = curve_1; %or curve_2
x = cc(:,1);
y = cc(:,2);
z_min = min(cc(:,3));
z_max = max(cc(:,3));
z = z_min:0.1:z_max;
z = z';
% code
[pp,p] = csaps(z,[x;y]);
val = fnval(pp,z);
figure
plot3(x,y,z);
hold on
plot3(val(1,:),val(2,:),z,'r-')
grid on

 Réponse acceptée

Mathieu NOE
Mathieu NOE le 7 Juin 2024
hello again !
as your post title is about smoothing, this is what I propose , based on the fex submission :
I attached the function in my answer if it makes your life simpler
now, maybe we should also create some intermediate points with interpolation (will be done just after this first answer)
so the starter :
load curve_1
cc = curve_1; %or curve_2
% load curve_2
% cc = curve_2; %or curve_2
x = cc(:,1);
y = cc(:,2);
z = cc(:,3);
% smoothing (if needed)
[zz,s,exitflag] = smoothn({x,y,z},1);
xn = zz{1};
yn = zz{2};
zn = zz{3};
plot3(x,y,z,'*');
hold on
plot3(xn,yn,zn);

4 commentaires

now with interpolation (to generate more points ) before smoothing
now you can play with interpolation number of points , method (here spline to already bring some smoothness) and last stage is smoothn itself (with optionnal smoothing factor S)
hope it helps !
% load curve_1
% cc = curve_1; %or curve_2
load curve_2
cc = curve_2; %or curve_2
x = cc(:,1);
y = cc(:,2);
z = cc(:,3);
% unique & sort z ascending (for interpolation purposes)
[z,ia,ic] = unique(z);
x = x(ia);
y = y(ia);
% interpolation first
z_min = min(z);
z_max = max(z);
zi = linspace(z_min,z_max,100);
xi = interp1(z,x,zi,'spline');
yi = interp1(z,y,zi,'spline');
% smoothing (if needed)
[zzz,s,exitflag] = smoothn({xi,yi,zi},1);
xn = zzz{1};
yn = zzz{2};
zn = zzz{3};
plot3(x,y,z,'dr');
hold on
plot3(xi,yi,zi,'*g');
plot3(xn,yn,zn,'b');
hold off
yet another alternative, uing interpolation and polynomial fitting with polyfitn
load curve_1
cc = curve_1; %or curve_2
% load curve_2
% cc = curve_2; %or curve_2
x = cc(:,1);
y = cc(:,2);
z = cc(:,3);
% unique & sort z ascending (for interpolation purposes)
[z,ia,ic] = unique(z);
x = x(ia);
y = y(ia);
% interpolation first
z_min = min(z);
z_max = max(z);
zi = linspace(z_min,z_max,100)';
xi = interp1(z,x,zi,'spline');
yi = interp1(z,y,zi,'spline');
% polynomial fit
% % FEX : https://fr.mathworks.com/matlabcentral/fileexchange/34765-polyfitn?s_tid=ta_fx_results
order = 1;
p = polyfitn([xi,yi],zi,order);
pC = p.Coefficients; % get the polynomial coefficients
pTerms = p.ModelTerms;
% create the polynomial model (z = f(x,y))
zt = 0;
for k = 1:numel(pC)
zt = zt + pC(k)*(xi.^pTerms(k,1)).*(yi.^pTerms(k,2)); %
end
figure(1),
plot3(x,y,z,'r*',xi,yi,zt,'k','linewidth',2); %
xlabel('X');
ylabel('Y');
zlabel('Z');
legend('raw data','fitted curve');
axis tight square
Alberto Acri
Alberto Acri le 7 Juin 2024
Thank you @Mathieu NOE! You always surprise me!
Mathieu NOE
Mathieu NOE le 10 Juin 2024
My pleasure , and thanks again for accepting my answer
you can also reward other contributors by voting

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 7 Juin 2024
Modifié(e) : Matt J le 7 Juin 2024
load curve_1
cc = curve_1; %or curve_2
x = cc(:,1);
y = cc(:,2);
z = cc(:,3);
t=linspace(0,1,height(cc));
tu=linspace(0,1,5*height(cc));
% code
[pp,p] = csaps(t,cc',0.995);
val = fnval(pp,tu);
figure
plot3(x,y,z,'o');
hold on
plot3(val(1,:),val(2,:),val(3,:),'r-')
grid on

1 commentaire

Alberto Acri
Alberto Acri le 7 Juin 2024
Thank you! I accepted Mathieu's answer because it gave me more solutions. However, your answer is well accepted and I cannot accept both!

Connectez-vous pour commenter.

Catégories

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by