Finding and Graphing FWHM (Trouble with Find Function)

Hello! After looking at many other FWHM question i've come up with the code below. I have attached that data and the plotted data as well as the data ploted with FWHM.The FWHM is obviously incorrect, it is failing in the find function. hmLeft is giving me 1 and hmRight is giving me 500.Let me know your suggestions!
load data_10micronslit.txt;
xdata10 = data_10micronslit(:,1);
ydata10 = data_10micronslit(:,2);
ymax = max(ydata10);
halfmax = ymax/2;
hmLeft = find(ydata10 >= halfmax, 1, 'first');
hmRight = find(ydata10 >= halfmax, 1, 'last');
figure
plot(xdata10,ydata10,'bo');
xlabel('Wavelength (nm)');
ylabel('Signal');
title('Signal vs Wavelength');
hold on
line([hmLeft hmLeft], [0 halfmax], 'Color', 'r', 'LineWidth', 1);
line([hmRight hmRight], [0 halfmax], 'Color', 'r', 'LineWidth', 1);
line([hmLeft hmRight], [halfmax halfmax], 'color', 'r', 'LineWidth', 1);
hold off

Answers (1)

Without ‘data_10micronslit.txt’, it is impossible to provide a specific solution. However the findpeaks function will return the FWHM of each peak it identifies (if you request that it do so). Additionally, the islocalmin and islocalmax functions can help to define the beginning and end of peaks as well as the amplitude of the peaks, and from that you can then use interp1 (twice) to calculate FWHM.

5 Comments

I have attached the data set and a bit more infomation! Thank you for your help.
Though I dont think i understand how to use the functions you have given.
This findpeaks call:
D = load('data_10micronslit.txt');
xdata10 = D(:,1);
ydata10 = D(:,2);
[~,~,fwhm,prom] = findpeaks(ydata10, xdata10, 'MinPeakProminence',1E-3)
produces:
fwhm =
0.228209696888428
EDIT — (27 Mar 2020 at 17:28)
The code to plot it becomes a bit more involved because interp1 has problems with the ‘ydata10’ vectors not being monotonic:
ymin = min(ydata10);
[pks,locs] = findpeaks(ydata10, 'MinPeakProminence',1E-3);
idxrng = [locs-20:locs; locs:locs+20]'; % Calculate ‘x’ Values To Plot ‘FWHM’
xm = xdata10(idxrng);
ym = ydata10(idxrng);
B1 = [xm(:,1), ones(size(xm(:,1)))] \ ym(:,1);
B2 = [xm(:,2), ones(size(xm(:,2)))] \ ym(:,2);
hmv = (pks-ymin)/2;
hm(1) = (hmv+ymin-B1(2))/B1(1);
hm(2) = (hmv+ymin-B2(2))/B2(1);
figure
plot(xdata10, ydata10)
hold on
plot(hm, [1 1]*hmv+ymin, '-r')
hold off
grid
text(hm(2), hmv+ymin, sprintf('\\leftarrow FWHM = %.5f',fwhm))
producing:
Is there any way to use the find function so I can plot it using "line" and use hmleft-hmright = fwhm ?
I am also finding that as I use this with othe data sets, like the one i have attached, I am getting the following as an output:
pks =
0.2038
0.2678
0.2966
0.3091
0.3441
0.3543
0.3452
0.3648
0.3676
0.3686
0.3677
0.3686
0.3588
0.3472
0.3454
0.3258
0.3158
0.2393
0.2301
locs =
251
262
267
271
279
281
283
286
288
292
294
296
299
301
303
308
310
328
330
fwhm =
0.6763
0.6262
1.1284
0.5787
0.8014
0.9827
0.7835
0.6270
1.0751
9.1444
0.7254
87.7117
0.6506
0.6384
0.9176
0.6596
0.7704
0.6441
0.7670
For the second data set, the code changes slightly:
[pks,locs] = findpeaks(ydata10, 'MinPeakProminence',0.05);
[~,~,fwhm,prom] = findpeaks(ydata10, xdata10, 'MinPeakProminence',0.05);
idxrng = [locs-90:locs; locs:locs+90]'; % Calculate ‘x’ Values To Plot ‘FWHM’
xm = xdata10(idxrng);
ym = ydata10(idxrng);
B1 = [xm(:,1), ones(size(xm(:,1)))] \ ym(:,1);
B2 = [xm(:,2), ones(size(xm(:,2)))] \ ym(:,2);
hmv = (pks-ymin)/2;
hm(1) = (hmv+ymin-B1(2))/B1(1);
hm(2) = (hmv+ymin-B2(2))/B2(1);
and produces:
NOTE — There are two findpeaks calls in my code, one to calculate the FWHM value, and the other one to return the data necessary for the plot.

Sign in to comment.

Categories

Products

Asked:

on 27 Mar 2020

Commented:

on 27 Mar 2020

Community Treasure Hunt

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

Start Hunting!