3

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?

Donbeo
  • 17,067
  • 37
  • 114
  • 188
  • shouldn't be an `integral` before `L(f(t)-g(t+s(t)))` as well? also, are `f(t)` and `g(t)` analytic functions or given at finite set of points? – behzad.nouri Feb 21 '14 at 19:12
  • You could try Scipy's spline interpolation (http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html#spline-interpolation) - it also has smoothing. If you want to dive deeper into the topic, you might want to look at http://scikit-learn.org – Dietrich Feb 22 '14 at 01:39
  • @behzad.nouri The integral is only on the last part. This is a modified version of GAM (generalized additive models). At the beginning the f and g where vector but I have interpolated with interp1d so now they are functions. – Donbeo Feb 22 '14 at 11:05
  • @Dietrich I use sklearn very often but I did not find anything of useful in this case. Do you have any advices? – Donbeo Feb 22 '14 at 11:08
  • @Dietrich Oops, I just saw your ``g()``, so its not a standard generalized linear model problem as originally thought. Now that I think about, it looks quite unusal. No chance on bringing it to standard form (maybe by a Fourier Transform)? – Dietrich Feb 22 '14 at 21:08
  • I am not working anymore in this problem but I have been able to solve a similar one using PSO algorithm. Hope it can help – Donbeo May 09 '14 at 09:30

0 Answers0