Trying to combine two IR spectra where x is wavenumber (cm-1) and y is transmittance (0 to 1), having trouble.
6 views (last 30 days)
Show older comments
I am trying to combine two IR spectra from a data base, they are being treated as two components. I want to add them together and plot it and make it into a single spectrum. The problem with this is the number of rows in one set is not the same as the other. The other problem is that the x (wavenumber) values for the two spectra are not the same (spacing is different). I can plot them superimposed, but I want to add them as a linear combination.
0 Comments
Answers (2)
Voss
on 16 Apr 2024
Edited: Voss
on 16 Apr 2024
You'll can interpolate the two spectra onto a common set of wavenumbers. Then you can add the interpolated spectra together. Here's an example:
% some made-up data
x1 = linspace(650,700,6);
y1 = -0.25*sind(x1);
x2 = linspace(645,705,8);
y2 = 0.2*cosd(x2);
% plot the two curves
figure
plot(x1,y1,'.-b',x2,y2,'.-r')
% interpolate each y on a common set of x values,
% in this case the union of the two x vectors
x = union(x1,x2);
y1i = interp1(x1,y1,x);
y2i = interp1(x2,y2,x);
% add the interpolated values together
y = y1i+y2i;
% plot the interpolated values
hold on
plot(x,y1i,'bo',x,y2i,'ro')
% plot the sum
plot(x,y,'o-k')
% make a legend
legend({'y1','y2','y1i','y2i','y1i+y2i'},'Location','best','NumColumns',5)
% vertical lines showing how the x of each original curve
% maps to an x in the sum curve
xline(x,'--','Color',[0.7 0.7 0.7],'HandleVisibility','off');
0 Comments
Star Strider
on 16 Apr 2024
It would help to have the data.
The best way is probably to interpolate the longer series to the length of the shorter series. They do not seem to have different names, so I would do something like this, using the resample function —
short_series(:,1) = linspace(651, 900, 397).';
short_series(:,2) = sin(2*pi*(short_series(:,1)-short_series(1,1))*0.01);
long_series = linspace(633, 910, 707).';
long_series(:,2) = sin(2*pi*(long_series(:,1)-long_series(1,1))*0.01);
figure
plot(short_series(:,1), short_series(:,2))
hold on
plot(long_series(:,1), long_series(:,2))
hold off
grid
res2 = resample(long_series(:,2), size(short_series,1), size(long_series,1)).'; % Use 'resample'
figure
plot(short_series(:,1), short_series(:,2))
hold on
plot(short_series(:,1), res2)
hold off
grid
Resampling the longer series to the length of the shorter series, instead of the other way round, avoids creating data in the shorter series where none previusly existed.
.
0 Comments
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!