Fit gaussian to X-Y data (one X column, multiple Y column), for each Y column from an excel file.

16 visualizzazioni (ultimi 30 giorni)
Hi, I want to fit gaussian curves to each Y column for an X-Y data set. I have ued cftool but I want to automatize the process which can give me FWHM values for each y column and also save the image of the fit so that I can see which fits are good and which are not. I can do these for each X-Y data one by one but not at once. I'll really appreciate any help.

Risposta accettata

Star Strider
Star Strider il 5 Apr 2024
Modificato: Star Strider il 5 Apr 2024
One approach —
T1 = readtable('X-Y_gauss_matlab.xlsx')
T1 = 63x38 table
X Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9 Y10 Y11 Y12 Y13 Y14 Y15 Y16 Y17 Y18 Y19 Y20 Y21 Y22 Y23 Y24 Y25 Y26 Y27 Y28 Y29 Y30 Y31 Y32 Y33 Y34 Y35 Y36 Y37 _____ ________ ________ _________ ________ ________ ________ ________ _________ _________ ________ ________ ________ ________ ________ _________ ________ ________ ________ ________ _________ ________ ________ _________ _________ _________ ________ ________ ________ _________ ________ ________ ________ _________ ________ ________ _________ _________ 0.064 0.030798 0.038794 0.029972 0.040885 0.016881 0.078605 0.26918 0.1234 0.013885 0.045108 0.12755 0.15063 0.082613 0.53255 0.025195 0.18746 0.24425 0.050968 0.075806 0.010603 0.10471 0.10975 0.023132 0.099023 0.57571 0.024438 0.027075 0.036342 0.080198 0.1447 0.058616 0.15126 0.11308 0.23208 0.36799 0.044399 0.070281 0.128 0.06169 0.019298 0.011704 0.062252 0.015978 0.069837 0.21943 0.15019 0.010553 0.063461 0.085626 0.22516 0.05613 0.32751 0.039427 0.27566 0.184 0.088615 0.04211 0.0016198 0.12562 0 0.03377 0.13907 0.43443 0.060324 0.017079 0.075535 0.17466 0.051017 0.031654 0.044126 0.0479 0.21634 0.25601 0.0059801 0.10447 0.192 0.032765 0.027719 0.0098855 0.057489 0.057433 0.052132 0.18802 0.13075 0.027279 0.060614 0.095036 0.134 0.055852 0.32477 0.10865 0.13021 0.059066 0.10545 0.062775 0.035667 0.10617 0.19332 0.008348 0.043763 0.21091 0.26761 0.017389 0.074438 0.093553 0.13062 0.020362 0.12808 0.037354 0.15918 0.21279 0 0.11794 0.256 0.023859 0.04043 0 0.041395 0.03676 0.060961 0.19773 0.069731 0.045914 0.032516 0.072278 0.094119 0.010724 0.14872 0.10694 0.092725 0.055407 0.10232 0.050552 0.035112 0.15325 0.060921 0.021956 0.06924 0.27457 0.11268 0.041108 0.024393 0.10103 0.040472 0.014986 0.30953 0.04599 0.071291 0.17893 0.071235 0.0037564 0.32 0.041612 0.049417 0.0038388 0.060509 0.055972 0.057739 0.14281 0.039431 0.0099566 0.083844 0.054314 0.1064 0.082712 0.023722 0.019201 0.051014 0.023255 0.096026 0.065852 0.019424 0.12326 0.069016 0.010306 0.04773 0.34025 0.15759 0.03136 0.017577 0.058612 0.086855 0 0.17127 0.0071622 0.080854 0.085359 0.06736 0 0.384 0.038373 0.057738 0.0081141 0.059651 0.020788 0.10074 0.079793 0.0095542 0.023948 0.047429 0.067464 0 0.044731 0.25076 0.033327 0.17791 0.034545 0.017433 0.19923 0.03199 0.095854 0.089752 0.0036772 0.0036663 0.29529 0.08646 0.01272 0.028724 0.095145 0.10826 0.023613 0.20942 0.0059219 0.20557 0.04685 0.093139 0.040676 0.448 0.06749 0.093487 0.033561 0.048297 0.01616 0.10614 0.078396 0.0015332 0.048226 0.040624 0.017303 0.085375 0.03636 0.21911 0.036675 0.16085 0.12218 0 0.20108 0.028641 0.13255 0.29926 0.0067631 0.004442 0.19197 0.060076 0.017963 0.048686 0.027998 0.14082 0.045327 0.27401 0.0018583 0.225 0.045957 0.038326 0.049152 0.512 0.050722 0.10568 0.058148 0.078314 0.034408 0.042904 0.11838 0 0.050101 0.035744 0.048339 0.11279 0.11461 0.20831 0.0018235 0.18147 0 0.17375 0.43016 0.051555 0.050419 0.21126 0.0058173 0.004375 0.11105 0.084146 0.03514 0.042251 0.039399 0.10909 0.085016 0.19538 0.041225 0.097833 0.02632 0.14962 0.062301 0.576 0.035318 0.072829 0.091737 0.06552 0.090155 0.08793 0.12634 0.0074965 0.077273 0.082364 0.03136 0.16869 0.053318 0 0.0421 0.12435 0.081263 0.25551 0.30456 0.033657 0.080307 0.16628 0 0 0 0.21408 0.06873 0.035546 0 0.044038 0.11933 0.16767 0.071668 0.29323 0.012788 0.1241 0.12405 0.64 0.034674 0.031214 0.06114 0.047219 0.067666 0.06003 0.098754 0.026198 0.095185 0.056276 0.084384 0.16466 0.080458 0.1773 0.035281 0.42418 0.10058 0.21704 0.28121 0.084128 0.11197 0.14384 0.006286 0.001661 0.0049921 0.11141 0.12356 0.051063 0.0096785 0 0.046122 0.28932 0.088354 0.16501 0.012231 0.066151 0.17424 0.704 0.048715 0.061641 0.10451 0.022529 0.052977 0.030962 0.075159 0.021505 0.075372 0.076981 0.10328 0.1198 0.07875 0.67277 0.10489 0.50686 0.21341 0.15129 0.23115 0.12939 0.078386 0.086338 0.0095904 0.0067286 0.087635 0.16924 0.13992 0.03603 0.0071431 0.081141 0.058019 0.16464 0.06386 0.064218 0 0.02943 0.075007 0.768 0.077618 0.10561 0.15487 0.029528 0.025128 0.035331 0.062979 0.02403 0.046254 0.11018 0.074527 0.12585 0.085157 0.38661 0.13972 0.30792 0.16706 0.25553 0.40029 0.077905 0.22675 0.31776 0.015016 0.0010416 0.1617 0.33814 0.26391 0.01498 0.08645 0.030974 0.018377 0.10397 0.10942 0.21984 0.070804 0.16024 0.22619 0.832 0.099608 0.078898 0.18479 0.088961 0.11727 0.095537 0.030588 0.017666 0.057021 0.11192 0.14461 0.14144 0.031997 0.06141 0.12569 0.1974 0.28255 0.20494 0.4501 0.12297 0.08665 0.31307 0.029519 0.015925 0.19169 0.41201 0.47479 0.020897 0.1396 0.043433 0.046735 0.066803 0.11414 0.2242 0.14525 0.21926 0.32311 0.896 0.16745 0.1639 0.25539 0.069182 0.1776 0.023333 0 0.07201 0.073571 0.15843 0.18944 0.20053 0.071579 0.31282 0.095397 0.28262 0.4751 0.19589 0.49052 0.17988 0 0.44937 0.040895 0.026264 0.17155 0.15542 0.45406 0.10567 0.1379 0.010986 0.0952 0 0.10544 0.2378 0.15684 0.090967 0.25557 0.96 0.12433 0.22057 0.40548 0.069287 0.15946 0.026805 0.17551 0.17491 0.064023 0.27192 0.13754 0.17289 0.1147 0.13345 0.10048 0.24045 0.2226 0.31921 0.59812 0.18295 0.13361 0.11057 0.07012 0.092458 0.17325 0.28656 0.60705 0.086819 0.19376 0.078764 0.13623 0.11835 0.31944 0.12802 0.088153 0.079049 0.49159 1.024 0.15362 0.21641 0.51064 0.14991 0.27654 0.058094 0.14353 0.26194 0.094462 0.44827 0.18547 0.31779 0.14522 0.082922 0.14205 0.52205 0.10252 0.36428 0.74601 0.21071 0.1898 0.11874 0.20594 0.11716 0.32766 0.57333 0.65081 0.12891 0.26187 0.24417 0.097386 0.19772 0.18845 0.22346 0.20964 0.13663 0.46538
figure
plot(T1{:,1}, T1{:,2:end})
grid
gausfit = @(b,x) b(1) .* exp(-(x-b(2)).^2 .* b(3));
opts = optimset('MaxFunEvals',1E+5, 'MaxIter',1E+4);
for k = 1:size(T1,2)-1
xy = [T1{:,1}, T1{:,k+1}];
xy(~isfinite(xy)) = NaN;
xy = rmmissing(xy);
[col(:,k),nres(k)] = fminsearch(@(b) norm(xy(:,2) - gausfit(b,xy(:,1))), [1; 2; 1], opts);
idxrng = xy(:,1) <= col(2,k);
mp(1) = interp1(xy(idxrng,2), xy(idxrng,1), col(1,k)/2);
mp(2) = interp1(xy(~idxrng,2), xy(~idxrng,1), col(1,k)/2);
FWHM(k,:) = mp(2) - mp(1);
end
col
col = 3x37
0.9496 0.8603 0.8023 0.9041 0.8848 0.7060 0.7223 0.8846 0.9125 0.8137 0.8309 0.9013 0.6050 0.6557 0.7313 0.5729 0.5558 0.7464 0.7398 0.6817 0.6901 0.7987 0.8932 0.7846 0.6588 0.7282 0.8391 0.8946 0.8854 0.8695 2.1357 1.8875 1.9204 2.2754 1.9475 2.4601 2.0894 1.8739 1.9940 1.6258 2.1818 1.9325 2.1332 2.4166 2.2081 1.8655 1.8548 1.6003 1.7762 2.2029 2.1495 2.4899 1.5872 2.0944 2.0241 1.8957 1.6650 2.2148 2.0585 2.0647 1.9333 1.3161 0.9603 2.3480 1.1845 1.5916 1.3139 1.9455 2.7897 1.6352 0.7491 1.1086 1.7750 0.3967 1.5023 0.4873 0.5657 1.5090 0.5720 0.8267 1.1991 1.1266 4.0319 1.7544 0.4515 0.8603 1.1021 1.5624 1.0538 1.4435
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
nres
nres = 1x37
0.5258 1.2453 1.2504 0.7134 0.8159 1.2814 1.1824 0.5487 0.4621 1.0618 0.9878 1.1204 0.7640 1.4497 0.7399 1.2904 1.3172 1.0538 1.0972 1.1886 1.0251 1.2761 0.7087 0.5941 1.3318 1.1261 0.8454 0.7230 0.8007 0.8497
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
tcol = [(2:size(T1,2)).' col.' FWHM nres(:)];
Results = array2table(tcol, 'VariableNames',{'Column','Height','Centre','Scale','FWHM','Res Norm'})
Results = 37x6 table
Column Height Centre Scale FWHM Res Norm ______ _______ ______ _______ _______ ________ 2 0.94959 2.1357 1.9333 1.2012 0.52575 3 0.86031 1.8875 1.3161 1.4809 1.2453 4 0.80229 1.9204 0.96026 1.6484 1.2504 5 0.90413 2.2754 2.348 1.1044 0.71344 6 0.88476 1.9475 1.1845 1.6568 0.81589 7 0.70597 2.4601 1.5916 1.3829 1.2814 8 0.72228 2.0894 1.3139 1.6576 1.1824 9 0.88458 1.8739 1.9455 1.2399 0.54867 10 0.91246 1.994 2.7897 0.97875 0.46206 11 0.81373 1.6258 1.6352 0.93968 1.0618 12 0.83092 2.1818 0.74911 1.9541 0.9878 13 0.9013 1.9325 1.1086 1.5739 1.1204 14 0.60498 2.1332 1.775 1.3643 0.76402 15 0.6557 2.4166 0.39668 3.1505 1.4497 16 0.73127 2.2081 1.5023 1.4948 0.73994 17 0.57294 1.8655 0.4873 2.4991 1.2904
figure
tiledlayout(6,7)
for k = 1:size(col,2)
nexttile
plot(T1{:,1}, T1{:,k+1})
hold on
plot(T1{:,1}, gausfit(col(:,k),T1{:,1}))
hold off
grid
title("Column "+(k+1))
end
EDIT — (5 Apr 2024 at 13:37)
Added ‘opts’ to the fminsearch call, added ‘FWHM’.
.

