{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ad51c589-8c0c-4677-b145-ad5ab4710e70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: numpy in /opt/homebrew/lib/python3.12/site-packages (1.26.4)\n",
      "Collecting scipy\n",
      "  Downloading scipy-1.13.1-cp312-cp312-macosx_12_0_arm64.whl.metadata (60 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.6/60.6 kB\u001b[0m \u001b[31m234.8 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: numpy<2.3,>=1.22.4 in /opt/homebrew/lib/python3.12/site-packages (from scipy) (1.26.4)\n",
      "Downloading scipy-1.13.1-cp312-cp312-macosx_12_0_arm64.whl (30.4 MB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m30.4/30.4 MB\u001b[0m \u001b[31m13.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
      "\u001b[?25hInstalling collected packages: scipy\n",
      "Successfully installed scipy-1.13.1\n",
      "Collecting matplotlib\n",
      "  Downloading matplotlib-3.9.0-cp312-cp312-macosx_11_0_arm64.whl.metadata (11 kB)\n",
      "Collecting contourpy>=1.0.1 (from matplotlib)\n",
      "  Downloading contourpy-1.2.1-cp312-cp312-macosx_11_0_arm64.whl.metadata (5.8 kB)\n",
      "Collecting cycler>=0.10 (from matplotlib)\n",
      "  Using cached cycler-0.12.1-py3-none-any.whl.metadata (3.8 kB)\n",
      "Collecting fonttools>=4.22.0 (from matplotlib)\n",
      "  Downloading fonttools-4.53.0-cp312-cp312-macosx_11_0_arm64.whl.metadata (162 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m162.2/162.2 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting kiwisolver>=1.3.1 (from matplotlib)\n",
      "  Using cached kiwisolver-1.4.5-cp312-cp312-macosx_11_0_arm64.whl.metadata (6.4 kB)\n",
      "Requirement already satisfied: numpy>=1.23 in /opt/homebrew/lib/python3.12/site-packages (from matplotlib) (1.26.4)\n",
      "Requirement already satisfied: packaging>=20.0 in /opt/homebrew/Cellar/jupyterlab/4.1.6_1/libexec/lib/python3.12/site-packages (from matplotlib) (24.0)\n",
      "Collecting pillow>=8 (from matplotlib)\n",
      "  Downloading pillow-10.3.0-cp312-cp312-macosx_11_0_arm64.whl.metadata (9.2 kB)\n",
      "Collecting pyparsing>=2.3.1 (from matplotlib)\n",
      "  Downloading pyparsing-3.1.2-py3-none-any.whl.metadata (5.1 kB)\n",
      "Requirement already satisfied: python-dateutil>=2.7 in /opt/homebrew/Cellar/jupyterlab/4.1.6_1/libexec/lib/python3.12/site-packages (from matplotlib) (2.9.0.post0)\n",
      "Requirement already satisfied: six>=1.5 in /opt/homebrew/Cellar/jupyterlab/4.1.6_1/libexec/lib/python3.12/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n",
      "Downloading matplotlib-3.9.0-cp312-cp312-macosx_11_0_arm64.whl (7.8 MB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.8/7.8 MB\u001b[0m \u001b[31m14.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
      "\u001b[?25hDownloading contourpy-1.2.1-cp312-cp312-macosx_11_0_arm64.whl (245 kB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m245.3/245.3 kB\u001b[0m \u001b[31m7.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hUsing cached cycler-0.12.1-py3-none-any.whl (8.3 kB)\n",
      "Downloading fonttools-4.53.0-cp312-cp312-macosx_11_0_arm64.whl (2.2 MB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m13.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
      "\u001b[?25hUsing cached kiwisolver-1.4.5-cp312-cp312-macosx_11_0_arm64.whl (64 kB)\n",
      "Downloading pillow-10.3.0-cp312-cp312-macosx_11_0_arm64.whl (3.4 MB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m13.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
      "\u001b[?25hDownloading pyparsing-3.1.2-py3-none-any.whl (103 kB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m103.2/103.2 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hInstalling collected packages: pyparsing, pillow, kiwisolver, fonttools, cycler, contourpy, matplotlib\n",
      "Successfully installed contourpy-1.2.1 cycler-0.12.1 fonttools-4.53.0 kiwisolver-1.4.5 matplotlib-3.9.0 pillow-10.3.0 pyparsing-3.1.2\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "!{sys.executable} -m pip install numpy\n",
    "!{sys.executable} -m pip install scipy\n",
    "!{sys.executable} -m pip install matplotlib\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3d02afe8-a522-4b2b-875a-2e3004634c30",
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy.io.wavfile as wav\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b85a0d1-6f28-4b0e-b02d-8c61aa55ae7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Writes a WAV file of rendered data\n",
    "def wavwrite(filename, samples, sample_rate):\n",
    "    wav.write(filename, int(sample_rate), (32768.0 * samples).astype(np.int16))\n",
    "\n",
    "def wavread(filename):\n",
    "    sample_rate, samples = wav.read(filename)\n",
    "    return samples.astype(np.float32) / 32768.0, sample_rate\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d9f238c9-e5b6-4445-89df-768bb2dc23d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#test_name = 'TestSineOsc'\n",
    "#test_name = 'TestFilter'\n",
    "#test_name = 'TestFilter24'\n",
    "#test_name = 'TestFilterLFO'\n",
    "#test_name = 'TestChorus'\n",
    "#test_name = 'TestPulseOsc'\n",
    "#test_name = 'TestPcm'\n",
    "#test_name = 'TestSineEnv'\n",
    "#test_name = 'TestAlgo'\n",
    "#test_name = 'TestLFO'\n",
    "#test_name = 'TestPartial'\n",
    "#test_name = 'TestDuty'\n",
    "#test_name = 'TestBrass'\n",
    "#test_name = 'TestSawUpOsc'\n",
    "#test_name = 'TestGuitar'\n",
    "#test_name = 'TestPcmShift'\n",
    "#test_name = 'TestPcm'\n",
    "#test_name = 'TestOverload'\n",
    "#test_name = 'TestJunoPatch'\n",
    "#test_name = 'TestJunoClip'\n",
    "#test_name = 'TestLowVcf'\n",
    "#test_name = 'TestGlobalEQ'\n",
    "#test_name = 'TestFlutesEq'\n",
    "#test_name = 'TestChainedOsc'\n",
    "#test_name = 'TestPartial'\n",
    "#test_name = 'TestPartialNoteOff'\n",
    "test_name = 'TestInterpPartialsRetrigger'\n",
    "#d_ref, sr = wavread('tests/flt_ref/' + test_name + '.wav')\n",
    "d_ref, sr = wavread('../tests/ref/' + test_name + '.wav')\n",
    "d_tst, sr = wavread('../tests/tst/' + test_name + '.wav')\n",
    "# Scale waveforms up to int16 units.\n",
    "d_ref *= 32768\n",
    "d_tst *= 32768"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ba360672-c480-461b-9c94-30f916f15df8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(44032, 2)\n"
     ]
    }
   ],
   "source": [
    "print(d_ref.shape)\n",
    "tt = np.arange(d_ref.shape[0]) / sr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "222aca09-2c6b-4344-80fd-0f87531aede2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x11e0f29c0>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 66024 characters]",
      "text/plain": [
       "<Figure size 1200x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ii =  np.arange(2000, 44000)\n",
    "#ii = np.arange(int(0.2 * sr), int(0.25 * sr))\n",
    "#ii =  np.arange(38000, 42000)\n",
    "#ii =  np.arange(24000, 28000)\n",
    "plt.figure(figsize=(12,4))\n",
    "plt.plot(tt[ii], d_ref[ii, 0], tt[ii], d_tst[ii, 0])\n",
    "plt.legend(['ref', 'tst'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ba7b1c1b-8b72-4363-af5a-1af66c4e8063",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x11e516ba0>]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 11556 characters]",
      "text/plain": [
       "<Figure size 1200x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(12,4))\n",
    "plt.plot(tt[ii], d_tst[ii, 0] - d_ref[ii, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41731f4b-502b-4f04-914b-afe4d349cf72",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "275986c0-ec7f-46eb-bcd2-3f220a14ed65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x1486dd370>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 12968 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "xcorr = np.correlate(np.mean(d_ref, axis=1), np.mean(d_tst, axis=1), mode='same')\n",
    "center = np.argmax(xcorr)\n",
    "offset = int(center - len(d_ref) / 2)\n",
    "print(offset)\n",
    "ii = np.arange(20000, 22000)\n",
    "plt.plot(tt[ii], d_ref[ii, 0], tt[ii], d_tst[ii - offset, 0])\n",
    "plt.legend(['ref', 'tst'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b8c11a9c-42a6-43f1-a7fd-7781043860c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.328085122666891\n"
     ]
    }
   ],
   "source": [
    "print(np.log2(np.exp(3.0)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4b1108c2-d3de-4c10-871e-2101367150a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-98.30169903639559\n",
      "4.25\n",
      "9.375\n"
     ]
    }
   ],
   "source": [
    "print(np.log2(0.0002) * 8)\n",
    "print(34/8)\n",
    "print(75/8)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "f62ce11c-075c-4d9c-aed3-0e51fafee644",
   "metadata": {},
   "source": [
    "print(99/8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f680446d-341c-4582-b06d-acf7f162c26d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
