{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy, scipy, matplotlib.pyplot as plt, IPython.display, sklearn\n", "import essentia, essentia.standard as ess\n", "plt.rcParams['figure.figsize'] = (14,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[← Back to Index](index.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Spectral Features in Essentia" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For classification, we're going to be using new features in our arsenal: spectral moments (centroid, bandwidth, skewness, kurtosis) and other spectral statistics." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[*Moments*](https://en.wikipedia.org/wiki/Moment_(mathematics) is a term used in physics and statistics. There are raw moments and central moments. \n", "\n", "You are probably already familiar with two examples of moments: mean and variance. The first raw moment is known as the mean. The second central moment is known as the variance." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `essentia.standard.Centroid`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To compute the spectral centroid in Essentia, we will use [`essentia.standard.Centroid`](http://essentia.upf.edu/documentation/reference/std_Centroid.html):" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.498104333878\n" ] } ], "source": [ "spectrum = ess.Spectrum()\n", "centroid = ess.Centroid()\n", "x = essentia.array(scipy.randn(1024))\n", "X = spectrum(x)\n", "spectral_centroid = centroid(X)\n", "print spectral_centroid" ] }, { "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", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.49810430034108322" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum((X/sum(X))*numpy.linspace(0, 1, len(X)))" ] }, { "cell_type": "markdown", "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", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1.00000000e+00 0.00000000e+00 8.12825486e-02 -3.57219629e-04\n", " 1.20308148e-02]\n" ] } ], "source": [ "central_moments = ess.CentralMoments()\n", "print central_moments(X)" ] }, { "cell_type": "markdown", "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", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0.08128254860639572, -0.01541485171765089, -1.1790399551391602)\n" ] } ], "source": [ "distributionshape = ess.DistributionShape()\n", "spectral_moments = distributionshape(central_moments(X))\n", "print spectral_moments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[← Back to Index](index.html)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }