from read_baseline_data import *
from IPython import embed
import matplotlib.pyplot as plt
import numpy as np

data_dir = '../data'
#dataset = '2018-11-09-ad-invivo-1'
data = ["2018-11-09-ad-invivo-1",
        "2018-11-13-aa-invivo-1", "2018-11-13-ad-invivo-1", "2018-11-13-ah-invivo-1", "2018-11-13-ai-invivo-1",
        "2018-11-13-ak-invivo-1", "2018-11-13-al-invivo-1",
        "2018-11-14-ac-invivo-1", "2018-11-14-af-invivo-1", "2018-11-14-ag-invivo-1", "2018-11-14-ak-invivo-1",
        "2018-11-14-al-invivo-1", "2018-11-14-am-invivo-1", "2018-11-14-an-invivo-1",
        "2018-11-20-ab-invivo-1", "2018-11-20-ac-invivo-1", "2018-11-20-ad-invivo-1", "2018-11-20-af-invivo-1",
        "2018-11-20-ag-invivo-1", "2018-11-20-ah-invivo-1", "2018-11-20-ai-invivo-1"]

for dataset in data:
# read eod and time of baseline
    time, eod = read_baseline_eod(os.path.join(data_dir, dataset))

    eod_norm = eod - np.mean(eod)

# calculate eod times and indices by zero crossings
    threshold = 0
    shift_eod = np.roll(eod_norm, 1)
    eod_times = time[(eod_norm >= threshold) & (shift_eod < threshold)]


## normal
    eod_freq_normal = 1/np.diff(eod_times)
    overallfreq = np.mean(eod_freq_normal)
    print(overallfreq)

exit()
kernel = np.ones(7)/7

smooth_eod_freq_normal = np.convolve(eod_freq_normal, kernel, mode = 'valid')
time_axis = np.arange(len(smooth_eod_freq_normal))
#print(eod_freq)
#print(smooth_eod_freq)

fig = plt.plot(time_axis,smooth_eod_freq_normal, linewidth = 2.0)
plt.xlabel("time [ms]", fontsize = 14)
plt.xticks(fontsize = 12)
plt.ylabel("eod frequency [mV]", fontsize = 14)
plt.yticks(fontsize = 12)
plt.axis([0, len(time_axis), 0, 1000])

plt.show()







## beat
#x = np.arange(0, len(time_axis, 0.000001)  # Start, Stop, Step
#y = np.sin(x * 2* np.pi * 600)
#plt.plot(x, y)
#plt.show()


#eod_freq_beat = eod_freq_normal + np.sin(x * 2* np.pi * 600)
#smooth_eod_freq_beat = np.convolve(eod_freq_beat, kernel, mode = 'valid')
#fig = plt.plot(time_axis,smooth_eod_freq_beat)
#plt.xlabel("time [ms]")
#plt.ylabel("eod frequency [mV]")
#plt.show()