Clear Filters
Clear Filters

How to calculate FWHM on the point

9 views (last 30 days)
mohd akmal masud
mohd akmal masud on 18 Jun 2023
Commented: Star Strider on 18 Jun 2023
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
mohd akmal masud
mohd akmal masud on 18 Jun 2023
Edited: mohd akmal masud on 18 Jun 2023
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 on 18 Jun 2023
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?

Sign in to comment.

Answers (1)

Star Strider
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')
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 Comments
mohd akmal masud
mohd akmal masud on 18 Jun 2023
If I have the graph as attached, how to calculate the FWHM?
Star Strider
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)
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
.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!