{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Discrete Fourier Transform\n\n\nThis example shows how to approximate a time series using only\nsome of its Fourier coefficients using\n:class:`pyts.approximation.DiscreteFourierTransform`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom pyts.approximation import DiscreteFourierTransform\n\n# Parameters\nn_samples, n_timestamps = 100, 48\n\n# Toy dataset\nrng = np.random.RandomState(41)\nX = rng.randn(n_samples, n_timestamps)\n\n# DFT transformation\nn_coefs = 30\ndft = DiscreteFourierTransform(n_coefs=n_coefs, norm_mean=False,\n                               norm_std=False)\nX_dft = dft.fit_transform(X)\n\n# Compute the inverse transformation\nif n_coefs % 2 == 0:\n    real_idx = np.arange(1, n_coefs, 2)\n    imag_idx = np.arange(2, n_coefs, 2)\n    X_dft_new = np.c_[\n        X_dft[:, :1],\n        X_dft[:, real_idx] + 1j * np.c_[X_dft[:, imag_idx],\n                                        np.zeros((n_samples, ))]\n    ]\nelse:\n    real_idx = np.arange(1, n_coefs, 2)\n    imag_idx = np.arange(2, n_coefs + 1, 2)\n    X_dft_new = np.c_[\n        X_dft[:, :1],\n        X_dft[:, real_idx] + 1j * X_dft[:, imag_idx]\n    ]\nX_irfft = np.fft.irfft(X_dft_new, n_timestamps)\n\n# Show the results for the first time series\nplt.figure(figsize=(12, 8))\nplt.plot(X[0], 'o--', label='Original')\nplt.plot(X_irfft[0], 'o--', label='DFT - {0} coefs'.format(n_coefs))\nplt.legend(loc='best', fontsize=14)\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
}