How do I find the x and y coordinates of a fitted Gaussian curve at a given slope?
12 次查看(过去 30 天)
显示 更早的评论
I have written a code that fits my data with a Gaussian curve. I need to be able to set a baseline for multiple images with the same defined parameter then caluclate the full width-half maximum and slope between the baseline and maximum for each curve. What I'm thinking is to define the baseline as the point where the slope of the curve is equal to -1 (or another value). There may be other ways to do this, but it just needs to be consistant over all trials.
My code is:
clear
I=3; sumon=0; %holds variable of images to be added%
for k=[7:9]; %loop to add images together%
on{k}=imread(sprintf('on_ %d.tif',k)); %reads on files at _k%
on{k}=double(on{k}); %stores variable as 64 bit, double precision floating variable%
sumon=sumon+(on{k}); %adds image k to previous image%
end %end for loop%
on=sumon/I %averages images taken%
sumoff=0; I=3; %holds variable of images to be added%
for k=[4:6]; %loop to add images together%
off{k}=imread(sprintf('off_ %d.tif',k));%reads on files at _k%
off{k}=double(off{k}); %stores variable as 64 bit, double precision floating variable%
sumoff=sumoff+(off{k}); %adds image k to previous image%
end %end for loop%
off=sumoff/I %averages images taken%
result=on-off %subtracts averaged off image from average on image%
imshow(result) %shows result%
% rotate
result=imrotate(result,-93,'bilinear')
%crop image
result=imcrop(result, [100, 450, 800, 200])
%show image
imshow(result)
%% Plotting intensity vs x pixel index
vertsum = sum(result, 1)
vertsum =vertsum-max(vertsum)
% Set data points
[xData, yData] = prepareCurveData( [], vertsum );
% Set up fittype and options.
ft = fittype( 'gauss2' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf 0 -Inf -Inf 0];
opts.StartPoint = [0 301 300 0 601 300];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'vertsum', 'Gaussian fit', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
ylabel( 'vertsum', 'Interpreter', 'none' );
grid on
I've also included a picture of the resulting curve. In this case I'd like the baseline to be set at ~150,-3, and the maximum at -350, -15. I already have it written to culculate the FWHM and slope between these points if I just manually set the baseline. Thanks!
0 个评论
回答(1 个)
Star Strider
2024-6-17
Once you have the equation for the gaussian, the rest is striaightforward. Find the midpoint in the mamplitude to determine the half-maximum p[oint, then interpolate to find the independent variable values at those points. A simplie linear regression will then provide the approximate slopes at those points, although you can also use the gradient function for that. The full-width-half-maximum (FWHM) is simply the difference between the independent variable values at the midpoint intersections.
I would do something like this —
xv = linspace(0, 900);
yv = -exp(-(0.01*(xv-350)).^2);
% dydx = gradient(yv, xv);
[y1,y2] = bounds(yv);
ys = y1-y2;
thrshld = ys/2;
zxi = find(diff(sign(yv-thrshld)));
for k = 1:numel(zxi)
idxrng = (zxi(k)-2) : (zxi(k)+2);
xq(k,:) = interp1(yv(idxrng), xv(idxrng), thrshld);
% dq(k,:) = interp1(yv(idxrng), dydx(idxrng), thrshld);
B(:,k) = [xv(idxrng); ones(size(idxrng))].' \ yv(idxrng).';
dq(k,:) = B(1,k);
xl(k,:) = xv(idxrng);
yl(k,:) = [xv(idxrng); ones(size(idxrng))].' * B(:,k);
end
FWHM = diff(xq)
Results = table(xq, dq, 'VariableNames',{'x','dydx'})
figure
plot(xv, yv)
hold on
plot(xq, ones(size(xq))*thrshld, 'sr')
for k = 1:numel(zxi)
plot(xl(k,:), yl(k,:), '-r')
end
hold off
grid
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!