Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

python - Align (overlap) two vectors that are slightly shifted

I am working on a software for spectral analyses. Data consists of a np.array with spectral intensities, and a "wavelength vector" (wlv), i.e. the X-axis, so to speak.

When combining data, not all data has exactly the same wavelength vector, but they might be shifted ever so slightly, say by 0.00002 units or so.

Here is some example data:
a (blue) is the "proper" dataset, while b (red) is slightly shifted:
a's wavelengths are

[0, 1, 2, 3, 4, 5, 6].  

b's wavelengths are

[-0.9, 0.1, 1.1, 2.1, 3.1, 4.1, 5.1]

Example

a_intensities = np.array([[2, 4.2, 5.2, 5, 4, 5, 2.8], [5, 5.2, 5.5, 5.4, 5.3, 5.3, 3.7]])
a_wlv = np.array([0, 1, 2, 3, 4, 5, 6])
b_intensities = np.array([[1, 1.1, 1.7, 1.9, 1.8, 1.9, 1.2], [0.1, 0.7, 0.8, 0.6, 0.5, 0.45, 0.31]])
b_wlv = np.array([-0.9, 0.1, 1.1, 2.1, 3.1, 4.1, 5.1])
plt.figure('Spectra')
plt.plot(a_wlv, a_intensities.T, 'bo-', color='blue')
plt.grid()
plt.plot(b_wlv, b_intensities.T, 'bo--', color='red')
plt.xlabel('Wavelength')
plt.ylabel('Intensity')
plt.show()

Example spectra:

Spectra images (updated)

I now want to "shift" b's wavelength vector so that it overlaps with a. The shift needs to be conducted in the direction where the necessary change occurs. Note that the wavelengths do not need to be identical (i.e. both be 0:7) but it is important that they overlap exactly. Rounding to three decimals works in some cases, but not always (since I don't want to lose too much data), so I would like to have a way of doing this without rounding.

Therefore, in above's example, I want to shift b so that the bins overlap with a:

Before shifting:

a: [     0,   1,   2,   3,   4,   5,   6].
b: [-0.9, 0.1, 1.1, 2.1, 3.1, 4.1, 5.1]

After shifting:

a: [      0,   1,   2,   3,   4,   5,   6].
b: [-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0]

How would you go about this? I am currently writing a rather lengthy function to do this, but I am afraid I am missing an obvious, much easier way here.

Edit: Some real-world data:

a_intensities = np.array([0.2184649, 0.2201522, 0.2186561, 0.2158286, 0.2134198, 0.2112872, 0.2141369, 0.2177338, 0.2169663, 0.2109826, 0.2084285, 0.2115282])
a_wlv = np.array([800.3474, 802.2759, 804.2045, 806.1331, 808.0616, 809.9902, 811.9187, 813.8472, 815.7758, 817.7043, 819.6329, 821.5615])
b_intensities = np.array([0.02411663, 0.02425605, 0.02463717, 0.02500274, 0.0251241, 0.02488419, 0.0243349, 0.02371608, 0.02327189, 0.023061, 0.023011, 0.02312733])
b_wlv = np.array([800.34744206, 802.27599007, 804.20453808, 806.1330861, 808.06163411, 809.99018212, 811.91873013, 813.84727814, 815.77582616, 817.70437417, 819.63292218, 821.56147019])

plt.plot(a_wlv, a_intensities, 'bo-', color='red')
plt.plot(b_wlv, b_intensities, 'bo-', color='green')
plt.grid()
plt.show()
question from:https://stackoverflow.com/questions/65879014/align-overlap-two-vectors-that-are-slightly-shifted

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...