{
 "metadata": {
  "name": "",
  "signature": "sha256:4dadf70135ac19f86b46054f55c35735380ff80be8d92673ea1a71fe1779641e"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Exercise: Source Separation using NMF"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Goals:\n",
      "\n",
      "1. Separate sources using NMF.\n",
      "2. Analyze and classify separated sources."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Load an audio file:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import urllib\n",
      "from urlparse import urljoin\n",
      "filename = 'simpleLoop.wav'\n",
      "#filename = 'CongaGroove-mono.wav'\n",
      "#filename = '125BOUNC-mono.WAV'\n",
      "url = urljoin('https://ccrma.stanford.edu/workshops/mir2014/audio/', filename)\n",
      "print url\n",
      "urllib.urlretrieve(url, filename=filename)\n",
      "\n",
      "from essentia.standard import MonoLoader\n",
      "fs = 44100.0\n",
      "MonoLoader?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "https://ccrma.stanford.edu/workshops/mir2014/audio/simpleLoop.wav\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Plot the spectrogram:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import librosa\n",
      "librosa.stft?\n",
      "librosa.display.specshow?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use NMF to decompose the spectrogram:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sklearn.decomposition import NMF\n",
      "NMF?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Plot the decomposed matrices:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use the inverse STFT to synthesize the separated sources:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "librosa.istft?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use the columns of the matrix, $W$, otherwise known as *spectral atoms*, as inputs into the kick/snare classifier that you created in an earlier exercise. Observe the results; are you able to automatically classify the separated sources?"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Bonus"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use different audio files."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Alter the rank of the decomposition, `n_components`. What happens when `n_components` is too large? too small?"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "NMF is a useful preprocessor for MIR tasks such as music transcription. Using the steps above, build your own simple transcription system that returns a sequence of note events, `[(onset time, class label, volume/gain)...]`."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}