create a 3D smoothing with csaps (or similar) (2024)

15 views (last 30 days)

Show older comments

Alberto Acri about 7 hours ago

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar

Commented: Alberto Acri 2 minutes ago

Accepted Answer: Mathieu NOE

  • curve_1.mat
  • curve_2.mat

Open in MATLAB Online

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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Mathieu NOE about 5 hours ago

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#answer_1468791

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#answer_1468791

Open in MATLAB Online

  • smoothn.m

hello again !

as your post title is about smoothing, this is what I propose , based on the fex submission :

smoothn - File Exchange - MATLAB Central (mathworks.com)

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);

3 Comments

Show 1 older commentHide 1 older comment

Mathieu NOE 14 minutes ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#comment_3181521

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#comment_3181521

Open in MATLAB Online

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 !

create a 3D smoothing with csaps (or similar) (4)

% 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

Mathieu NOE 13 minutes ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#comment_3181526

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#comment_3181526

Open in MATLAB Online

yet another alternative, uing interpolation and polynomial fitting with polyfitn

polyfitn - File Exchange - MATLAB Central (mathworks.com)

create a 3D smoothing with csaps (or similar) (6)

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 4 minutes ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#comment_3181716

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#comment_3181716

Thank you @Mathieu NOE! You always surprise me!

Sign in to comment.

More Answers (1)

Matt J about 5 hours ago

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#answer_1468786

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#answer_1468786

Edited: Matt J about 1 hour ago

Open in MATLAB Online

  • curve_1.mat

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

create a 3D smoothing with csaps (or similar) (9)

1 Comment

Show -1 older commentsHide -1 older comments

Alberto Acri 2 minutes ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#comment_3181721

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2126306-create-a-3d-smoothing-with-csaps-or-similar#comment_3181721

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

Sign in to comment.

Sign in to answer this question.

See Also

Tags

  • csaps
  • fnval
  • spline
  • curve
  • curve fitting
  • smoothing
  • 3d
  • 3d plots

Products

  • MATLAB

Release

R2021b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


create a 3D smoothing with csaps (or similar) (11)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

create a 3D smoothing with csaps (or similar) (2024)

References

Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5575

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.