In [110]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',',dtype='int')
rawdata_microseconds = rawdata[:,0] / 1000000
rawdata_zacurate_output = rawdata[:,1]
rawdata_zacurate_sensor = rawdata[:,2]
rawdata_fab_sensor = rawdata[:,3]

fig, ax = plt.subplots(figsize=(16,9), dpi=60)
ax.plot(rawdata_microseconds, rawdata_zacurate_output)
ax.set_xlabel('seconds') 
ax.set_ylabel('SpO2 (%)') 
Out[110]:
Text(0,0.5,'SpO2 (%)')
In [122]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',',dtype='int')
rawdata_microseconds = rawdata[:,0] / 1000000
rawdata_zacurate_output = rawdata[:,1]
rawdata_zacurate_sensor = rawdata[:,2]
rawdata_fab_sensor = rawdata[:,3]

fig, ax = plt.subplots(figsize=(16,9), dpi=60)
ax.plot(rawdata_microseconds, rawdata_zacurate_sensor, marker='o', linestyle='none', markersize=0.2, label='zacurate')
ax.plot(rawdata_microseconds, rawdata_fab_sensor, marker='o', linestyle='none', markersize=0.2, label='fab')
ax.set_xlabel('seconds')  # Add an x-label to the axes.
ax.set_ylabel('ADC counts')  # Add a y-label to the axes.
ax.legend()
Out[122]:
<matplotlib.legend.Legend at 0x7f4696a382e8>
In [120]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',',dtype='int')
rawdata_microseconds = rawdata[80000:80500,0] / 1000000
rawdata_zacurate_output = rawdata[80000:80500,1]
rawdata_zacurate_sensor = rawdata[80000:80500,2]
rawdata_fab_sensor = rawdata[80000:80500,3]

fig, ax = plt.subplots(figsize=(16,9), dpi=60)
ax.plot(rawdata_microseconds, rawdata_zacurate_sensor, marker='o', markersize=3, label='zacurate')
ax.plot(rawdata_microseconds, rawdata_fab_sensor, marker='o', markersize=3, label='fab')
ax.set_xlabel('seconds')  # Add an x-label to the axes.
ax.set_ylabel('ADC counts')  # Add a y-label to the axes.
ax.legend()
Out[120]:
<matplotlib.legend.Legend at 0x7f4694dc2f28>
In [2]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',',dtype='int')
rawdata_microseconds = rawdata[80030:80150,0] / 1000000
rawdata_zacurate_output = rawdata[80030:80150,1]
rawdata_zacurate_sensor = rawdata[80030:80150,2]
rawdata_fab_sensor = rawdata[80030:80150,3]

fig, ax = plt.subplots(figsize=(16,9), dpi=60)
ax.plot(rawdata_microseconds, rawdata_zacurate_sensor, marker='o', markersize=5, label='zacurate')
ax.plot(rawdata_microseconds, rawdata_fab_sensor, marker='o', markersize=5, label='fab')
ax.set_xlabel('seconds')  # Add an x-label to the axes.
ax.set_ylabel('ADC counts')  # Add a y-label to the axes.
ax.legend()
Out[2]:
<matplotlib.legend.Legend at 0x7fa7bcf52630>
In [2]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',')

threshold = 0.1 #change percentage must be below this value to register a stable data point
prev_zacurate_sensor = 1 #previous sensor value for threshold calculation (starts at 1 to avoid divide-by-zero error)
counter = 0 #for averaging and checking for background
background_mintime = 5 #consecutive stable readings needed to indicate a background value
accumulator = np.zeros([2,1]) #for averaging
cleandata_zacurate_sensor_pulse_0 = np.zeros([2,0]) #output np.array for first pulse
cleandata_zacurate_sensor_pulse_1 = np.zeros([2,0]) #output np.array for second pulse
cleandata_zacurate_sensor_background = np.zeros([2,0]) #output np.array for background data 
bucket = -1 #-1 for start (unknown bucket), 0 for pulse_0, 1 for pulse_1, 2 for background
for x in rawdata:
    if abs((x[2] - prev_zacurate_sensor) / prev_zacurate_sensor) > threshold:
        if counter > 0: #checks for repeated over-threshold changes
            temp = np.array([accumulator[0] / counter, accumulator[1] / counter])
            if counter > background_mintime:    
                cleandata_zacurate_sensor_background = np.append(cleandata_zacurate_sensor_background,temp,axis=1)
                bucket = 2
            elif bucket == 2:
                cleandata_zacurate_sensor_pulse_0 = np.append(cleandata_zacurate_sensor_pulse_0,temp,axis=1)
                bucket = 0
            elif bucket == 0: # ignore background pulse between signal pulses
                bucket = 0.5
            elif bucket == 0.5:
                cleandata_zacurate_sensor_pulse_1 = np.append(cleandata_zacurate_sensor_pulse_1,temp,axis=1)
                bucket = 1
            counter = 0
            accumulator = np.zeros([2,1])
    else:
        counter += 1
        accumulator[0] += x[0] / 1000000
        accumulator[1] += x[2]
    prev_zacurate_sensor = x[2]
    
