Is there a way to smoothen the function/"bridge the gap"?

1 回表示 (過去 30 日間)
Teshan Rezel
Teshan Rezel 2021 年 5 月 26 日
コメント済み: Mathieu NOE 2021 年 5 月 27 日
Hi folks,
I have a graph as follows. I am looking to replace the valley (red) with smoothened data. Is there a way to do this? I am unaware of the best method of achieving this currently.
Thanks

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 5 月 26 日
hello
this would be me suggestion if the idea is to replace the "faulty" data segment with a piecewise linear segment and then do a further smoothing
%--- Example #2: smooth a curve / wide valley removal ---
x = linspace(0,100,256);
y = cos(x/10)+(x/50).^2;
y([70:120]) = - 2;
y = y+ randn(size(x))/10;
yy = y;
% define segment to be removed based on sharp slope changes
dy = gradient(y);
% remove bad data "in the valley"
[val_min,ind_min] = min(dy);
[val_max,ind_max] = max(dy);
ind_offset = 2; % expand a bit the data segment beyond the limits ind_min / ind_max => ind_min-ind_offset / ind_max+ind_offset
y(ind_min-ind_offset:ind_max+ind_offset) = linspace(y(ind_min-ind_offset),y(ind_max+ind_offset),ind_max-ind_min+1+2*ind_offset); % replace outliers by linear segment
N = 25;
ys = smoothdata(y,'gaussian',N);
figure (3);
plot(x,yy,'b',x,y,'k',x,ys,'r-.','LineWidth',2);
legend('raw data','interpolated data','interpolated and smoothed data')
now there is another possibility is to "shift" upwards this segement (if we want to keep the data) and do a smoothing as well
%--- Example #3: shift the "valley" to be (more or less) aligned with rest of data ---
x = linspace(0,100,256);
y = cos(x/10)+(x/50).^2;
y([70:120]) = - 2;
y = y+ randn(size(x))/10;
yy = y;
% define segment to be shifted based on sharp slope changes
dy = gradient(y);
% remove bad data "in the valley"
[val_min,ind_min] = min(dy);
[val_max,ind_max] = max(dy);
ind_offset1 = 0; % expand a bit the data segment beyond the limits ind_min / ind_max => ind_min-ind_offset / ind_max+ind_offset
ind_offset2 = 0; % expand a bit the data segment beyond the limits ind_min / ind_max => ind_min-ind_offset / ind_max+ind_offset
delta = (y(ind_min-ind_offset1) + y(ind_max+ind_offset2))/2 - mean(y(ind_min-ind_offset1:ind_max+ind_offset2));
y(ind_min-ind_offset1:ind_max+ind_offset2) = y(ind_min-ind_offset1:ind_max+ind_offset2) + delta; % shift segment based on mean value
N = 25;
ys = smoothdata(y,'gaussian',N);
  2 件のコメント
Teshan Rezel
Teshan Rezel 2021 年 5 月 26 日
@Mathieu NOE this is perfect, thank you!
Mathieu NOE
Mathieu NOE 2021 年 5 月 27 日
My pleasure !

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by