Incorporating local regression and smoothing splines to smooth data, can this be achieved by one of them?

1 visualización (últimos 30 días)
Hi, we are trying to build a curve-fitting model according to some constraints, and we need a bit of advice here, to see whether we are on the right track or not. The model uses two different methods of smoothing. The first one is local regression using the function
smoothdata(y,"loess",20);
The results of this function will smoothen the curve for us and would allow us to achieve certain constraints, mainly nonlinear shifting of some data points according to certain conditions, but it will not generate a completely smooth curve, there will be some parts of the generated curve that needs further smoothing. For that, we used the second method which is smoothing splines using the curve fitting toolbox.
Now, we are afraid that using two smoothing methods might be unnecessary, and we are not aware if what we want can be achieved using one method, so the question is, would there be a way to use one of these two methods to fulfill the constraints that we have and get a full smoothed curve at the end, or both methods has to be incorporated.
this is the code for the two methods and a sample of the data is attached
[myb,ib] = max(yb2);
mxb = xb2(ib);
xs = xb2;
ys = smoothdata(yb2,"loess",20);
if max(ys) > ys(ib)
ys(ib) = max(ys);
end
% the goal is to make the peak of the fitted curve as close as
% it can be to the the two data points hmf2_RO and NmF2_RO
% non linear y shift, trying to create a y shift based on x distance from peak point (exp
% distribution)
dx = abs(xs-hmf2_RO);
dy = abs(ys(ib)-NmF2_RO);
y_shift = dy*exp(-dx.^2/3000);
yss = ys + y_shift;
[fitrslt, gof1] = createFit2(xs, yss);
h1=plot(fitrslt,xs,yss);
curveHandle = findobj(h1,'DisplayName', 'fitted curve');
X = curveHandle.XData;
Y = curveHandle.YData;
function [fitresult, gof] = createFit2(xs, yss)
[xData, yData] = prepareCurveData( xs, yss );
% Set up fittype and options.
ft = fittype( 'smoothingspline' );
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'yss vs. xs', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'xs', 'Interpreter', 'none' );
ylabel( 'yss', 'Interpreter', 'none' );
grid on
end
  3 comentarios
Malak
Malak el 9 de Mayo de 2023
Editada: Malak el 9 de Mayo de 2023
Thank you for your replay. I can see that Gaussian fit is givving you a very smooth and nice curve. However, our ultimate goal is not just to fit the curve that we have.The curve that we have includes some coruppted data and we are aiming to correct this data so for each curve we have a point (nmf2, hmf2) the ones that I am attaching with the data, and w want to make the peak of our original profile as colse as it can be to this point or even be exactly on. Another issue is that, we do not know where exactly the peak of our original curve is, because again the data there are corrupted and we can't assume that the heightest value is the peak, the peak should be some how in the midile of the profile, (just as you are getting with the Gaussian fit). SO for that purpuse, I used smoothdat first to be able to some how see where the peak can be, then I applied some non liear shifftng to shift the peak point to match the point (nmf2, hmf2). but the resultant profile was not smooth, so I used smoothing splines again to obtain a smoother curve. Now can this be done in less steps and using one approach maybe and not two or is there an easier way to do this?
This is what we get when we try both approaches together
Mathieu NOE
Mathieu NOE el 9 de Mayo de 2023
hello again
a modified gaussian fit that goes exactly through the peak point
not other smoothing required ;
does it answer your need ?
load('data.mat')
x = xs;
y = yb2;
clear xs yb2
id = x<700;
x=x(id);
y=y(id);
% curve fit using fminsearch
f = @(a,c,x) a+ (NmF2_RO-a).*exp(-(x-hmf2_RO).^2 / c.^2);
obj_fun = @(params) norm(f(params(1),params(2),x)-y);
sol = fminsearch(obj_fun, [y(end) max(x)/10]);
a_sol = sol(1);
c_sol = sol(2);
xx = linspace(min(x),max(x),300);
y_fit = f(a_sol,c_sol, xx);
yy = interp1(x,y, xx);
Rsquared = my_Rsquared_coeff(yy,y_fit); % correlation coefficient
plot(hmf2_RO,NmF2_RO,'dk',x,y, 'r',xx, y_fit, 'b-' , 'Linewidth', 2.5)
title(['Gaussian Fit / R² = ' num2str(Rsquared) ], 'FontSize', 15)
ylabel('Intensity (arb. unit)', 'FontSize', 14)
xlabel('x(nm)', 'FontSize', 14)
legend('peak data','raw data','gaussian fit')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Rsquared = my_Rsquared_coeff(data,data_fit)
% R2 correlation coefficient computation
% The total sum of squares
sum_of_squares = sum((data-mean(data)).^2);
% The sum of squares of residuals, also called the residual sum of squares:
sum_of_squares_of_residuals = sum((data-data_fit).^2);
% definition of the coefficient of correlation is
Rsquared = 1 - sum_of_squares_of_residuals/sum_of_squares;
end

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Smoothing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by