How can I interpolate or spline a dataset into a curve?

6 Ansichten (letzte 30 Tage)
FsC
FsC am 10 Nov. 2021
Kommentiert: Mathieu NOE am 18 Nov. 2021
Hello,
I have a dataset that I need to interpolate or create a curve from; however, the datasets include different curves and I'm unsure how to interpolate or create an average line from them. I've attached an examples from one of the datasets to this post. When plotting the points it looks like this:
% for Data1 dataset
plot(Data1(:,1),Data1(:,2),'b.')
Ideally, when connecting the points or plotting a line, the curve would look something like this:
However, When I connect the points in a line this is the result:
I thought perhaps an interpolation approach might work. If I use interp1, in the following code the result is:
xq = 0:1:length(Data1(:,1))-1;
x = Data1(:,1);
y = Data1(:,2);
vq = interp1(x,y,xq,'nearest');
plot(xq,vq)
Am I applying the interp1() function incorrectly or should I be using another method? or is there a way to rearrange the matrix to get the line shown above?
Thank you for your help!
  1 Kommentar
dpb
dpb am 10 Nov. 2021
By combining the multi-valued curves, the interpolation routines have fit the input data in strict ascending order of the points globally, not sequentially in the piecewise segments.
MATLAB doesn't have builtin tools designed for such problems, unfortunately; to use interp1 or spline you'll have to treat the segments as non-overlapping segments individually and then paste the results together. That likely won't be all that satisfactory at the disjoint locations as you have overlap there.
One trick that might help would be to reverse the sense of the x-y coordinates for the LH center section -- it looks to be univalued in the plotted y-direction so that one could smooth it in x- instead.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 10 Nov. 2021
hello
this would be my suggestion
clc
clearvars
%% load('');
load('Data1.mat')
x = Data1(:,1);
y = Data1(:,2);
centroid_x = mean(x);
centroid_y = mean(y);
[theta,r] = cart2pol(x-centroid_x,y-centroid_y);
% sort theta in ascending order
[theta_sorted,ind] = sort(theta);
r_sorted = r(ind);
% remove duplicates before interpolation
[theta_unique,IA,IC] = unique(theta_sorted);
r_unique = r_sorted(IA);
% put angle in range 0 - 2pi
ind_neg = theta_unique<=0;
ind_pos = theta_unique>0;
theta_new = [theta_unique(ind_pos); theta_unique(ind_neg)+2*pi];
r_new = [r_unique(ind_pos); r_unique(ind_neg)];
% robust average ()
N = 15; % adjust smoothing factor
rr = smoothdata(r_new,'sgolay', N,'Degree',1);
[u,v] = pol2cart(theta_new,rr);
u = u + centroid_x;
v = v + centroid_y;
plot(x,y,'.b',u,v,'r');grid
  2 Kommentare
FsC
FsC am 16 Nov. 2021
Thank you for your help! I found that this approach was better for the different datasets

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Smoothing finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by