{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Date: Jul 2019<br>Programmer:  Patricio López-Serrano, Christian Dittmar, Yigitcan Oezer\n",
    "\n",
    "#### If you use the 'NMF toolbox' please refer to:\n",
    "[] Patricio López-Serrano, Christian Dittmar, Yiğitcan Özer, and Meinard Müller\n",
    "<br>NMF Toolbox: Music Processing Applications of Nonnegative Matrix Factorization\n",
    "<br>In Proceedings of the International Conference on Digital Audio Effects (DAFx), 2019.\n",
    "\n",
    "#### License:\n",
    "This file is part of 'NMF toolbox'. 'NMF toolbox' is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 'NMF toolbox' is distributed in the hope that it will be useful, but ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.\n",
    "\n",
    "#### You should have received a copy of the GNU General Public License along<br>with 'NMF toolbox'. If not, see http://www.gnu.org/licenses/."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import scipy.io.wavfile as wav\n",
    "import matplotlib.pyplot as plt\n",
    "import IPython.display as ipd\n",
    "\n",
    "from NMFToolbox.forwardSTFT import forwardSTFT\n",
    "from NMFToolbox.inverseSTFT import inverseSTFT\n",
    "from NMFToolbox.logFreqLogMag import logFreqLogMag\n",
    "from NMFToolbox.initTemplates import initTemplates\n",
    "from NMFToolbox.initActivations import initActivations\n",
    "from NMFToolbox.NMFD import NMFD\n",
    "from NMFToolbox.NMFdiag import NMFdiag\n",
    "from NMFToolbox.convModel import convModel\n",
    "from NMFToolbox.visualizeComponentsNMF import visualizeComponentsNMF\n",
    "from NMFToolbox.alphaWienerFilter import alphaWienerFilter\n",
    "from NMFToolbox.utils import EPS, make_monaural, pcmInt16ToFloat32Numpy\n",
    "from NMFToolbox.semiFixedComponentConstraintsNMF import semiFixedComponentConstraintsNMF\n",
    "\n",
    "inpPath = '../data/'\n",
    "outPath = 'output/'\n",
    "\n",
    "filename = '120_jd.wav';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Load the audio signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "fs, xTr = wav.read(os.path.join(inpPath, filename))\n",
    "\n",
    "# make monaural if necessary\n",
    "xTr = make_monaural(xTr)\n",
    "\n",
    "# int16 -> float32 conversion\n",
    "xTr = pcmInt16ToFloat32Numpy(xTr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Compute STFT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# spectral parameters\n",
    "paramSTFT = dict()\n",
    "paramSTFT['blockSize'] = 4096;\n",
    "paramSTFT['hopSize'] = 2048;\n",
    "paramSTFT['winFunc'] = np.hanning(paramSTFT['blockSize'])\n",
    "paramSTFT['reconstMirror'] = True\n",
    "paramSTFT['appendFrame'] = True\n",
    "paramSTFT['numSamples'] = len(xTr)\n",
    "\n",
    "\n",
    "# STFT computation\n",
    "XTr, ATr, PTr  = forwardSTFT(xTr, paramSTFT)\n",
    "\n",
    "# get dimensions and time and freq resolutions\n",
    "numBinsTr, numFramesTr = XTr.shape\n",
    "deltaT = paramSTFT['hopSize'] / fs\n",
    "deltaF = fs / paramSTFT['blockSize']\n",
    "\n",
    "# set common parameters\n",
    "numComp = 3\n",
    "numIter = 15\n",
    "numTemplateFrames = 87\n",
    "\n",
    "# generate initial activations\n",
    "paramActivations = dict()\n",
    "paramActivations['numComp'] = numComp\n",
    "paramActivations['numFrames'] = numFramesTr\n",
    "initH = initActivations(paramActivations, 'uniform');\n",
    "\n",
    "# NMFD parameters\n",
    "paramNMFD = dict()\n",
    "paramNMFD['numComp'] = numComp\n",
    "paramNMFD['numFrames'] = numFramesTr\n",
    "paramNMFD['numIter'] = numIter\n",
    "paramNMFD['numTemplateFrames'] = numTemplateFrames\n",
    "paramNMFD['initH'] = initH\n",
    "paramNMFD['numBins'] = numBinsTr\n",
    "\n",
    "# generate initial templates\n",
    "paramTemplates = dict()\n",
    "paramTemplates['numComp'] = numComp\n",
    "paramTemplates['numBins'] = numBinsTr\n",
    "paramTemplates['numTemplateFrames'] = numTemplateFrames\n",
    "\n",
    "initW = initTemplates(paramTemplates, 'random');\n",
    "paramNMFD['initW'] = initW"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 331172 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fc87b09cf60>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 289872 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fc87b01a0f0>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 288696 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fc87b073668>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# visualize initialization data\n",
    "paramVis = dict()\n",
    "paramVis['deltaF'] = deltaF\n",
    "paramVis['deltaT'] = deltaT\n",
    "\n",
    "fh1 = visualizeComponentsNMF(ATr, initW, initH, None, paramVis)\n",
    "\n",
    "# NMFD core method\n",
    "nmfdW, nmfdH, nmfdV, divKL, _ = NMFD(ATr, paramNMFD)\n",
    "\n",
    "# visualize after NMFD\n",
    "paramVis['logComp'] = 1e4\n",
    "fh2 = visualizeComponentsNMF(ATr, nmfdW, nmfdH, nmfdV, paramVis)\n",
    "\n",
    "# Second pass of NMFD\n",
    "paramNMFD['initH'] = nmfdH\n",
    "paramNMFD['initW'] = nmfdW\n",
    "nmfdW, nmfdH, nmfdV, divKL, _= NMFD(ATr, paramNMFD)\n",
    "\n",
    "fh3 = visualizeComponentsNMF(ATr, nmfdW, nmfdH, nmfdV, paramVis)"
   ]
  }
 ],
 "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
