How to calculate FWHM on the point
9 views (last 30 days)
Show older comments
Dear all,
I have image1 as attached (image1.png). The statistics as attached. (stats.mat)
Then I plot the graph like picture attached. (graph.jpg)
Anyone can help me to calculate the FWHM?
I try using the function fwhm as attached, but got error
Error using max
Invalid data type. First argument must be numeric or logical.
Error in fwhm (line 13)
y = y / max(y);
4 Comments
dpb
on 18 Jun 2023
whos -file stats
Well, "Houston, we have a problem!". stats is a stuct, not a pair of x,y vectors.
load stats
fieldnames(akmal)
OK, who inside there is the x,y for which to do the FWHM on?
Answers (1)
Star Strider
on 18 Jun 2023
Those fields do not exist in ‘stats.mat’ so those are empty vectors and the error is obvious.
This works —
LD = load('stats.mat')
akmal = LD.akmal
pf = LD.akmal.profile;
cx = LD.akmal.cx;
cy = LD.akmal.cy;
figure
plot3(cx, cy, pf)
grid
wx = fwhm(cx,pf)
wy = fwhm(cy,pf)
[wx,cxr] = myFWHM(cx,pf)
[wy,cyr] = myFWHM(cy,pf)
function [width,xr] = myFWHM(x, y)
p1 = polyfit(x([1 end]), y([1 end]), 1);
y_dtrnd = y - polyval(p1,x);
[ymax,yidx] = max(y_dtrnd);
[ymin,xidx] = min(y_dtrnd);
idxrng = {1:yidx; yidx:numel(y)};
xr(1) = interp1(y_dtrnd(idxrng{1})-ymin, x(idxrng{1}), (ymax-ymin)/2, 'linear');
xr(2) = interp1(y_dtrnd(idxrng{2})-ymin, x(idxrng{2}), (ymax-ymin)/2, 'linear');
width = xr(2)-xr(1);
end
My function returns slightly different values because it detrends the dependent variable first, and then interpolates to find the half-maximum values. (I coded it for fun, just to see if my values matched the others.)
.
2 Comments
Star Strider
on 18 Jun 2023
That has different independent variable values.
Try this —
F = openfig('untitled.fig');
Lines = findobj(F, 'Type','Line');
x = Lines.XData;
y = Lines.YData;
wx = fwhm(x,y)
[wm,xr,hm] = myFWHM(x,y)
[ymax,idx] = max(y);
ymin = min(y);
figure
plot(x, y)
hold on
plot(xr, [1 1]*hm+ymin, '.-r')
hold off
grid
text(x(idx), hm+ymin, sprintf('FWHM = %.3f',wm), 'Horiz','center', 'Vert','bottom')
% ylim([min(y) max(y)])
function [width,xr,hm] = myFWHM(x, y)
p1 = polyfit(x([1 end]), y([1 end]), 1);
y_dtrnd = y - polyval(p1,x);
[ymax,yidx] = max(y_dtrnd);
[ymin,xidx] = min(y_dtrnd);
idxrng = {1:yidx; yidx:numel(y)};
hm = (ymax-ymin)/2;
xr(1) = interp1(y_dtrnd(idxrng{1})-ymin, x(idxrng{1}), hm, 'linear');
xr(2) = interp1(y_dtrnd(idxrng{2})-ymin, x(idxrng{2}), hm, 'linear');
width = xr(2)-xr(1);
end
.
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!