TODO [ex999.0]ΒΆ

../../_images/sphx_glr_fig-solution-exercise-constraints-a-fit_001.png

Out:

No handles with labels found to put in legend.

import matplotlib.pyplot as plt
import numpy as np
import lmlib as lm
from lmlib.utils.generator import load_multi_channel

# --- Generating Test signal ---
K = 650
OMEGA = .02*np.pi # sinusoidal frequency

sin_period = 800 # sinusoidal periodicity
k = np.arange(K)


k_start = 68
y_mc = load_multi_channel('EECG_FILT_9CH_10S_FS600HZ.csv', K=K+k_start)
y = y_mc[k_start:,1] # select single channel


# ----- Models and Parameter Estimation  --------------
a = -np.int(0.5*np.pi/OMEGA) # interval length
b = -a+1

x0s = [1, 0] # ALSSM initial state vectors for sine

# Sinusoidal ALSSM
alssm = lm.AlssmSin(omega=OMEGA, rho=1, label='sine model')
# Segment
segment = lm.Segment(a=a, b=b, direction=lm.BACKWARD, g=5000)

# CostSeg
costs = lm.CostSegment(alssm, segment)


# filter signal and take the approximation
se_param = lm.SEParam(costs)
se_param.filter(y)

H = [[1, 0],
     [0, 1]]
H = [[1], # force x_2=0 (=cosine part)
     [0]]
xs = se_param.minimize_x(H) # unconstrained minimization


# ----------------  Plot  -----------------
ks = [148,  290,  454] # indeces to display fit
trajs = lm.map_trajectories(costs.trajectories(xs[ks]), ks, K, merge_ks=True)
wins = lm.map_window(costs.window(), ks, K, merge_ks=True)


fig = plt.figure(figsize=(8,3))

axs = fig.add_subplot(3, 1, 1)
axs.plot(k, wins, lw=1, c='k', ls='--')
axs.set( ylabel='window(s)')


axs = fig.add_subplot(3, 1, (2,3))
axs.plot(k, y, lw=1.0, c='k')

axs.plot(k, trajs, lw=1.5, c='tab:red', ls='-')
axs.grid(True)
axs.legend(loc='upper right')

plt.subplots_adjust(hspace=0.4)

plt.show()

Total running time of the script: ( 0 minutes 0.262 seconds)

Gallery generated by Sphinx-Gallery