print(cleandata_zacurate_sensor_background.shape)
    
fig, ax = plt.subplots(figsize=(16,9), dpi=60)
ax.plot(cleandata_zacurate_sensor_pulse_0[0,:], cleandata_zacurate_sensor_pulse_0[1,:], label='pulse_0')
ax.plot(cleandata_zacurate_sensor_pulse_1[0,:], cleandata_zacurate_sensor_pulse_1[1,:], label='pulse_1')
ax.plot(cleandata_zacurate_sensor_background[0,:], cleandata_zacurate_sensor_background[1,:], label='background')
ax.set_xlabel('seconds')  # Add an x-label to the axes.
ax.set_ylabel('ADC counts')  # Add a y-label to the axes.
ax.set_title('Zacurate sensor')
ax.legend()
(2, 5000)
Out[2]:
<matplotlib.legend.Legend at 0x7f6d8190cac8>
In [141]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',')

threshold = 0.1 #change percentage must be below this value to register a stable data point
prev_zacurate_sensor = 1 #previous sensor value for threshold calculation (starts at 1 to avoid divide-by-zero error)
counter = 0 #for averaging and checking for background
background_mintime = 5 #consecutive stable readings needed to indicate a background value
accumulator = np.zeros([2,1]) #for averaging
cleandata_zacurate_sensor_pulse_0 = np.zeros([2,0]) #output np.array for first pulse
cleandata_zacurate_sensor_pulse_1 = np.zeros([2,0]) #output np.array for second pulse
cleandata_zacurate_sensor_background = np.zeros([2,0]) #output np.array for background data 
bucket = -1 #-1 for start (unknown bucket), 0 for pulse_0, 1 for pulse_1, 2 for background
for x in rawdata:
    if abs((x[2] - prev_zacurate_sensor) / prev_zacurate_sensor) > threshold:
        if counter > 0: #checks for repeated over-threshold changes
            temp = np.array([accumulator[0] / counter, accumulator[1] / counter])
            if counter > background_mintime:    
                cleandata_zacurate_sensor_background = np.append(cleandata_zacurate_sensor_background,temp,axis=1)
                bucket = 2
            elif bucket == 2:
                cleandata_zacurate_sensor_pulse_0 = np.append(cleandata_zacurate_sensor_pulse_0,temp,axis=1)
                bucket = 0
            elif bucket == 0: # ignore background pulse between signal pulses
                bucket = 0.5
            elif bucket == 0.5:
                cleandata_zacurate_sensor_pulse_1 = np.append(cleandata_zacurate_sensor_pulse_1,temp,axis=1)
                bucket = 1
            counter = 0
            accumulator = np.zeros([2,1])
    else:
        counter += 1
        accumulator[0] += x[0] / 1000000
        accumulator[1] += x[2]
    prev_zacurate_sensor = x[2]
    
print(cleandata_zacurate_sensor_background.shape)
    
fig, ax = plt.subplots(figsize=(16,9), dpi=60)
ax.plot(cleandata_zacurate_sensor_pulse_0[0,:1000], cleandata_zacurate_sensor_pulse_0[1,:1000], label='pulse_0')
ax.plot(cleandata_zacurate_sensor_pulse_1[0,:1000], cleandata_zacurate_sensor_pulse_1[1,:1000], label='pulse_1')
ax.plot(cleandata_zacurate_sensor_background[0,:1000], cleandata_zacurate_sensor_background[1,:1000], label='background')
ax.set_xlabel('seconds')  # Add an x-label to the axes.
ax.set_ylabel('ADC counts')  # Add a y-label to the axes.
ax.set_title('Zacurate sensor')
ax.legend()
(2, 5000)
Out[141]:
<matplotlib.legend.Legend at 0x7fa7b7f1e438>
In [422]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',')

