{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Randomly Mapped Cosines\n\nAn example plot of :class:`sksfa.SFA` applied to randomly mixed and shifted cosines.\nSFA is able to recover the slowest of the underlying signals perfectly, except for\nsign and offset.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom sksfa import SFA\nimport matplotlib.pyplot as plt\n\n\nn_samples = 500\ndim = 8\nn_slow_features = 2\n\n# Generate different randomly shifted time-scales\nt = np.linspace(0, 2*np.pi, n_samples).reshape(n_samples, 1)\nt = t * np.arange(1, dim+1)\nt += np.random.uniform(0, 2*np.pi, (1, dim))\n\n# Generate latent cosine signals\nx = np.cos(t)\n\n# Compute random affine mapping of cosines (observed)\nA = np.random.normal(0, 1, (dim, dim))\nb = np.random.normal(0, 2, (1, dim))\ndata = np.dot(x, A) + b\n\n# Extract slow features from observed data\nsfa = SFA(n_slow_features)\nslow_features = sfa.fit_transform(data)\n\n# Plot cosines, mapped data, and extracted features\nfig, ax = plt.subplots(3, 1, sharex=True)\nfig.subplots_adjust(hspace=0.5)\nfor d in reversed(range(n_slow_features, dim)):\n    ax[0].plot(x[:, d], color=(0.2, 0.2, 0.2, 0.25))\nfor d in range(n_slow_features):\n    ax[0].plot(x[:, d])\nax[1].plot(data)\nax[2].plot(slow_features)\nax[0].set_title(\"x(t), slowest features colored\")\nax[1].set_title(\"A\u22c5x(t) + b\")\nax[2].set_title(\"Extracted features\")\nax[2].set_xlabel(\"Time t\")\nfor idx in range(3):\n    ax[idx].set_ylabel(\"Features\")\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.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}