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

3 ビュー (過去 30 日間)
FsC
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?
Thank you for your help!
  1 件のコメント
dpb
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
Mathieu NOE 2021 年 11 月 10 日
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 件のコメント
FsC
FsC 2021 年 11 月 16 日
Thank you for your help! I found that this approach was better for the different datasets
Mathieu NOE
Mathieu NOE 2021 年 11 月 18 日
My pleasure !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSmoothing についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by