threshold = 0.1 #change percentage must be below this value to register a stable data point
prev_zacurate_sensor = 1 #previous sensor value for threshold calculation (starts at 1 to avoid divide-by-zero error)
counter = 0 #for averaging and checking for background
background_mintime = 5 #consecutive stable readings needed to indicate a background value
accumulator = np.zeros([2,1]) #for averaging
cleandata_zacurate_sensor_pulse_0 = np.zeros([2,0]) #output np.array for first pulse
cleandata_zacurate_sensor_pulse_1 = np.zeros([2,0]) #output np.array for second pulse
cleandata_zacurate_sensor_background = np.zeros([2,0]) #output np.array for background data 
bucket = -1 #-1 for start (unknown bucket), 0 for pulse_0, 1 for pulse_1, 2 for background
for x in rawdata:
    if abs((x[2] - prev_zacurate_sensor) / prev_zacurate_sensor) > threshold:
        if counter > 0: #checks for repeated over-threshold changes
            temp = np.array([accumulator[0] / counter, accumulator[1] / counter])
            if counter > background_mintime:    
                cleandata_zacurate_sensor_background = np.append(cleandata_zacurate_sensor_background,temp,axis=1)
                bucket = 2
            elif bucket == 2:
                cleandata_zacurate_sensor_pulse_0 = np.append(cleandata_zacurate_sensor_pulse_0,temp,axis=1)
                bucket = 0
            elif bucket == 0: # ignore background pulse between signal pulses
                bucket = 0.5
            elif bucket == 0.5:
                cleandata_zacurate_sensor_pulse_1 = np.append(cleandata_zacurate_sensor_pulse_1,temp,axis=1)
                bucket = 1
            counter = 0
            accumulator = np.zeros([2,1])
    else:
        counter += 1
        accumulator[0] += x[0] / 1000000
        accumulator[1] += x[2]
    prev_zacurate_sensor = x[2]
    
# combine data sets and subtract background levels
normalized_zacurate = np.zeros([6,0])
counter2 = 0
for y in cleandata_zacurate_sensor_background.T:
    counter2 += 1
    if counter2 < cleandata_zacurate_sensor_pulse_0.shape[1] and counter2 < cleandata_zacurate_sensor_pulse_1.shape[1]:
        temp2 = np.array([[y[0]],[cleandata_zacurate_sensor_pulse_0[1,counter2] - y[1]],[cleandata_zacurate_sensor_pulse_1[1,counter2] - y[1]],[0],[0],[0]])
        normalized_zacurate = np.append(normalized_zacurate, temp2, axis=1)
        
# determine rolling minimum value and divide out
counter3 = 0 # index
averaging_period = 50

for z in normalized_zacurate.T:
    counter3 += 1
    if (counter3 < averaging_period):
        normalized_zacurate[3,counter3] = normalized_zacurate[1,counter3] / np.amin(normalized_zacurate[1,0:counter3])
        normalized_zacurate[4,counter3] = normalized_zacurate[2,counter3] / np.amin(normalized_zacurate[2,0:counter3])
        normalized_zacurate[5,counter3] = normalized_zacurate[3,counter3] / normalized_zacurate[4,counter3]
    elif (counter3 < normalized_zacurate.shape[1]):
        normalized_zacurate[3,counter3] = normalized_zacurate[1,counter3] / np.amin(normalized_zacurate[1,(counter3 - averaging_period):counter3])
        normalized_zacurate[4,counter3] = normalized_zacurate[2,counter3] / np.amin(normalized_zacurate[2,(counter3 - averaging_period):counter3])
        normalized_zacurate[5,counter3] = normalized_zacurate[3,counter3] / normalized_zacurate[4,counter3]

normalized_zacurate[3,0] = 1
normalized_zacurate[4,0] = 1
normalized_zacurate[5,0] = 1
    
fig, ax = plt.subplots(figsize=(16,9), dpi=60)
ax.plot(normalized_zacurate[0,:1000], normalized_zacurate[3,:1000], label='pulse_0')
ax.plot(normalized_zacurate[0,:1000], normalized_zacurate[4,:1000], label='pulse_1')
ax.plot(normalized_zacurate[0,:1000], normalized_zacurate[5,:1000], label='R')
ax.set_xlabel('seconds')  # Add an x-label to the axes.
ax.set_ylabel('normalized value')  # Add a y-label to the axes.
ax.set_title('Zacurate sensor')
ax.legend()
Out[422]:
<matplotlib.legend.Legend at 0x7fa7b48c6198>
In [450]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',')

