Clear Filters
Clear Filters

Gaussian fitting, peak value, and FWHM measurement method.

20 views (last 30 days)
Hi I am suhan
I will attach the file.
I have 1 to 144 columns.
I want to draw 144 gaussian histograms fitting according to the column and find the positions of 144 FWHM and Peak.
There are a few more questions.
1. The x-axis range of all 144 columns should be -300 to 300.
2. I want to set bin size to 30. (Because the range of the x-axis is -300 to 300, there will be 21 bar graphs.)
3. If bin size changes, does FWHM and Peak value change a little?
help me please
  2 Comments
수환 김
수환 김 on 21 Feb 2022
yes.
I used the code below.
M1 = readmatrix('D:/optical dat file/brightnix/20mm/7_MATLAB_20mm_diff_pivoted.txt');
figure
hold on
for k = [1,2,3,4,5,6]%[40, 45, 54, 55, 53, 56]
subplot(3,2,k)
hf{k} = histfit(M1(:,k),15); % Distribution Plot
[pks{k},locs{k},fwhm{k},prm{k}] = findpeaks(hf{k}(2).YData, hf{k}(2).XData);
xticks([-5 -2.5 -1 0 1 2.5 5])
pd{k} = fitdist(M1(:,k), 'Normal'); % Parameters
grid
title(sprintf('Column #%3d (\\mu = %5.2f, \\sigma = %5.2f FWHM = %7.2f)',k, pd{k}.mu, pd{k}.sigma,fwhm{k}))
end
hold off
I haven't been able to solve for 1,2,3.

Sign in to comment.

Answers (1)

Ayush Anand
Ayush Anand on 12 Jan 2024
Hi,
Here are the answers to the questions you asked above:
  1. To set the x-axis range to be from -300 to 300 for all histograms,you can use the "xlim" function.
  2. To set the bin size to 30 over the range of -300 to 300, you can calculate the number of bins required to cover this range with a bin width of 30 and then specify this number of bins as an argument in the "histfit" function.
  3. Yes, the FWHM and peak value might change slightly with different bin sizes because the histogram is an approximation of the underlying distribution, and changing the bin size changes this approximation.
Here's how you could modifiy the code for the same:
% M1 = readmatrix('D:/optical dat file/brightnix/20mm/7_MATLAB_20mm_diff_pivoted.txt');
% figure
% hold on
% Define the range and bin width
xRange = [-300, 300];
binWidth = 30;
% Calculate the number of bins that will cover the range with the given bin width
numBins = ceil((xRange(2)-xRange(1))/binWidth);
for k = 1:6
subplot(3,2,k)
% Use the specified number of bins and fit a normal distribution
hf{k} = histfit(M1(:,k), numBins, 'normal');
% Set the x-axis limits to range within -300 and 300
xlim(xRange)
% Find peaks and compute FWHM
[pks{k},locs{k},fwhm{k},prm{k}] = findpeaks(hf{k}(2).YData, hf{k}(2).XData, 'Annotate','extents', 'WidthReference','halfheight'); %Adding option WidthReference with halfheight so that the width value returned is FWHM
% Rest of the code remains same ...
end
% hold off
You can follow the links given below for more insights into this:
  1. https://www.mathworks.com/help/stats/histfit.html (Official documentation of the "histfit" function)
  2. https://www.mathworks.com/help/signal/ref/findpeaks.html (Official documentation of the "findpeaks" function)
  3. https://www.mathworks.com/matlabcentral/answers/741247-how-to-find-fwhm-for-each-peak (MATLAB Answer on how to find FWHM value for each peak)
Hope this helps!

Community Treasure Hunt

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

Start Hunting!