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

조회 수: 7 (최근 30일)
FsC 2021년 11월 10일
댓글: Mathieu NOE 2021년 11월 18일
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?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
dpb 2021년 11월 10일
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Mathieu NOE 2021년 11월 10일
hello
this would be my suggestion
clc
clearvars
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없음 표시없음 숨기기
FsC 2021년 11월 16일
Thank you for your help! I found that this approach was better for the different datasets
Mathieu NOE 2021년 11월 18일
My pleasure !

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by