threshold = 0.1 #change percentage must be below this value to register a stable data point
prev_zacurate_sensor = 1 #previous sensor value for threshold calculation (starts at 1 to avoid divide-by-zero error)
counter = 0 #for averaging and checking for background
background_mintime = 5 #consecutive stable readings needed to indicate a background value
accumulator = np.zeros([2,1]) #for averaging
cleandata_zacurate_sensor_pulse_0 = np.zeros([2,0]) #output np.array for first pulse
cleandata_zacurate_sensor_pulse_1 = np.zeros([2,0]) #output np.array for second pulse
cleandata_zacurate_sensor_background = np.zeros([2,0]) #output np.array for background data 
bucket = -1 #-1 for start (unknown bucket), 0 for pulse_0, 1 for pulse_1, 2 for background
for x in rawdata:
    if abs((x[2] - prev_zacurate_sensor) / prev_zacurate_sensor) > threshold:
        if counter > 0: #checks for repeated over-threshold changes
            temp = np.array([accumulator[0] / counter, accumulator[1] / counter])
            if counter > background_mintime:    
                cleandata_zacurate_sensor_background = np.append(cleandata_zacurate_sensor_background,temp,axis=1)
                bucket = 2
            elif bucket == 2:
                cleandata_zacurate_sensor_pulse_0 = np.append(cleandata_zacurate_sensor_pulse_0,temp,axis=1)
                bucket = 0
            elif bucket == 0: # ignore background pulse between signal pulses
                bucket = 0.5
            elif bucket == 0.5:
                cleandata_zacurate_sensor_pulse_1 = np.append(cleandata_zacurate_sensor_pulse_1,temp,axis=1)
                bucket = 1
            counter = 0
            accumulator = np.zeros([2,1])
    else:
        counter += 1
        accumulator[0] += x[0] / 1000000
        accumulator[1] += x[2]
    prev_zacurate_sensor = x[2]
    
# combine data sets and subtract background levels
normalized_zacurate = np.zeros([6,0])
counter2 = 0
for y in cleandata_zacurate_sensor_background.T:
    counter2 += 1
    if counter2 < cleandata_zacurate_sensor_pulse_0.shape[1] and counter2 < cleandata_zacurate_sensor_pulse_1.shape[1]:
        temp2 = np.array([[y[0]],[cleandata_zacurate_sensor_pulse_0[1,counter2] - y[1]],[cleandata_zacurate_sensor_pulse_1[1,counter2] - y[1]],[0],[0],[0]])
        normalized_zacurate = np.append(normalized_zacurate, temp2, axis=1)
        
# determine rolling minimum value and divide out
counter3 = 0 # index
averaging_period = 150

for z in normalized_zacurate.T:
    counter3 += 1
    if (counter3 < averaging_period):
        normalized_zacurate[3,counter3] = normalized_zacurate[1,counter3] / np.amin(normalized_zacurate[1,0:counter3])
        normalized_zacurate[4,counter3] = normalized_zacurate[2,counter3] / np.amin(normalized_zacurate[2,0:counter3])
        if (counter3 > 1):
            normalized_zacurate[5,counter3] = np.amax(normalized_zacurate[3,0:counter3]) / np.amax(normalized_zacurate[4,0:counter3])
    elif (counter3 < normalized_zacurate.shape[1]):
        normalized_zacurate[3,counter3] = normalized_zacurate[1,counter3] / np.amin(normalized_zacurate[1,(counter3 - averaging_period):counter3])
        normalized_zacurate[4,counter3] = normalized_zacurate[2,counter3] / np.amin(normalized_zacurate[2,(counter3 - averaging_period):counter3])
        normalized_zacurate[5,counter3] = np.amax(normalized_zacurate[3,counter3 - averaging_period:counter3]) / np.amax(normalized_zacurate[4,counter3 - averaging_period:counter3])

normalized_zacurate[3,0] = 1
normalized_zacurate[4,0] = 1
normalized_zacurate[5,0] = 1
normalized_zacurate[5,1] = 1

fig, ax = plt.subplots(figsize=(16,9), dpi=60)
#ax.plot(normalized_zacurate[0,:], normalized_zacurate[3,:], label='pulse_0')
#ax.plot(normalized_zacurate[0,:], normalized_zacurate[4,:], label='pulse_1')
ax.plot(normalized_zacurate[0,:], normalized_zacurate[5,:], label='R')
ax.set_xlabel('seconds')  # Add an x-label to the axes.
ax.set_ylabel('normalized value')  # Add a y-label to the axes.
ax.set_title('Zacurate sensor')
ax.legend()
Out[450]:
<matplotlib.legend.Legend at 0x7fa7a5fa2ac8>
In [471]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',')

