{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/anaconda3/lib/python3.8/site-packages/scipy/__init__.py:138: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.23.0)\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion} is required for this version of \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "importing Jupyter notebook from params.ipynb\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import import_ipynb\n",
    "from params import model_params\n",
    "p = model_params('config.ini')\n",
    "\n",
    "def freq2erb (freq_hz):\n",
    "    return 9.265 * np.log(1 + freq_hz / (24.7 * 9.265))\n",
    "\n",
    "def erb2freq (n_erb):\n",
    "    return 24.7 * 9.265 * (np.exp(n_erb / 9.265) -1)\n",
    "\n",
    "def ERBBand(N, high_lim, NFFT):\n",
    "    freqRangePerBin = high_lim / (NFFT/2)\n",
    "    erb_low = freq2erb(0)\n",
    "    erb_high = freq2erb(high_lim)\n",
    "    erb_lims = np.linspace(erb_low, erb_high, N)\n",
    "    \n",
    "    cutoffs = [erb2freq(erb_lims[i]) for i in range (N)]\n",
    "    \n",
    "    nfftborder = [np.round((cutoffs[i]+(freqRangePerBin/2))/freqRangePerBin) for i in range (N)]\n",
    "\n",
    "    for i in range (N-2):\n",
    "        if (nfftborder[i+2] - nfftborder[i]) < 2:\n",
    "            nfftborder[i+2] += (2-(nfftborder[i+2] - nfftborder[i]))\n",
    "    \n",
    "    return nfftborder\n",
    "\n",
    "\n",
    "def ERB_pro_matrix(nfftborder, NFFT, mode):\n",
    "    N                 = len(nfftborder)\n",
    "    ERB_Matrix        = np.zeros((int(NFFT / 2 +1) , N))\n",
    "    \n",
    "    for i in range(N - 1):\n",
    "        band_size        = nfftborder[i+1]-nfftborder[i]\n",
    "        \n",
    "        for j in range(int(band_size)):\n",
    "            ERB_Matrix[int(nfftborder[i])+j, i]  = 1 - j / band_size\n",
    "            ERB_Matrix[int(nfftborder[i])+j,i+1] = j / band_size\n",
    "    if mode==0:\n",
    "        ERB_Matrix[:, 0]   *=2\n",
    "        ERB_Matrix[:, N-1] *=2\n",
    "\n",
    "    return ERB_Matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.0,\n",
       " 1.0,\n",
       " 2.0,\n",
       " 3.0,\n",
       " 5.0,\n",
       " 6.0,\n",
       " 8.0,\n",
       " 10.0,\n",
       " 12.0,\n",
       " 15.0,\n",
       " 18.0,\n",
       " 21.0,\n",
       " 25.0,\n",
       " 30.0,\n",
       " 36.0,\n",
       " 42.0,\n",
       " 50.0,\n",
       " 59.0,\n",
       " 69.0,\n",
       " 81.0,\n",
       " 94.0,\n",
       " 110.0,\n",
       " 129.0,\n",
       " 151.0,\n",
       " 176.0,\n",
       " 205.0,\n",
       " 239.0,\n",
       " 279.0,\n",
       " 325.0,\n",
       " 378.0,\n",
       " 440.0,\n",
       " 513.0]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.ones(513)\n",
    "ERBB = ERBBand(N=32, high_lim=24000, NFFT=1024)\n",
    "ERBB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ERB_Matrix = ERB_pro_matrix(ERBB, NFFT=1024, mode=0) #  ERB convert matrix\n",
    "# iERB_Matrix = ERB_pro_matrix(ERBB, NFFT=1024, mode=1) #  ERB convert matrix\n",
    "# plt.plot(ERB_Matrix)\n",
    "# plt.show()\n",
    "# plt.plot(iERB_Matrix)\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.41421356, 1.        , 1.        , 1.22474487, 1.22474487,\n",
       "       1.22474487, 1.41421356, 1.41421356, 1.58113883, 1.73205081,\n",
       "       1.73205081, 1.87082869, 2.12132034, 2.34520788, 2.44948974,\n",
       "       2.64575131, 2.91547595, 3.082207  , 3.31662479, 3.53553391,\n",
       "       3.80788655, 4.18330013, 4.52769257, 4.84767986, 5.19615242,\n",
       "       5.61248608, 6.08276253, 6.55743852, 7.03562364, 7.58287544,\n",
       "       8.21583836, 8.48528137])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ERB_Matrix = ERB_pro_matrix(ERBB, NFFT=1024, mode=0) #  ERB convert matrix\n",
    "Shane_B = np.sqrt(X @ ERB_Matrix)\n",
    "Shane_B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.ones(64)\n",
    "iERB_Matrix = ERB_pro_matrix(ERBB, NFFT=1024, mode=1) # inverse ERB convert matrix\n",
    "# X @ iERB_Matrix.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.        , 0.        , ..., 0.        , 0.        ,\n",
       "        0.        ],\n",
       "       [0.        , 1.        , 0.        , ..., 0.        , 0.        ,\n",
       "        0.        ],\n",
       "       [0.        , 0.        , 1.        , ..., 0.        , 0.        ,\n",
       "        0.        ],\n",
       "       ...,\n",
       "       [0.        , 0.        , 0.        , ..., 0.        , 0.        ,\n",
       "        0.        ],\n",
       "       [0.        , 0.        , 0.        , ..., 0.04109589, 0.02739726,\n",
       "        0.01369863],\n",
       "       [0.        , 0.        , 0.        , ..., 0.95890411, 0.97260274,\n",
       "        0.98630137]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iERB_Matrix.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# iERB_Matrix = ERB_pro_matrix(ERBB, NFFT=960, mode=1) # inverse ERB convert matrix\n",
    "# iShane_B = Shane_B @ iERB_Matrix.T\n",
    "# np.round(iShane_B,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
