{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plot feature map clusters\n\n\nThis example shows how to cluster rhythmic patterns from a feature map.\n\nThis is based on the rhythmic patterns analysis proposed in [CIM2014]_.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Code source: Mart\u00edn Rocamora\n# License: MIT"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Imports\n  - matplotlib for visualization\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import print_function\nimport matplotlib.pyplot as plt\nimport carat"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We group rhythmic patterns into clusters to aid the analysis\nof their differences and similarities.\n\nFirst, we'll load one of the audio files included in `carat`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "audio_path = carat.util.example_audio_file(num_file=1)\n\ny, sr = carat.audio.load(audio_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, we'll load the annotations provided for the example audio file.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "annotations_path = carat.util.example_beats_file(num_file=1)\n\nbeats, beat_labs = carat.annotations.load_beats(annotations_path)\ndownbeats, downbeat_labs = carat.annotations.load_downbeats(annotations_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then, we'll compute the accentuation feature.\n\n**Note:** This example is tailored towards the rhythmic patterns of the lowest\nsounding of the three drum types taking part in the recording, so the analysis\nfocuses on the low frequencies (20 to 200 Hz).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "acce, times, _ = carat.features.accentuation_feature(y, sr, minfreq=20, maxfreq=200)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, we'll compute the feature map.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n_beats = int(round(beats.size/downbeats.size))\nn_tatums = 4\n\nmap_acce, _, _, _ = carat.features.feature_map(acce, times, beats, downbeats, n_beats=n_beats,\n                                               n_tatums=n_tatums)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then, we'll group rhythmic patterns into clusters. This is done using the classical\nK-means method with Euclidean distance (but other clustering methods and distance\nmeasures can be used too).\n\n**Note:** The number of clusters n_clusters has to be specified as an input parameter.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n_clusters = 4\n\ncluster_labs, centroids, _ = carat.clustering.rhythmic_patterns(map_acce, n_clusters=n_clusters)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally we plot the feature map and the obtained clusters.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(12, 6))\n# plot feature map\nax1 = plt.subplot(211)\ncarat.display.map_show(map_acce, ax=ax1, n_tatums=n_tatums)\n# plot feature map with clusters in colors\nax2 = plt.subplot(212)\ncarat.display.map_show(map_acce, ax=ax2, n_tatums=n_tatums, clusters=cluster_labs)\n\nplt.show()"
      ]
    }
  ],
  "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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}