{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 266,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gymnasium as gym\n",
    "import numpy as np\n",
    "from gymnasium import spaces\n",
    "import ctypes\n",
    "from collections import deque\n",
    "from typing import Tuple\n",
    "import sys\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import queue\n",
    "import pandas as pd\n",
    "FIFO_LEN = 60\n",
    "CONV_LEN = 60"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 341,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 133432 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# target = 1.25 * np.sin(np.linspace(0.5 * np.pi, 8.5*np.pi, FIFO_LEN))\n",
    "target = np.sin(np.linspace(0, 8*np.pi, FIFO_LEN)) + 0.5 * np.sin(np.linspace(0.3 * np.pi, 16.3*np.pi, FIFO_LEN)) + 2\n",
    "s_div = np.sin(np.linspace(0, 8*np.pi, FIFO_LEN)) + 0.5 * np.sin(np.linspace(0.3 * np.pi, 16.3*np.pi, FIFO_LEN)) + 1\n",
    "s_amp = 0.5*np.sin(np.linspace(0, 8*np.pi, FIFO_LEN)) + 0.25 * np.sin(np.linspace(0.3 * np.pi, 16.3*np.pi, FIFO_LEN)) + 2\n",
    "s_sin = 1.25 * np.sin(np.linspace(-0.05 * np.pi, 7.95*np.pi, FIFO_LEN)) + 2\n",
    "s_pha = np.sin(np.linspace(1*np.pi, 9*np.pi, FIFO_LEN)) + 0.5 * np.sin(np.linspace(2.3 * np.pi, 18.3*np.pi, FIFO_LEN)) + 2\n",
    "s_lin = np.ones(FIFO_LEN) * 2 + 1e-5 * np.random.rand(FIFO_LEN)\n",
    "s_rad = np.random.rand(FIFO_LEN) * 3 + 0.5\n",
    "\n",
    "plt.plot(np.array([target, s_div, s_amp, s_sin, s_pha, s_lin, s_rad]).T)\n",
    "data = {}\n",
    "data[\"div\"] = s_div\n",
    "data[\"amp\"] = s_amp\n",
    "data[\"sin\"] = s_sin\n",
    "data[\"pha\"] = s_pha\n",
    "data[\"lin\"] = s_lin\n",
    "data[\"rad\"] = s_rad\n",
    "data[\"target\"] = target\n",
    "df = pd.DataFrame(data)\n",
    "df.to_csv(\"data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 438,
   "metadata": {},
   "outputs": [],
   "source": [
    "state = s_rad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 439,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1601700742597777"
      ]
     },
     "execution_count": 439,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "correlation = np.correlate(target - np.mean(target), state - np.mean(state), \"full\")\n",
    "np.max(correlation) / FIFO_LEN / np.std(target) / np.std(state)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 440,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.12411271771033355"
      ]
     },
     "execution_count": 440,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(np.corrcoef(target, state)[0, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 441,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9588048719244383"
      ]
     },
     "execution_count": 441,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(-np.abs((np.mean(target) - np.mean(state)) / np.std(target) * 0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 442,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8732182363783481"
      ]
     },
     "execution_count": 442,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(-np.abs((np.std(target-np.mean(target)) - np.std(state-np.mean(state))) / np.std(target) * 1.25))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 443,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3686030949663357"
      ]
     },
     "execution_count": 443,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def dynamic_time_warping(target, state):\n",
    "    n = len(target)\n",
    "    m = len(state)\n",
    "    \n",
    "    # 创建一个 (n+1) x (m+1) 的矩阵，初始化为无穷大\n",
    "    dtw_matrix = np.full((n + 1, m + 1), np.inf)\n",
    "    dtw_matrix[0, 0] = 0\n",
    "    \n",
    "    # 填充 DTW 矩阵\n",
    "    for i in range(1, n + 1):\n",
    "        for j in range(1, m + 1):\n",
    "            cost = abs(target[i - 1] - state[j - 1])\n",
    "            last_min = min(dtw_matrix[i - 1, j],    # 插入\n",
    "                           dtw_matrix[i, j - 1],    # 删除\n",
    "                           dtw_matrix[i - 1, j - 1]) # 匹配\n",
    "            dtw_matrix[i, j] = cost + last_min\n",
    "    \n",
    "    # 返回 DTW 距离\n",
    "    return dtw_matrix[n, m]\n",
    "1 - (dynamic_time_warping(target, state) / FIFO_LEN)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pycuda_3_10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
