{
 "metadata": {
  "name": "",
  "signature": "sha256:319b465b5be6a89b194af42e27ece8ad415727e2aab733ccd86cd4378d4f6a86"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Spectral Features"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "For classification, we're going to be using the new features in our arsenal: cherishing those \"spectral moments\" (centroid, bandwidth, skewness, kurtosis) and also examining other spectral statistics."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "*Moments* is a term used in physics and statistics. There are raw moments and central moments. The first raw moment is known as the mean. The second central moment is known as the variance."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "`essentia.standard.Centroid`"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "To compute the spectral centroid, we will use [`essentia.standard.Centroid`](http://essentia.upf.edu/documentation/reference/std_Centroid.html):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import essentia\n",
      "from essentia.standard import Spectrum, Centroid\n",
      "spectrum = Spectrum()\n",
      "centroid = Centroid()\n",
      "x = essentia.array(randn(1024))\n",
      "X = spectrum(x)\n",
      "spectral_centroid = centroid(X)\n",
      "print spectral_centroid"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0.510950088501\n"
       ]
      }
     ],
     "prompt_number": 46
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This value is normalized between 0 and 1. If 0, then the centroid is at zero. If 1, then the centroid is all the way to the \"right\", i.e., equal to `fs/2`, the Nyquist frequency, or the highest frequency a digital signal can possibly have."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Here is a sanity check:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sum((X/sum(X))*linspace(0, 1, len(X)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 47,
       "text": [
        "0.51095009882361531"
       ]
      }
     ],
     "prompt_number": 47
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "`essentia.standard.CentralMoments`"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The first step to computing the other three spectral moments (spread, skewness, and kurtosis) is to compute the central moments of a spectrum:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from essentia.standard import CentralMoments\n",
      "central_moments = CentralMoments()\n",
      "print central_moments(X)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[  1.00000000e+00   0.00000000e+00   8.45586956e-02  -9.95449489e-04\n",
        "   1.29118226e-02]\n"
       ]
      }
     ],
     "prompt_number": 48
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "`essentia.standard.DistributionShape`"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "To compute the spectral spread, skewness, and kurtosis, we use [`essentia.standard.DistributionShape`](http://essentia.upf.edu/documentation/reference/std_DistributionShape.html):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from essentia.standard import DistributionShape\n",
      "distributionshape = DistributionShape()\n",
      "spectral_moments = distributionshape(central_moments(X))\n",
      "print spectral_moments"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(0.08455869555473328, -0.04048385098576546, -1.1941944360733032)\n"
       ]
      }
     ],
     "prompt_number": 49
    }
   ],
   "metadata": {}
  }
 ]
}