Spectral or peak alignment

Hello: I have two spectra, Spectrum A and Spectrum B. Spectrum A is the reference spectrum and spectrum B is one I just collected. I'm trying to match spectrum B with the reference spectrum A. However, I realized that the peaks in B are a bit shifted.
My question is how do I correctly align B to the reference A using matlab?
How do I correct for the peak shifts in my spectrum so that they align correctly to the reference spectrum using matlab or any other program or algorithm?
Thank you.

2 Comments

'Pends on what kind of shift you're talking about...is it simply a phase shift of the whole spectrum or a shrinkage/expansion?
Attach at least figure; data would be better...
I just attached a figure. Thank you.

Sign in to comment.

 Accepted Answer

Kateryna Artyushkova
Kateryna Artyushkova on 19 Oct 2018
Maybe this will work: https://www.mathworks.com/matlabcentral/fileexchange/15392-shifts-set-of-spectra-to-a-new-maximum-position?s_tid=prof_contriblnk

More Answers (2)

Star Strider
Star Strider on 9 Jul 2017
One option is to use the Signal Processing Toolbox alignsignals (link) function. That is not its intended use, but it could work. (It is relatively recent. The documentation does not say when it was introduced.)

14 Comments

Cool - I didn't know about that function! Thanks for mentioning it.
My pleasure!
@Francis Kwofie —
The two spectra do not look ‘shifted’ in wavenumber, but varying slightly in amplitude, probably reflecting sample collection or preparation and instrument calibration.
I do not see a problem.
Curious Mind
Curious Mind on 9 Jul 2017
Edited: Curious Mind on 9 Jul 2017
@Star Strider:
The two spectrum were obtained on two different FTIR spectrometers. So what you are saying is absolutely true. But I hoped there was a way or an algorithm that can be implemented to bring spectrum B in alignment with spectrum A(reference spectrum obtained on a different instrument).
@Francis Kwofie —
I would just use your data as you have them, write off the differences as normal variations and noise, and leave it at that. This is what statistics (specifically statistical tests and confidence intervals) are for.
The best you can probably do is to run the same sample on both instruments, and create a calibration curve. Running different samples on different instruments and expecting the results to match exactly is probably not realistic.
@Star Strider-
The issue is that my lab does not have any control over how the reference spectrum was collected and we don't have access to that spectrometer. We prepared a search prefilter based on the reference spectra and we are trying to see if the prefilter will correctly place Spectrum B in the same cluster as the reference spectrum A. It does not seem to be working and we envisioned that the problem might be due to the shifts observed in the spectrum that we collected on our instrument. Do you think that the shifts we are observing could be the reason behind the unsuccessful placement of our spectrum in the same cluster as that of the reference spectrum? Thank you.
I honestly have no idea. This is far outside my areas of expertise.
My Answer was posted long before you posted your image, and I thought that ‘alignment’ referred only to the peaks position in reference to your independent variable. Thus my thought to use the alignsignals function.
The only approach I can think of (and I cannot guarantee that it is valid) is to develop some sort of nonlinear fit between the amplitudes of the two signals, if they share exactly the same values of the independent variable, and use it to equalise the spectra. The polyfit and polyval functions could be one way to do this. (Fit one spectrum to the other, using only the dependent variables of both data sets, not the independent variables. I would keep the polynomial order at 7 or less. Then use the same function to ‘correct’ other spectra you may want to analyse.)
Thank you. I will give it a try.
I work with some world class spectroscopists. I could ask them tomorrow. But first I'd like to know if all the peaks are shifted in the same direction. And if so, does alignsignals translate it so that the peaks align to your satisfaction or not.
@Image Analyst:
That will be appreciated. The shifts appears to be a nonlinear one. This is because as you try to align one peak to the a certain peak in the reference spectrum, other peaks which were originally well aligned begins to go out of phase and becomes misaligned. I think alignsignal is for two data that have different lengths. Alignsignal may add zeros to bring two data to the same length and other things. Because the shifts are not consistent, alignsignal is unable to perform the task I want it to.
I asked my best spectroscopist about your spectra, and he said it could be many things. He said by far the most likely reason is the chemistry of your sample. He said that differences in the chemistry between the two samples could shift the peaks in different directions and by different amounts, and that your spectra look perfectly reasonable with that in mind. The peaks will, or may, shift slightly due to the water content, the environment the sample is in, the species and concentrations of various chemicals in the sample, etc. He said the shifts are most likely not due to any problem with your spectrometer instrument, but rather to legitimate differences in the chemistry of the sample itself. He said that any kind of warping of one signal to match the other would be inadvisable Such manipulations might hide/mask/obscure any effect that is truly due to the actual chemistry, and advised you to NOT do that. He said that his best advice would for you to personally consult with an experienced spectroscopist.
What kind of spectroscopy of what sample?
In gamma spec, a slight energy calibration difference will shorten/lengthen on reference spectrum vis a vis another. An alignment there by a linear ratio vs channel will bring the two in line but it is linear by the gain difference. A truly nonlinear response wouldn't occur that way.
@Image Analyst:
I really appreciate the information. I think I'm gonna leave the data the way it is. Thank you so much.
@dpb Thank you.

Sign in to comment.

Image Analyst
Image Analyst on 9 Jul 2017
Look up xcorr in the help and see this entry "Find a Signal in a Measurement". It says "You receive some data and would like to know if it matches a longer stream you have measured. Cross-correlation allows you to make that determination, even when the data are corrupted by noise."
and then it gives an example. Try it.

Community Treasure Hunt

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

Start Hunting!