from functionssimulation import find_lm
from functionssimulation import find_periods
from functionssimulation import integrate_chirp
from functionssimulation import global_maxima
from functionssimulation import find_lm
from functionssimulation import find_dev
from functionssimulation import remove_all_spines
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
#embed()
from functionssimulation import pl_eods
from functionssimulation import single_stim
from functionssimulation import find_times


#embed()
if __name__ == "__main__":
    delta_t = 0.014  # ms
    interest_interval = delta_t * 1.2
    bef_c = interest_interval / 2
    aft_c = interest_interval / 2
    sigma = delta_t / math.sqrt((2 * math.log(10)))  # width of the chirp
    size = [120]  # maximal frequency excursion during chirp / 60 or 100 here
    phase_zero = [0]  # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
    phase_zero = np.arange(0,2*np.pi,2*np.pi/10)
    eod_fr = 500  # eod fish reciever
    a_fr = 1  # amplitude fish reciever
    amplitude = a_fe = 0.50  # amplitude fish emitter
    factor = 200
    sampling = eod_fr * factor
    sampling_fish = 500
    start = 510
    end = 3500
    step = 500
    win = 'w2'
    minus_bef = -15
    plus_bef = -10
    eod_fe = np.array([10, 510,1010,1510])
    eod_fe = np.array([560])
    eod_fr = 500
    beats = eod_fe - eod_fr
    beat_corr = eod_fe % eod_fr
    beat_corr[beat_corr > eod_fr / 2] = eod_fr - beat_corr[beat_corr > eod_fr / 2]
    #plot_eod(beat_corr, minus_bef, plus_bef,2,2,a_fe, delta_t, a_fr, size, beat_corr, 0, 0, a_fe, phase_zero, deviation_ms, eod_fe, deviation_dp, eod_fr, sampling, factor,fs = [6.2992, 5],shift_phase = (2 * np.pi) / 4)
    ax = {}
    s = 0
    times_width = 1.5
    times_up = 1
    fig = plt.figure(figsize = [6.7,1.8*times_up])
    beat_corr_col = 'goldenrod'
    df_col = 'steelblue'
    beat_corr_col = 'no'
    df_col = 'no'
    colors = ['red']
    sampling_rate = sampling
    nfft = 4096
    row = 1
    col = 1
    p = 0
    delta_t = 0.014  # ms
    x = [ 1.5]
    deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling)
    d = 0
    e = 0
    shift_phase = 0
    ax = {}
    lower = gridspec.GridSpec(1, 5,width_ratios = [1,0.33,1,0.33,1],left = 0.13,hspace = 0.5, wspace = 0.1, bottom = 0.27)
    #ax = plot_beat_corr(ax,lower,beat_corr_col = beat_corr_col,df_col = df_col,ax_nr = 0)
    #plt.grid()
    left_c = minus_bef * delta_t * sampling
    right_c = plus_bef * delta_t * sampling
    time, time_cut, cut = find_times(left_c, right_c, sampling, deviation_dp[d] / (1000 * sampling))
    time_fish_r = time * 2 * np.pi * eod_fe[0]
    eod_fish_e = a_fe * np.sin(time_fish_r)

    nr = 1
    ax[nr] = plt.subplot(lower[nr])
    plt.scatter(0,0, 200,marker = '+',color = 'black')
    plt.xlim([-50,100])
    #plt.ylim([-200, 200])
    remove_all_spines(ax, nr)
    ax[nr].set_yticks([])
    ax[nr].set_xticks([])
    ax[nr] .spines['right'].set_visible(False)
    ax[nr] .spines['top'].set_visible(False)

    #embed()
    nr = 0
    ax[nr] = plt.subplot(lower[nr])
    plt.ylabel('[mv]')
    #plt.xlabel('Time [ms]')
    plt.xlabel('Time [ms]')
    plt.title('Receiver f = '+str(eod_fr)+' Hz')
    time_fish_r = time * 2 * np.pi * eod_fr
    eod_fish_r = a_fr * np.sin(time_fish_r)
    plt.plot(time * 1000-time[0]* 1000,eod_fish_r,color = 'silver')
    ax[nr].set_xlabel('Time [ms]')
    ax[nr] .spines['right'].set_visible(False)
    ax[nr] .spines['top'].set_visible(False)





    nr = 3
    ax[nr] = plt.subplot(lower[nr])
    #plt.scatter(0,0, 200,marker = '+',color = 'black')
    plt.plot([-35, 15],[20,20], 10, color='black',linewidth = 3)
    plt.plot([-35, 15],[-20,-20], 10, color='black',linewidth = 3)
    ax[nr] .spines['right'].set_visible(False)
    ax[nr] .spines['top'].set_visible(False)

    plt.xlim([-50,100])
    #plt.ylim([-200, 200])
    remove_all_spines(ax, nr)
    ax[nr].set_yticks([])
    ax[nr].set_xticks([])
    plt.ylim([-200, 200])

    fmax,lims,ax  = single_stim(ax,colors, row, col,  eod_fr, eod_fe,e,lower,  s = s, p = p, d = d, df_col = df_col,  factor = factor, beat_corr_col = beat_corr_col, nfft = nfft, minus_bef = minus_bef, delta_t = delta_t, sampling = sampling,
                                 plus_bef = plus_bef,a_fe = a_fe, a_fr = a_fr, phase_zero = phase_zero, shift_phase = shift_phase, size = size, a_fe = a_fe,ax_nr = 4,lw_whole = 1,y = 'no')

    #embed()
    minumum = np.min([lims, eod_fish_e, eod_fish_r])
    maximum = np.max([lims, eod_fish_e, eod_fish_r])
    plt.ylim([minumum,maximum])


    nr = 2
    ax[nr] = plt.subplot(lower[nr])
    #plt.ylabel('[mv]')

    plt.title('Emitting f = ' + str(eod_fe[0])+' Hz')
    plt.plot(time * 1000-time[0]* 1000,eod_fish_e,color = 'silver')
    plt.xlabel('Time [ms]')
    plt.ylim([minumum, maximum])
    ax[0].set_ylim([minumum, maximum])
    ax[nr] .spines['right'].set_visible(False)
    ax[nr] .spines['top'].set_visible(False)
    ax[2].set_ylim([minumum, maximum])


    plt.savefig('introamtuning.pdf')
    plt.savefig('../highbeats_pdf/introamtuning.pdf')
    plt.show()