Search code examples
pythonpython-2.7scipylinear-interpolation

Finding multiple interpolated x-values from a function that is not invertible


I am trying to find two interpolated x-values of a function found where a threshold is crossed.

here

I have tried switching the x and y values as proposed in previous answers to similar questions; however, this only returns one of the two x-values I am looking for due to the values having the same x-coordinate when inverted.

Inverted Graph.

The red point on each graph shows the point I've already located with the following code:

interp = interp1d(data[1], data.times)
val = interp(great.middle.iloc[1])

Where data is a pandas data frame that contains all the events (so here we are looking at event 1) and a single time column and great is another pandas data frame where the middle value is the greatest value in the event column divided by 2. Here val is equal to 42.28045192307682 which is the interpolated time at which the middle value is reached (for the second time).

I have tried narrowing the the values for the interpolation function, but the interpolation of a y- value always results in Nan x-values.

The data that I am using is kind of large but here is an output for a voltage and time: https://drive.google.com/open?id=16xPB5HU_ZJ4qmIdD8icKrDKAAXAO2lH7 https://drive.google.com/open?id=1Yc-_ole-dFAnpTNJhEjKNYU6hQfCSiar


Solution

  • Here's how I solved this:

    #save the event data column as pulse (y-values)
    pulse = data[1]
    #save the time data as time (x-values)
    times = data.times
    
    #resample the zero points of the data (the data - the threshold)
    spulse = signal.resample(pulse - np.max(pulse)/2., 10*len(pulse))
    
    #find the place where the data changes signs
    crossings = np.where(np.diff(np.sign(spulse)))[0]
    
    #because the data has noise, average the first and second crossings 
    first = np.mean(crossings[crossings < np.argmax(spulse)])/1000
    second = np.mean(crossings[crossings > np.argmax(spulse)])/1000
    
    #print the times that the threshold was crossed
    print("First crossing at: " + str(first) + ", Second at: " + str(second))