threshold = 0.1 #change percentage must be below this value to register a stable data point
prev_zacurate_sensor = 1 #previous sensor value for threshold calculation (starts at 1 to avoid divide-by-zero error)
counter = 0 #for averaging and checking for background
background_mintime = 5 #consecutive stable readings needed to indicate a background value
accumulator = np.zeros([2,1]) #for averaging
cleandata_zacurate_sensor_pulse_0 = np.zeros([2,0]) #output np.array for first pulse
cleandata_zacurate_sensor_pulse_1 = np.zeros([2,0]) #output np.array for second pulse
cleandata_zacurate_sensor_background = np.zeros([2,0]) #output np.array for background data 
bucket = -1 #-1 for start (unknown bucket), 0 for pulse_0, 1 for pulse_1, 2 for background
for x in rawdata:
    if abs((x[2] - prev_zacurate_sensor) / prev_zacurate_sensor) > threshold:
        if counter > 0: #checks for repeated over-threshold changes
            temp = np.array([accumulator[0] / counter, accumulator[1] / counter])
            if counter > background_mintime:    
                cleandata_zacurate_sensor_background = np.append(cleandata_zacurate_sensor_background,temp,axis=1)
                bucket = 2
            elif bucket == 2:
                cleandata_zacurate_sensor_pulse_0 = np.append(cleandata_zacurate_sensor_pulse_0,temp,axis=1)
                bucket = 0
            elif bucket == 0: # ignore background pulse between signal pulses
                bucket = 0.5
            elif bucket == 0.5:
                cleandata_zacurate_sensor_pulse_1 = np.append(cleandata_zacurate_sensor_pulse_1,temp,axis=1)
                bucket = 1
            counter = 0
            accumulator = np.zeros([2,1])
    else:
        counter += 1
        accumulator[0] += x[0] / 1000000
        accumulator[1] += x[2]
    prev_zacurate_sensor = x[2]
    
# combine data sets and subtract background levels
normalized_zacurate = np.zeros([6,0])
counter2 = 0
for y in cleandata_zacurate_sensor_background.T:
    counter2 += 1
    if counter2 < cleandata_zacurate_sensor_pulse_0.shape[1] and counter2 < cleandata_zacurate_sensor_pulse_1.shape[1]:
        temp2 = np.array([[y[0]],[cleandata_zacurate_sensor_pulse_0[1,counter2] - y[1]],[cleandata_zacurate_sensor_pulse_1[1,counter2] - y[1]],[0],[0],[0]])
        normalized_zacurate = np.append(normalized_zacurate, temp2, axis=1)
        
# determine rolling minimum value and divide out
counter3 = 0 # index
averaging_period = 50

for z in normalized_zacurate.T:
    counter3 += 1
    if counter3 > averaging_period and counter3 + averaging_period < normalized_zacurate.shape[1]:
        normalized_zacurate[3,counter3] = np.amin(normalized_zacurate[1,counter3-averaging_period:counter3+averaging_period])
        normalized_zacurate[4,counter3] = np.amax(normalized_zacurate[1,counter3-averaging_period:counter3+averaging_period])
        
print(counter3)
print(counter4)
plot_min = averaging_period + 1
plot_max = normalized_zacurate.shape[1]-averaging_period
    
fig, ax = plt.subplots(figsize=(16,9), dpi=60)
ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[1,plot_min:plot_max], label='pulse_0')
ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[3,plot_min:plot_max], label='pulse_0_min')
ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[4,plot_min:plot_max], label='pulse_0_max')
ax.set_xlabel('seconds')  # Add an x-label to the axes.
ax.set_ylabel('normalized value')  # Add a y-label to the axes.
ax.set_title('Zacurate sensor')
ax.legend()
4998
4897
Out[471]:
<matplotlib.legend.Legend at 0x7fa7ac351198>
In [540]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',')
rawdata_seconds = rawdata[:,0] / 1000000
rawdata_zacurate_output = rawdata[:,1]

