Skip to content

Instantly share code, notes, and snippets.

@cibomahto
Created March 23, 2022 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cibomahto/f1b3a0cefebfeb6d1b747230731c8679 to your computer and use it in GitHub Desktop.
Save cibomahto/f1b3a0cefebfeb6d1b747230731c8679 to your computer and use it in GitHub Desktop.
# Based on: https://github.com/uutzinger/C12880MA
import serial
import serial.tools.list_ports
import time
import matplotlib.pyplot as plt
import numpy as np
import scipy.signal as signal
def find_serial_device(vid=None,pid=None,product=None):
devices = []
for port in serial.tools.list_ports.comports():
# print(port.device,port.vid, port.pid, port.product)
if (vid != None) and (vid != port.vid):
continue
if (pid != None) and (pid != port.pid):
continue
if (product != None) and (product != port.product):
continue
devices.append(port.device)
return devices
class MicroSpec(object):
def __init__(self, port):
self._ser = serial.Serial(port,baudrate=115200)
def set_integration_time(self, seconds):
cmd = "SPEC.INTEG %0.6f\n" % seconds
self._ser.write(cmd.encode('utf8'))
def read(self):
self._ser.write(b"SPEC.READ?\n")
sdata = self._ser.readline()
sdata = np.array([int(p) for p in sdata.split(b",")])
self._ser.write(b"SPEC.TIMING?\n")
tdata = self._ser.readline()
tdata = np.array([int(p) for p in tdata.split(b",")])
return (sdata, tdata)
def read_many(self, integration_time, count):
spec.set_integration_time(integration_time)
stotal = None
for sample in range(0,count):
sdata, tdata = self.read()
if type(stotal) == type(None):
stotal = sdata
else:
stotal += sdata
return stotal/sample_count
cal_data = {
"A_0" : 3.062951791e2,
"B_1" : 2.720613040e0,
"B_2" : -1.302773396e-3,
"B_3" : -7.101037823e-6,
"B_4" : 8.279199710e-9,
"B_5" : 5.742926217e-12
}
def correct(pix, cal_data):
return (cal_data["A_0"]) \
+ (cal_data["B_1"]*pix) \
+ (cal_data["B_2"]*(pix**2)) \
+ (cal_data["B_3"]*(pix**3)) \
+ (cal_data["B_4"]*(pix**4)) \
+ (cal_data["B_5"]*(pix**5))
# frequency points
nm = [correct(i, cal_data) for i in range(1,289)]
spec = MicroSpec(find_serial_device(vid=5824, pid=1155)[0]) # Teensy 3.2
# integration_time = 0.000085 # read time, in seconds
# sample_count = 20 # number of samples to average
integration_time = 0.000085 # read time, in seconds
sample_count = 20 # number of samples to average
datas = {}
datas['dark'] = spec.read_many(integration_time, sample_count)
def normalize(a):
b=np.ndarray.sum(a,axis=0)
return a/b
integration_time = 0.00000085 # read time, in seconds
datas['uv'] = spec.read_many(integration_time, sample_count)
plt.rcParams['figure.figsize'] = [20, 10]
for label, spectrum in datas.items():
if label=='dark' or label=='warm' or label=='natural':
continue
d = normalize(spectrum-datas['dark'])
plt.plot(nm,d,label=label)
peaks, _ = signal.find_peaks(d, distance=20, height=.005)
np.diff(peaks)
v = np.array(nm)
plt.plot(v[peaks], d[peaks], "x")
print(label, "peaks(nm):", [int(i) for i in v[peaks]])
#plt.show()
#plt.plot(nm,(datas['cool']-datas['dark'])*.65+(datas['warm']-datas['dark'])*.50,label='cool*.65+warm*.5')
plt.title('Normalized Spectrum')
plt.xlabel('Wavelength(nm)')
plt.ylabel('%')
plt.legend()
#data['spectrums'].append(stotal/sample_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment