How to calculate the FWHM from multiple spectra? (2024)

14 views (last 30 days)

Show older comments

Sam on 20 Jun 2022

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra

Commented: Star Strider on 21 Jun 2022

Accepted Answer: Star Strider

  • example.csv

Open in MATLAB Online

Hi,

I have been looking over the documentation for findpeaks and several FWHM scripts, but I am not having any luck.

I have multiple .csv files of a single peak of Raman data, with the magnitude and wavelength. I would like to have a script that reads each .csv file and calculate the FWHM of the peak.

I currently have a script that reads my .csv files from a folder and spits out information such as peak postion, area etc., so maybe its easier to just tag it onto that, however it is using peakfit, not fitpeak. I have inserted below the section of the script which fits the peaks.

% Code for reading in files is above here!

% Now go through all the files in that folder and apply signal processing.

% Skip any files that generate and error and keep going.

for FileNum=1:NumFiles

try

datamatrix=load([DataDirectory '\' fn(FileNum,1:NameLength)]);

DataArray=[datamatrix(:,1) datamatrix(:,2)]; % Change to suit format of data

disp([num2str(FileNum) ': ' fn(FileNum,1:NameLength)])% Prints out the file number and name

disp(' Peak# Position Height Width Area')

% Curve fitting settings:

windowcenter=0; % Change this to fit other regions of the spectrum

windowwidth=0; % Change to zoom in or out to smaller or larger region.

NumPeaks=15; % Change this to the expected number of peaks.

Shape= 4; % 1-Gaussian, 2-Lorentzian, etc. Type "help peakfit" for list.

NumTrials=10; % Usually 1 to 10. Lower numbers faster but less stable.

startvector=0; % Difficult cases may require start vector, see help file.

BaselineMode=0; % Can be 0, 1, 2,or 3 (1=linear baseline subtraction)

[FitResults,GoodnessOfFit]=peakfit(DataArray,windowcenter,windowwidth,NumPeaks,Shape,1,NumTrials,startvector,BaselineMode);

disp(FitResults)

disp(' % fitting error R2')

disp(GoodnessOfFit)

drawnow

disp(' ')

% Add code here if you want to perform another set of

% operations on the same data file with different settings, i.e.,

% different data region, number of peaks, peak shape, etc.

catch me

disp([ num2str(FileNum) ': Error encounted with file: ' me.identifier])

disp(' ')

end

(I cannot remember who to credit with the above code, as I found this many months ago!)

Would it be best to just tag a FWHM code into this script, or write something completely separate?

Thanks in advance.

EDIT: Addition of example data.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Star Strider on 20 Jun 2022

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#answer_989490

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#answer_989490

Edited: Star Strider on 20 Jun 2022

It might be easier if we had one or two typical .csv files to experiment with.

The findpeaks WidthReference option is 'halfprom' by default, although it can be set to 'halfheight' to calculate the FWHM value. If baseline offset or an inconstant baseline is a problem, there are several ways to deal with that.

EDIT — (20 Jun 2022 at 14:12)

Do you want to detrend the baseline first? If so, that is relatively straightforward —

D = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1039090/example.csv');

[~,ixu] = unique(D(:,1));

x = D(ixu,1);

y = D(ixu,2);

figure

plot(x, y)

grid

How to calculate the FWHM from multiple spectra? (3)

B = [x([1 end]) ones(2,1)] \ y([1 end]);

ydt = [x ones(size(y))] * B;

ybc = y-ydt; % Baselilne Corrected

figure

plot(x, ybc)

grid

How to calculate the FWHM from multiple spectra? (4)

It would be difficult to fit Gaussian peaks to this because the peak contributions are not well-defined. Calculating the FWHM of the single peak however would be relatively straightforward.

[pkmax,ixp] = max(ybc);

ixv = find(diff(sign(ybc-(pkmax/2))));

for k = 1:numel(ixv)

idxrng = max(1,ixv(k)-1) : min(numel(x),ixv(k)+1);

xv(k) = interp1(ybc(idxrng), x(idxrng), pkmax/2);

yv(k) = interp1(x(idxrng), ybc(idxrng), xv(k));

end

figure

plot(x, ybc)

hold on

plot(xv, yv, '+r')

hold off

text(x(ixp),pkmax/2, sprintf('\\leftarrow FWHM = %.1f \\rightarrow', diff(xv)), 'Horiz','center', 'Vert','middle', 'FontSize',7)

How to calculate the FWHM from multiple spectra? (5)

Make appropriate changes to get the result you want.

.

8 Comments

Show 6 older commentsHide 6 older comments

Sam on 20 Jun 2022

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224475

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224475

So sorry, I knew I was forgetting something. I have added the example data in now.

Is it possible to use both findpeaks and peakfit simultaneously or will this cause issues?

Sam on 20 Jun 2022

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224600

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224600

Open in MATLAB Online

I have managed to get a fit from my data now using:

data = importdata('example.csv');

wavelengths = data(:, 1); % Wavelengths in column 1

magnitude = data(:, 2); % Signal magnitude in column 2

findpeaks(data(:,2),data(:,1),...

'MinPeakHeight',3,'MinPeakProminence',1,'Annotate','Extents'...

);

And from here, using the graph I can use the x values from the width line to calculate the FWHM

Sam on 20 Jun 2022

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224780

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224780

That is great, thank you so much @Star Strider!

Looking back at my data, I think it would be better to fit a single peak from the whole spectrum, rather than "crop" the spectrum to the single peak and then fit it.

If I was to load in the whole spectrum, how would I "trim" it, so there is a specific window to select the correct peak from?

Star Strider on 20 Jun 2022

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224875

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224875

Open in MATLAB Online

As always, my pleasure!

If I was to load in the whole spectrum, how would I "trim" it, so there is a specific window to select the correct peak from?

That depends on the data. It would be necessary to select a specific peak manually or programmatically first. To get the ‘valleys’ on either side, use findpeaks (or islocalmin with find) to get the valley coordinate indices.

That would go something like this —

x = 920 : 980; % Create Data

y = sinc((x-mean(x))/5); % Create Data

figure

plot(x, y)

grid

How to calculate the FWHM from multiple spectra? (10)

[pks,plocs] = findpeaks(y, 'MinPeakProminence',1); % Desired Peak & Index

[vys,vlocs] = findpeaks(-y, 'MinPeakProminence',0.1); % Valleys & Indices

[vlc,ixv] = mink(abs(vlocs-plocs),2); % Choose Two Closest Valleys To Desired Peak

vlcs = plocs + [-1 1].*vlc; % Calculate Indices

idxr = vlcs(1) : vlcs(2); % Initial Index Range

figure

plot(x(idxr), y(idxr)) % Plot Desired Peak Region

grid

How to calculate the FWHM from multiple spectra? (11)

This would be a bit more difficult with a wandering baseline, so that would likely need to be addressed first, if it was a problem.

After that, my code simply analyses the peak it is presented with.

Sam on 20 Jun 2022

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224920

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2224920

This is great, thank you!

There seems to be an issue when I run this, the following error comes ups:

Matrix dimensions must agree.

Error in FWHMTrim (line 13)

[vlc,ixv] = mink(abs(vlocs - plocs),2); % Choose Two Closest Valleys To Desired Peak

Star Strider on 20 Jun 2022

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2225075

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2225075

Open in MATLAB Online

As always, my pleasure!

I forgot that your data are column vectors. My data are row vectors, so transposing them to give a column vector requires:

vlcs = plocs + [-1; 1].*vlc; % Calculate Indices

for my code to work correctly. The mink (introduced in R2017b) call works correctly for the column vector.

This now appears to work correctly with column vector data (in R2022a) —

x = (920 : 980).'; % Create Data (Column)

y = sinc((x-mean(x))/5); % Create Data (Column)

figure

plot(x, y)

grid

How to calculate the FWHM from multiple spectra? (14)

[pks,plocs] = findpeaks(y, 'MinPeakProminence',1); % Desired Peak & Index

[vys,vlocs] = findpeaks(-y, 'MinPeakProminence',0.1); % Valleys & Indices

[vlc,ixv] = mink(abs(vlocs-plocs),2); % Choose Two Closest Valleys To Desired Peak

vlcs = plocs + [-1; 1].*vlc; % Calculate Indices

idxr = vlcs(1) : vlcs(2); % Initial Index Range

figure

plot(x(idxr), y(idxr)) % Plot Desired Peak Region

grid

How to calculate the FWHM from multiple spectra? (15)

My apologies for overlooking that detail.

.

Sam on 21 Jun 2022

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2225955

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2225955

No need to apologise! Thank you so much for your help!

Star Strider on 21 Jun 2022

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2226120

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1744025-how-to-calculate-the-fwhm-from-multiple-spectra#comment_2226120

As always, my pleasure!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

Signal ProcessingSignal Processing ToolboxMeasurements and Feature ExtractionDescriptive Statistics

Find more on Descriptive Statistics in Help Center and File Exchange

Tags

  • signal processing
  • fwhm

Products

  • MATLAB

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to calculate the FWHM from multiple spectra? (18)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to calculate the FWHM from multiple spectra? (2024)
Top Articles
Calin Technology Co. Ltd. kündigt die erste Amtszeit der Mitglieder des Risikomanagement-Komitees an
Regionalism on the Celtic Fringe: How a Peripheral Community Resists, Negotiates, and Accommodates Political and Economic Integration
Spectrum Gdvr-2007
No Hard Feelings (2023) Tickets & Showtimes
Joi Databas
80 For Brady Showtimes Near Marcus Point Cinema
라이키 유출
Gw2 Legendary Amulet
Rubfinder
Learn How to Use X (formerly Twitter) in 15 Minutes or Less
414-290-5379
Voyeuragency
Persona 4 Golden Taotie Fusion Calculator
Craigslist Pets Sac
Binghamton Ny Cars Craigslist
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Hijab Hookup Trendy
The most iconic acting lineages in cinema history
Teenleaks Discord
Amc Flight Schedule
Lcwc 911 Live Incident List Live Status
Loves Employee Pay Stub
Vrachtwagens in Nederland kopen - gebruikt en nieuw - TrucksNL
Jang Urdu Today
The best firm mattress 2024, approved by sleep experts
Daytonaskipthegames
Wbiw Weather Watchers
Azur Lane High Efficiency Combat Logistics Plan
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
Page 2383 – Christianity Today
Copper Pint Chaska
Best Restaurants Ventnor
Swimgs Yuzzle Wuzzle Yups Wits Sadie Plant Tune 3 Tabs Winnie The Pooh Halloween Bob The Builder Christmas Autumns Cow Dog Pig Tim Cook’s Birthday Buff Work It Out Wombats Pineview Playtime Chronicles Day Of The Dead The Alpha Baa Baa Twinkle
Elanco Rebates.com 2022
First Light Tomorrow Morning
Flixtor Nu Not Working
The Mad Merchant Wow
Solemn Behavior Antonym
Instafeet Login
دانلود سریال خاندان اژدها دیجی موویز
Silive Obituary
Arnesons Webcam
Fedex Passport Locations Near Me
Strange World Showtimes Near Century Stadium 25 And Xd
Pgecom
Nope 123Movies Full
Cara Corcione Obituary
Workday Latech Edu
Besoldungstabellen | Niedersächsisches Landesamt für Bezüge und Versorgung (NLBV)
Mike De Beer Twitter
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 5644

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.