I have a Loss function L and 2 signals f(t),g(t) .
I would like to find the function s
that minimize: L(f(t)-g(t+s(t)))+lambda*integral(s''(t))
Ideally the s
should be a polynomial or a spline.
Here the code I have written to start the problem:
import numpy as np
from numpy import *
from numpy.linalg import *
import sklearn as sk
import scipy as sp
import scipy.io
from matplotlib.pyplot import *
from scipy.interpolate import *
#mat = scipy.io.loadmat('/home/luca/Documents/phd_python_code/peak_aligment/john_example.mat')
mat = scipy.io.loadmat('../peak_aligment/john_example.mat')
t1=mat["t1"].squeeze()
t2=mat["t2"].squeeze()
t3=mat["t3"].squeeze()
t=sort(unique(concatenate((t1,t2,t3),axis=0)))
t_min=max(min(t1),min(t2),min(t3))
t_max=min(max(t1),max(t2),max(t3))
t=[val for val in t if t_min<=val<=t_max]
sig1=mat["sig_1"].squeeze()
sig2=mat["sig_2"].squeeze()
sig3=mat["sig_3"].squeeze()
s1=interp1d(t1,sig1,kind="cubic")
s2=interp1d(t2,sig2,kind="cubic")
s3=interp1d(t3,sig3,kind="cubic")
EDIT: I am closer to the solutions but I am still having problems.
I defined a cost function:
def lost_function(p):
pt=polyval(p,t)
return norm(s1(t)-s2(pt))/len(index)
And I am trying to minimize it using scipy.optimize.minize.
the main problem is that often pt
is outside the range value of t
. So when I call s2(pt) i
receive an error. How can I interpolate the function also outside the range of t?