threshold = 0.1 #change percentage must be below this value to register a stable data point
prev_zacurate_sensor = 1 #previous sensor value for threshold calculation (starts at 1 to avoid divide-by-zero error)
counter = 0 #for averaging and checking for background
background_mintime = 5 #consecutive stable readings needed to indicate a background value
accumulator = np.zeros([2,1]) #for averaging
cleandata_zacurate_sensor_pulse_0 = np.zeros([2,0]) #output np.array for first pulse
cleandata_zacurate_sensor_pulse_1 = np.zeros([2,0]) #output np.array for second pulse
cleandata_zacurate_sensor_background = np.zeros([2,0]) #output np.array for background data 
bucket = -1 #-1 for start (unknown bucket), 0 for pulse_0, 1 for pulse_1, 2 for background
for x in rawdata:
    if abs((x[2] - prev_zacurate_sensor) / prev_zacurate_sensor) > threshold:
        if counter > 0: #checks for repeated over-threshold changes
            temp = np.array([accumulator[0] / counter, accumulator[1] / counter])
            if counter > background_mintime:    
                cleandata_zacurate_sensor_background = np.append(cleandata_zacurate_sensor_background,temp,axis=1)
                bucket = 2
            elif bucket == 2:
                cleandata_zacurate_sensor_pulse_0 = np.append(cleandata_zacurate_sensor_pulse_0,temp,axis=1)
                bucket = 0
            elif bucket == 0: # ignore background pulse between signal pulses
                bucket = 0.5
            elif bucket == 0.5:
                cleandata_zacurate_sensor_pulse_1 = np.append(cleandata_zacurate_sensor_pulse_1,temp,axis=1)
                bucket = 1
            counter = 0
            accumulator = np.zeros([2,1])
    else:
        counter += 1
        accumulator[0] += x[0] / 1000000
        accumulator[1] += x[2]
    prev_zacurate_sensor = x[2]
    
# combine data sets and subtract background levels
normalized_zacurate = np.zeros([7,0])
counter2 = 0
for y in cleandata_zacurate_sensor_background.T:
    counter2 += 1
    if counter2 < cleandata_zacurate_sensor_pulse_0.shape[1] and counter2 < cleandata_zacurate_sensor_pulse_1.shape[1]:
        temp2 = np.array([[y[0]],[cleandata_zacurate_sensor_pulse_0[1,counter2] - y[1]],[cleandata_zacurate_sensor_pulse_1[1,counter2] - y[1]],[0],[0],[0],[0]])
        normalized_zacurate = np.append(normalized_zacurate, temp2, axis=1)
        
# determine rolling minimum value and divide out
counter3 = 0 # index
averaging_period = 70
filtered_zacurate = np.zeros([7,0])

for z in normalized_zacurate.T:
    counter3 += 1
    if counter3 > averaging_period and counter3 + averaging_period < normalized_zacurate.shape[1]:
        normalized_zacurate[3,counter3] = np.amin(normalized_zacurate[1,counter3-averaging_period:counter3+averaging_period])
        normalized_zacurate[4,counter3] = np.amax(normalized_zacurate[1,counter3-averaging_period:counter3+averaging_period])
        normalized_zacurate[5,counter3] = np.amin(normalized_zacurate[2,counter3-averaging_period:counter3+averaging_period])
        normalized_zacurate[6,counter3] = np.amax(normalized_zacurate[2,counter3-averaging_period:counter3+averaging_period])    
        
        if normalized_zacurate[1,counter3] == np.amax(normalized_zacurate[1,counter3-averaging_period:counter3+averaging_period]):
            temp3 = np.array([[z[0]],[normalized_zacurate[4,counter3]],[normalized_zacurate[3,counter3]],[normalized_zacurate[4,counter3] / normalized_zacurate[3,counter3]],[normalized_zacurate[6,counter3]],[normalized_zacurate[5,counter3]],[normalized_zacurate[6,counter3] / normalized_zacurate[5,counter3]]])
            filtered_zacurate = np.append(filtered_zacurate, temp3, axis=1)
        
plot_min = averaging_period + 1
plot_max = normalized_zacurate.shape[1]-averaging_period
    
fig, ax1 = plt.subplots(figsize=(16,9), dpi=58)
#ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[1,plot_min:plot_max], label='pulse_0')
#ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[3,plot_min:plot_max], label='pulse_0_min')
#ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[4,plot_min:plot_max], label='pulse_0_max')
#ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[2,plot_min:plot_max], label='pulse_1')
#ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[5,plot_min:plot_max], label='pulse_1_min')
#ax.plot(normalized_zacurate[0,plot_min:plot_max], normalized_zacurate[6,plot_min:plot_max], label='pulse_1_max')
ax1.plot(filtered_zacurate[0,:],filtered_zacurate[6,:] / filtered_zacurate[3,:], label='R')
ax2 = ax1.twinx()
ax2.plot(rawdata_seconds, rawdata_zacurate_output, color='orange', label='SpO2')
ax1.set_xlabel('seconds')  # Add an x-label to the axes.
ax1.set_ylabel('R')  # Add a y-label to the axes.
ax2.set_ylabel('SpO2')  # Add a y-label to the axes.
ax1.set_title('Zacurate sensor')
ax1.legend()
Out[540]:
<matplotlib.legend.Legend at 0x7fa7a3199940>
In [544]:
import matplotlib.pyplot as plt
import numpy as np

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',')
rawdata_seconds = rawdata[:,0] / 1000000
rawdata_fab_output = rawdata[:,1]

