How to calculate FWHM on the point

9 次查看(过去 30 天)
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 个评论
mohd akmal masud
mohd akmal masud 2023-6-18
编辑:mohd akmal masud 2023-6-18
I tried this command,
h= fwhm (xstats, ystats); %xstats and ystats is from my data as attached
xstats and ystats is form my data as attached
But got error
dpb
dpb 2023-6-18
whos -file stats
Name Size Bytes Class Attributes akmal 1x1 2184 struct
Well, "Houston, we have a problem!". stats is a stuct, not a pair of x,y vectors.
load stats
fieldnames(akmal)
ans = 9×1 cell array
{'distance'} {'mean' } {'STD' } {'min' } {'max' } {'profile' } {'cx' } {'cy' } {'position'}
OK, who inside there is the x,y for which to do the FWHM on?

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2023-6-18
Those fields do not exist in ‘stats.mat’ so those are empty vectors and the error is obvious.
This works —
LD = load('stats.mat')
LD = struct with fields:
akmal: [1×1 struct]
akmal = LD.akmal
akmal = struct with fields:
distance: 23.5756 mean: 1.3427e+03 STD: 938.0745 min: 496 max: 3189 profile: [25×1 double] cx: [25×1 double] cy: [25×1 double] position: [2×2 double]
pf = LD.akmal.profile;
cx = LD.akmal.cx;
cy = LD.akmal.cy;
figure
plot3(cx, cy, pf)
grid
wx = fwhm(cx,pf)
Pulse Polarity = Positive Pulse is Impulse or Rectangular with 2 edges
wx = 8.0832
wy = fwhm(cy,pf)
Pulse Polarity = Positive Pulse is Impulse or Rectangular with 2 edges
wy = -0.0198
[wx,cxr] = myFWHM(cx,pf)
wx = 7.0372
cxr = 1×2
72.4166 79.4537
[wy,cyr] = myFWHM(cy,pf)
wy = -0.0172
cyr = 1×2
58.0415 58.0243
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 个评论
mohd akmal masud
mohd akmal masud 2023-6-18
If I have the graph as attached, how to calculate the FWHM?
Star Strider
Star Strider 2023-6-18
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)
Pulse Polarity = Positive Pulse is Impulse or Rectangular with 2 edges
wx = 8.0080
[wm,xr,hm] = myFWHM(x,y)
wm = 6.9718
xr = 1×2
9.0430 16.0148
hm = 1.3460e+03
[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
.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by