Last active
March 13, 2024 09:23
-
-
Save ZGainsforth/43b565a10a669afad64bd3317804b4c5 to your computer and use it in GitHub Desktop.
A quick way to plot some IR spectra extracted from a NanoIR stack.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from scipy.signal import medfilt, convolve | |
from scipy.signal.windows import gaussian | |
import os, sys | |
SpectraPath = os.path.join(".") | |
SpectraFiles = { | |
"Top Crystal" : os.path.join(SpectraPath, "Top Crystal.txt"), | |
"Right Crystal" : os.path.join(SpectraPath, "Right Crystal.txt"), | |
"Left Crystal" : os.path.join(SpectraPath, "Left Crystal.txt"), | |
"Bottom Dark" : os.path.join(SpectraPath, "Bottom Dark.txt"), | |
"Interstitial" : os.path.join(SpectraPath, "Interstitial.txt"), | |
"Contamination" : os.path.join(SpectraPath, "Contamination.txt"), | |
"Top Flicker" : os.path.join(SpectraPath, "Top Flicker.txt"), | |
# "Light Purple" : os.path.join(SpectraPath, "LightPurple.txt"), | |
} | |
# Some good colors: gold, goldenrod, royalblue, firebrick, forestgreen, limegreen, teal, darkturquoise, black | |
SpectraColors = { | |
"Top Crystal" : "gold", | |
"Top Flicker" : "forestgreen", | |
"Right Crystal" : "goldenrod", | |
"Left Crystal" : "yellow", | |
"Interstitial" : "forestgreen", | |
"Contamination" : "goldenrod", | |
"Bottom Dark" : "limegreen", | |
"Light Purple" : "teal", | |
} | |
Spectra = dict() | |
E = np.genfromtxt("Energies.csv", skip_header=1) | |
for Name, FileName in SpectraFiles.items(): | |
I = np.genfromtxt(FileName, skip_header=1) | |
S = np.stack((E,I[:,1]), axis=1) | |
Spectra[Name] = S | |
def NormMe(I): | |
I -= np.min(I) | |
I /= np.max(I) | |
return I | |
def SubtractPreEdge(S): | |
MaxInd = np.argmin(np.abs(S[:,0]-703.0)) | |
Pline = np.polyfit(S[:MaxInd,0], S[:MaxInd,1], 1) | |
Bkg = Pline[0]*S[:,0] + Pline[1] | |
S[:,1] -= Bkg | |
return S | |
def Preprocess(S): | |
S = S.copy() | |
# S = SubtractPreEdge(S) | |
k = gaussian(5, 2.0) # numpoints, sigma | |
S[:,1] = convolve(S[:,1], k, mode='same') | |
# S[:,1] = medfilt(S[:,1], 5) | |
S[:,1] = NormMe(S[:,1]) | |
return S | |
plt.figure(figsize=(8,12)) | |
Offset = 0 | |
for Name, S in Spectra.items(): | |
Sprime = Preprocess(S) | |
plt.plot(S[:,0], Sprime[:,1]+Offset, label=Name, color=SpectraColors[Name]) | |
np.savetxt(os.path.splitext(Name)[0] + '_raw.csv', S, header="cm-1, Intensity", delimiter=",") | |
np.savetxt(os.path.splitext(Name)[0] + '_processed.csv', Sprime, header="cm-1, Intensity", delimiter=",") | |
Offset -= 0.5 | |
plt.xlabel("cm$^{-1}$") | |
plt.ylabel("Absorption (arb)") | |
plt.xlim(917,1141) | |
# plt.legend() #bbox_to_anchor=(1,0.5)) | |
box = plt.gca().get_position() | |
plt.gca().set_position([box.x0, box.y0, box.width * 0.6, box.height]) | |
plt.legend(bbox_to_anchor=(1.1,0.6)) | |
# plt.tight_layout() | |
plt.savefig('Spectra.png', dpi=300) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment