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

1 view (last 30 days)
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

Accepted Answer

Mathieu NOE
Mathieu NOE on 26 May 2021
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);

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!