threshold = 0.1 #change percentage must be below this value to register a stable data point
prev_fab_sensor = 1 #previous sensor value for threshold calculation (starts at 1 to avoid divide-by-zero error)
counter = 0 #for averaging and checking for background
background_mintime = 5 #consecutive stable readings needed to indicate a background value
accumulator = np.zeros([2,1]) #for averaging
cleandata_fab_sensor_pulse_0 = np.zeros([2,0]) #output np.array for first pulse
cleandata_fab_sensor_pulse_1 = np.zeros([2,0]) #output np.array for second pulse
cleandata_fab_sensor_background = np.zeros([2,0]) #output np.array for background data 
bucket = -1 #-1 for start (unknown bucket), 0 for pulse_0, 1 for pulse_1, 2 for background
for x in rawdata:
    if abs((x[3] - prev_fab_sensor) / prev_fab_sensor) > threshold:
        if counter > 0: #checks for repeated over-threshold changes
            temp = np.array([accumulator[0] / counter, accumulator[1] / counter])
            if counter > background_mintime:    
                cleandata_fab_sensor_background = np.append(cleandata_fab_sensor_background,temp,axis=1)
                bucket = 2
            elif bucket == 2:
                cleandata_fab_sensor_pulse_0 = np.append(cleandata_fab_sensor_pulse_0,temp,axis=1)
                bucket = 0
            elif bucket == 0: # ignore background pulse between signal pulses
                bucket = 0.5
            elif bucket == 0.5:
                cleandata_fab_sensor_pulse_1 = np.append(cleandata_fab_sensor_pulse_1,temp,axis=1)
                bucket = 1
            counter = 0
            accumulator = np.zeros([2,1])
    else:
        counter += 1
        accumulator[0] += x[0] / 1000000
        accumulator[1] += x[3]
    prev_fab_sensor = x[3]
    
# combine data sets and subtract background levels
normalized_fab = np.zeros([7,0])
counter2 = 0
for y in cleandata_fab_sensor_background.T:
    counter2 += 1
    if counter2 < cleandata_fab_sensor_pulse_0.shape[1] and counter2 < cleandata_fab_sensor_pulse_1.shape[1]:
        temp2 = np.array([[y[0]],[cleandata_fab_sensor_pulse_0[1,counter2] - y[1]],[cleandata_fab_sensor_pulse_1[1,counter2] - y[1]],[0],[0],[0],[0]])
        normalized_fab = np.append(normalized_fab, temp2, axis=1)
        
# determine rolling minimum value and divide out
counter3 = 0 # index
averaging_period = 70
filtered_fab = np.zeros([7,0])

for z in normalized_fab.T:
    counter3 += 1
    if counter3 > averaging_period and counter3 + averaging_period < normalized_fab.shape[1]:
        normalized_fab[3,counter3] = np.amin(normalized_fab[1,counter3-averaging_period:counter3+averaging_period])
        normalized_fab[4,counter3] = np.amax(normalized_fab[1,counter3-averaging_period:counter3+averaging_period])
        normalized_fab[5,counter3] = np.amin(normalized_fab[2,counter3-averaging_period:counter3+averaging_period])
        normalized_fab[6,counter3] = np.amax(normalized_fab[2,counter3-averaging_period:counter3+averaging_period])    
        
        if normalized_fab[1,counter3] == np.amax(normalized_fab[1,counter3-averaging_period:counter3+averaging_period]):
            temp3 = np.array([[z[0]],[normalized_fab[4,counter3]],[normalized_fab[3,counter3]],[normalized_fab[4,counter3] / normalized_fab[3,counter3]],[normalized_fab[6,counter3]],[normalized_fab[5,counter3]],[normalized_fab[6,counter3] / normalized_fab[5,counter3]]])
            filtered_fab = np.append(filtered_fab, temp3, axis=1)
        
plot_min = averaging_period + 1
plot_max = normalized_fab.shape[1]-averaging_period
    
