Permalink
Browse files

Improved interpolation

Improving the cubic spline interpolation by using mirror symmetry
  • Loading branch information...
1 parent f3382f2 commit 6389443b872ca5ae33a33165aaf450f85d3b79f1 @jaidevd committed May 12, 2013
Showing with 92 additions and 1 deletion.
  1. +49 −0 boundary_conditions.py
  2. +43 −1 flandrin_emd.py
View
49 boundary_conditions.py
@@ -0,0 +1,49 @@
+import numpy as np
+import matplotlib.pyplot as plt
+from flandrin_emd import extr
+
+def boundary_conditions(x, t=None, z=None, NBSYM=2):
+
+ """ Generates mirrored extrema beyond the singal limits. """
+
+ if not t:
+ t = np.arange(len(x))
+ if not z:
+ z = x
+
+ indmin, indmax = extr(x)[:2]
+
+ lmin = indmin[:NBSYM]
+ lmax = indmax[:NBSYM]
+ rmin = indmin[len(indmin)-NBSYM:]
+ rmax = indmax[len(indmax)-NBSYM:]
+
+ lmin_extended = -1*lmin[::-1]
+ lmax_extended = -1*lmax[::-1]
+ rmin_extended = (len(x)-rmin)[::-1] - 1 + len(x)
+ rmax_extended = (len(x)-rmax)[::-1] - 1 + len(x)
+
+ tmin = np.concatenate((lmin_extended,indmin,rmin_extended))
+ tmax = np.concatenate((lmax_extended,indmax,rmax_extended))
+
+ zmin = x[indmin]
+ zmax = x[indmax]
+
+ zmin_left = x[lmin][::-1]
+ zmax_left = x[lmax][::-1]
+ zmin_right = x[rmin][::-1]
+ zmax_right = x[rmax][::-1]
+
+ zmin = np.concatenate((zmin_left, zmin, zmin_right))
+ zmax = np.concatenate((zmax_left, zmax, zmax_right))
+
+ return tmin, tmax, zmin, zmax
+
+if __name__ == "__main__":
+ x = np.random.randn(100)
+ indmin, indmax = extr(x)[:2]
+ plt.plot(x)
+ tmin, tmax, zmin, zmax = boundary_conditions(x)
+ plt.plot(tmin, zmin, 'r.')
+ plt.plot(tmax, zmax, 'g.')
+ plt.show()
View
44 flandrin_emd.py
@@ -144,11 +144,16 @@ def mean_and_amplitude(m,t,INTERP,MODE_COMPLEX,ndirs=4):
NBSYM = 2
if MODE_COMPLEX:
if MODE_COMPLEX == 1:
+ nem = []
+ nzm = []
for k in range(ndirs):
phi = k*pi.ndirs
y = np.real(np.exp(-1j*phi)*m)
indmin, indmax, indzer = extr(y)
-
+ nem.append(len(indmin)+len(indmax))
+ nzm.append(len(indzer))
+ tmin, tmax, zmin, zmax = boundary_conditions(indmin, indmax,
+ t, y, m, NBSYM)
@@ -159,6 +164,43 @@ def mean_and_amplitude(m,t,INTERP,MODE_COMPLEX,ndirs=4):
def stop_sifting_fixe_h():
pass
+################################################################################
+def boundary_conditions(x, t=None, z=None, NBSYM=2):
+
+ """ Generates mirrored extrema beyond the singal limits. """
+
+ if not t:
+ t = np.arange(len(x))
+ if not z:
+ z = x
+
+ indmin, indmax = extr(x)[:2]
+
+ lmin = indmin[:NBSYM]
+ lmax = indmax[:NBSYM]
+ rmin = indmin[len(indmin)-NBSYM:]
+ rmax = indmax[len(indmax)-NBSYM:]
+
+ lmin_extended = -1*lmin[::-1]
+ lmax_extended = -1*lmax[::-1]
+ rmin_extended = (len(x)-rmin)[::-1] - 1 + len(x)
+ rmax_extended = (len(x)-rmax)[::-1] - 1 + len(x)
+
+ tmin = np.concatenate((lmin_extended,indmin,rmin_extended))
+ tmax = np.concatenate((lmax_extended,indmax,rmax_extended))
+
+ zmin = x[indmin]
+ zmax = x[indmax]
+
+ zmin_left = x[lmin][::-1]
+ zmax_left = x[lmax][::-1]
+ zmin_right = x[rmin][::-1]
+ zmax_right = x[rmax][::-1]
+
+ zmin = np.concatenate((zmin_left, zmin, zmin_right))
+ zmax = np.concatenate((zmax_left, zmax, zmax_right))
+
+ return tmin, tmax, zmin, zmax
################################################################################
def stop_sifting():

0 comments on commit 6389443

Please sign in to comment.