Incorporating local regression and smoothing splines to smooth data, can this be achieved by one of them?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Mathieu NOE
il 9 Mag 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
Risposte (0)
Vedere anche
Categorie
Scopri di più su Smoothing in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!