Più risposte (2)

cui,xingxing
cui,xingxing il 5 Apr 2024
Modificato: cui,xingxing il 27 Apr 2024
You can export a function in the cftool APP to facilitate subsequent simultaneous manipulation of multiple variables.
For Example:
For Gaussian models, set up the various hyperparameters you need, and when your conditions are met, select the second option of the "export" button to export as a function.
function [fitresult, gof] = createFit(x, y1)
%CREATEFIT(X,Y1)
% 创建一个拟合。
%
% 要进行 '无标题拟合 1' 拟合的数据:
% X 输入: x
% Y 输出: y1
% 输出:
% fitresult: 表示拟合的拟合对象。
% gof: 带有拟合优度信息的结构体。
%
% 另请参阅 FIT, CFIT, SFIT.
% 由 MATLAB 于 05-Apr-2024 20:57:06 自动生成
%% 拟合: '无标题拟合 1'。
[xData, yData] = prepareCurveData( x, y1 );
% 设置 fittype 和选项。
ft = fittype( 'gauss1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf 0];
opts.StartPoint = [1 2.048 0.375013106757709];
% 对数据进行模型拟合。
[fitresult, gof] = fit( xData, yData, ft, opts );
% 绘制数据拟合图。
figure( 'Name', '无标题拟合 1' );
h = plot( fitresult, xData, yData );
legend( h, 'y1 vs. x', '无标题拟合 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% 为坐标区加标签
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y1', 'Interpreter', 'none' );
grid on
end
Finally, just use each X-Y data values in your script file to call the createFit function separately.
data = readmatrix("X-Y_gauss_matlab.xlsx");
x = data(:,1);
y1 = data(:,2);
createFit(x,y1)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.9496 (0.9053, 0.9939) b1 = 2.136 (2.108, 2.163) c1 = 0.7192 (0.6804, 0.7579)
for i = 2:size(data,2)
createFit(x,data(:,i))
end
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.9496 (0.9053, 0.9939) b1 = 2.136 (2.108, 2.163) c1 = 0.7192 (0.6804, 0.7579)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8603 (0.765, 0.9557) b1 = 1.888 (1.809, 1.966) c1 = 0.8717 (0.76, 0.9833)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8023 (0.7137, 0.8909) b1 = 1.92 (1.828, 2.012) c1 = 1.02 (0.8896, 1.151)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.9041 (0.841, 0.9672) b1 = 2.275 (2.238, 2.313) c1 = 0.6526 (0.6, 0.7052)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8848 (0.8239, 0.9456) b1 = 1.947 (1.896, 1.999) c1 = 0.9188 (0.8457, 0.9918)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.706 (0.603, 0.8089) b1 = 2.46 (2.366, 2.554) c1 = 0.7927 (0.6591, 0.9263)
Warning: Removing NaN and Inf from data
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7225 (0.6312, 0.8138) b1 = 2.089 (1.999, 2.179) c1 = 0.8718 (0.7445, 0.9991)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8846 (0.8383, 0.9309) b1 = 1.874 (1.843, 1.905) c1 = 0.7169 (0.6736, 0.7603)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.9125 (0.8698, 0.9552) b1 = 1.994 (1.971, 2.017) c1 = 0.5987 (0.5663, 0.631)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8135 (0.7277, 0.8993) b1 = 1.626 (1.559, 1.693) c1 = 0.7824 (0.687, 0.8779)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8309 (0.7648, 0.8969) b1 = 2.182 (2.107, 2.257) c1 = 1.155 (1.047, 1.264)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.9013 (0.8191, 0.9835) b1 = 1.932 (1.862, 2.003) c1 = 0.9498 (0.8495, 1.05)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.6051 (0.542, 0.6681) b1 = 2.133 (2.069, 2.197) c1 = 0.7504 (0.6601, 0.8406)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.6558 (0.5699, 0.7417) b1 = 2.417 (2.239, 2.594) c1 = 1.587 (1.297, 1.877)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7313 (0.6727, 0.7899) b1 = 2.208 (2.155, 2.261) c1 = 0.8158 (0.7404, 0.8912)
Warning: Removing NaN and Inf from data
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.573 (0.4931, 0.6529) b1 = 1.865 (1.702, 2.029) c1 = 1.432 (1.177, 1.687)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.5559 (0.4728, 0.639) b1 = 1.854 (1.692, 2.017) c1 = 1.329 (1.085, 1.573)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7463 (0.6627, 0.8298) b1 = 1.6 (1.526, 1.675) c1 = 0.8142 (0.7087, 0.9197)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7398 (0.6704, 0.8093) b1 = 1.776 (1.674, 1.878) c1 = 1.322 (1.169, 1.476)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.6817 (0.6004, 0.7631) b1 = 2.203 (2.096, 2.31) c1 = 1.1 (0.9464, 1.253)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.6902 (0.6135, 0.7669) b1 = 2.15 (2.067, 2.232) c1 = 0.9131 (0.7958, 1.03)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7987 (0.7043, 0.893) b1 = 2.49 (2.399, 2.581) c1 = 0.9423 (0.8122, 1.072)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8932 (0.8214, 0.9649) b1 = 1.587 (1.555, 1.62) c1 = 0.4981 (0.4518, 0.5443)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7846 (0.7358, 0.8335) b1 = 2.094 (2.056, 2.133) c1 = 0.755 (0.7007, 0.8093)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.6588 (0.5784, 0.7392) b1 = 2.024 (1.875, 2.173) c1 = 1.488 (1.255, 1.721)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7282 (0.6504, 0.806) b1 = 1.896 (1.802, 1.99) c1 = 1.078 (0.9437, 1.212)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8392 (0.7771, 0.9013) b1 = 1.665 (1.608, 1.722) c1 = 0.9525 (0.8703, 1.035)
Warning: Removing NaN and Inf from data
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8946 (0.8363, 0.9529) b1 = 2.215 (2.172, 2.257) c1 = 0.8001 (0.7399, 0.8603)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8854 (0.8274, 0.9435) b1 = 2.059 (2.006, 2.111) c1 = 0.9741 (0.9002, 1.048)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.8695 (0.8029, 0.9361) b1 = 2.065 (2.013, 2.117) c1 = 0.8323 (0.7587, 0.9059)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7895 (0.7384, 0.8406) b1 = 2.173 (2.135, 2.212) c1 = 0.7346 (0.6797, 0.7896)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.6753 (0.5939, 0.7567) b1 = 2.317 (2.196, 2.438) c1 = 1.229 (1.051, 1.408)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7248 (0.667, 0.7827) b1 = 2.135 (2.071, 2.2) c1 = 0.9907 (0.8991, 1.082)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.6662 (0.579, 0.7533) b1 = 1.994 (1.869, 2.119) c1 = 1.172 (0.9915, 1.352)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7218 (0.6453, 0.7983) b1 = 2.256 (2.165, 2.348) c1 = 1.057 (0.9265, 1.188)
Warning: Removing NaN and Inf from data
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.6867 (0.6168, 0.7566) b1 = 1.962 (1.893, 2.031) c1 = 0.8268 (0.7296, 0.9241)
ans =
General model Gauss1: ans(x) = a1*exp(-((x-b1)/c1)^2) Coefficients (with 95% confidence bounds): a1 = 0.7296 (0.6591, 0.8001) b1 = 1.566 (1.496, 1.635) c1 = 0.8816 (0.7824, 0.9807)
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com

Mathieu NOE
Mathieu NOE il 5 Apr 2024
coming late in the show
try this ...
if your data is "good" you can this this result , otherwise the fit may not allow any FWHM computation
% Importing Data from excel across multiple sheets.
filename = 'X-Y_gauss_matlab.xlsx';
T = readmatrix(filename,"NumHeaderLines",1); % X Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9 Y10 Y11 Y12 Y13 Y14 Y15 Y16 Y17 Y18 Y19 Y20 Y21 Y22 Y23 Y24 Y25 Y26 Y27 Y28 Y29 Y30 Y31 Y32 Y33 Y34 Y35 Y36 Y37
x = T(:,1);
for k= 1:size(T,2)-1
% Fitting data to a gaussian
yy = T(:,1+k);
yfit = my_gauss_fit(x,yy,x);
% FWHM computation (if possible)
threshold = max(yfit)/2;
[p1,p2] = find_zc(x,yfit,threshold);
if ~isempty(p1) & ~isempty(p2)
FWHM(k) = (p2-p1)
flag = 1;
else
FWHM(k) = NaN;
flag = 0;
end
figure(k)
plot(x,yy,'-',x,yfit, '-', 'LineWidth', 2);
if flag>0
hold on
plot([p1 p2],[threshold threshold],'r', 'LineWidth', 1);
plot(p1,threshold,'.r',p2,threshold,'.r', 'Markersize', 40);
text(p1*1.1,threshold*1.1,['FWHM = ' num2str(FWHM(k))]);
hold off
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function yfit = my_gauss_fit(x,y,xfit)
% Define g(x) = a*exp(-(x-mu)^2/(2*sigma^2)):
g = @(A,X) A(1)*exp(-(X-A(2)).^2/(2*A(3)^2));
%% ---Fit Data: Analitical Strategy---
% Cut Gaussian bell data
ymax = max(y); xnew=[]; ynew=[];
ind = y > 0.25*ymax;
xnew = x(ind);
ynew = y(ind);
% Fitting
ylog=log(ynew); xlog=xnew; B=polyfit(xlog,ylog,2);
% Compute Parameters
sigma=sqrt(-1/(2*B(1))); mu=B(2)*sigma^2; a=exp(B(3)+mu^2/(2*sigma^2));
% Plot fitting curve
A=[a,mu,sigma];
yfit = g(A,xfit);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% put data in rows
x = x(:);
y = y(:);
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end

Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by