import numpy as np
import matplotlib.pyplot as plt
from ripser import ripser
from persim import plot_diagrams

t = np.linspace(0, 30*np.pi, 600)
signal = np.cos(t) - np.cos(2 * t)

d = 6 
tau = 10 

def takens_embedding(signal, d, tau):
    n = len(signal)
    m = n - (d - 1) * tau
    embedded = np.zeros((m, d))
    for i in range(m):
        for j in range(d):
            embedded[i, j] = signal[i + j * tau]
    return embedded

point_cloud = takens_embedding(signal, d, tau)

result = ripser(point_cloud, maxdim=2)
diagrams = result['dgms']

fig, axes = plt.subplots(1, 2, figsize=(14, 5))

axes[0].plot(t, signal)
axes[0].set_xlabel('t')
axes[0].set_ylabel('cos(t) - cos(2·t)')
axes[0].grid(True, alpha=0.3)

plot_diagrams(diagrams, ax=axes[1], legend=True)

plt.tight_layout()
plt.show()