fig, ax1 = plt.subplots(figsize=(16,9), dpi=58)
#ax.plot(normalized_fab[0,plot_min:plot_max], normalized_fab[1,plot_min:plot_max], label='pulse_0')
#ax.plot(normalized_fab[0,plot_min:plot_max], normalized_fab[3,plot_min:plot_max], label='pulse_0_min')
#ax.plot(normalized_fab[0,plot_min:plot_max], normalized_fab[4,plot_min:plot_max], label='pulse_0_max')
#ax.plot(normalized_fab[0,plot_min:plot_max], normalized_fab[2,plot_min:plot_max], label='pulse_1')
#ax.plot(normalized_fab[0,plot_min:plot_max], normalized_fab[5,plot_min:plot_max], label='pulse_1_min')
#ax.plot(normalized_fab[0,plot_min:plot_max], normalized_fab[6,plot_min:plot_max], label='pulse_1_max')
ax1.plot(filtered_fab[0,:],filtered_fab[3,:] / filtered_fab[6,:], label='R')
ax2 = ax1.twinx()
ax2.plot(rawdata_seconds, rawdata_fab_output, color='orange', label='SpO2')
ax1.set_xlabel('seconds')  # Add an x-label to the axes.
ax1.set_ylabel('R')  # Add a y-label to the axes.
ax2.set_ylabel('SpO2')  # Add a y-label to the axes.
ax1.set_title('Fab sensor')
ax1.legend()
Out[544]:
<matplotlib.legend.Legend at 0x7fa7a1ecc128>
In [40]:
import matplotlib.pyplot as plt
import numpy as np
from scipy import fftpack

rawdata = np.genfromtxt('../data/log_holdingbreath.csv', delimiter=',')

threshold = 0.1 #change percentage must be below this value to register a stable data point
prev_zacurate_sensor = 1 #previous sensor value for threshold calculation (starts at 1 to avoid divide-by-zero error)
counter = 0 #for averaging and checking for background
background_mintime = 5 #consecutive stable readings needed to indicate a background value
accumulator = np.zeros([2,1]) #for averaging
cleandata_zacurate_sensor_pulse_0 = np.zeros([2,0]) #output np.array for first pulse
cleandata_zacurate_sensor_pulse_1 = np.zeros([2,0]) #output np.array for second pulse
cleandata_zacurate_sensor_background = np.zeros([2,0]) #output np.array for background data 
bucket = -1 #-1 for start (unknown bucket), 0 for pulse_0, 1 for pulse_1, 2 for background
for x in rawdata:
    if abs((x[2] - prev_zacurate_sensor) / prev_zacurate_sensor) > threshold:
        if counter > 0: #checks for repeated over-threshold changes
            temp = np.array([accumulator[0] / counter, accumulator[1] / counter])
            if counter > background_mintime:    
                cleandata_zacurate_sensor_background = np.append(cleandata_zacurate_sensor_background,temp,axis=1)
                bucket = 2
            elif bucket == 2:
                cleandata_zacurate_sensor_pulse_0 = np.append(cleandata_zacurate_sensor_pulse_0,temp,axis=1)
                bucket = 0
            elif bucket == 0: # ignore background pulse between signal pulses
                bucket = 0.5
            elif bucket == 0.5:
                cleandata_zacurate_sensor_pulse_1 = np.append(cleandata_zacurate_sensor_pulse_1,temp,axis=1)
                bucket = 1
            counter = 0
            accumulator = np.zeros([2,1])
    else:
        counter += 1
        accumulator[0] += x[0] / 1000000
        accumulator[1] += x[2]
    prev_zacurate_sensor = x[2]

print(cleandata_zacurate_sensor_pulse_0)
    
pulse_fft = fftpack.fft(cleandata_zacurate_sensor_pulse_0[1])
pulse_power = np.abs(pulse_fft)
pulse_freq = fftpack.fftfreq(cleandata_zacurate_sensor_pulse_0[1].size, d=0.02)
    
fig, ax = plt.subplots(figsize=(16,9), dpi=59)
ax.plot(pulse_freq, pulse_power, label='pulse_0 fft')
ax.set_xlim(left=0, right=2)
ax.set_ylim(bottom=0, top=80000)
#ax.plot(cleandata_zacurate_sensor_pulse_0[0,:], cleandata_zacurate_sensor_pulse_0[1,:], label='pulse_0')
#ax.plot(cleandata_zacurate_sensor_pulse_1[0,:], cleandata_zacurate_sensor_pulse_1[1,:], label='pulse_1')
#ax.plot(cleandata_zacurate_sensor_background[0,:], cleandata_zacurate_sensor_background[1,:], label='background')
ax.set_xlabel('period (s)')  # Add an x-label to the axes.
ax.set_ylabel('power')  # Add a y-label to the axes.
ax.set_title('Zacurate sensor')
ax.legend()
[[    5.374552     5.384552     5.394552 ...,    55.348352    55.358352
     55.368352]
 [ 2914.        2917.        2924.       ...,  3611.        3616.        3619.      ]]
Out[40]:
<matplotlib.legend.Legend at 0x7f6d63d08f28>
In [ ]: