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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by