.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/approximation/plot_dft.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_approximation_plot_dft.py: ========================== Discrete Fourier Transform ========================== Discrete Fourier Transform is a signal processing technique that transforms a signal of size `n` into a vector of complex Fourier coefficients of size `n`. When the signal consists of floats, the transformation can be made bijective and consists of a vector of floats of size `n`. The first Fourier coefficients are the coefficients from the lowest frequencies and represent the trend, while the last Fourier coefficients are for the highest frequencies and usually represent noise. A time series can thus be approximated using some of the first Fourier coefficients. This example illustrates the difference between the original time series and the time series approximated with the first Fourier coefficients. It is implemented as :class:`pyts.approximation.DiscreteFourierTransform`. .. GENERATED FROM PYTHON SOURCE LINES 18-65 .. image-sg:: /auto_examples/approximation/images/sphx_glr_plot_dft_001.png :alt: Discrete Fourier Transform :srcset: /auto_examples/approximation/images/sphx_glr_plot_dft_001.png :class: sphx-glr-single-img .. code-block:: default # Author: Johann Faouzi # License: BSD-3-Clause import numpy as np import matplotlib.pyplot as plt from pyts.approximation import DiscreteFourierTransform # Parameters n_samples, n_timestamps = 100, 48 # Toy dataset rng = np.random.RandomState(41) X = rng.randn(n_samples, n_timestamps) # DFT transformation n_coefs = 30 dft = DiscreteFourierTransform(n_coefs=n_coefs, norm_mean=False, norm_std=False) X_dft = dft.fit_transform(X) # Compute the inverse transformation if n_coefs % 2 == 0: real_idx = np.arange(1, n_coefs, 2) imag_idx = np.arange(2, n_coefs, 2) X_dft_new = np.c_[ X_dft[:, :1], X_dft[:, real_idx] + 1j * np.c_[X_dft[:, imag_idx], np.zeros((n_samples, ))] ] else: real_idx = np.arange(1, n_coefs, 2) imag_idx = np.arange(2, n_coefs + 1, 2) X_dft_new = np.c_[ X_dft[:, :1], X_dft[:, real_idx] + 1j * X_dft[:, imag_idx] ] X_irfft = np.fft.irfft(X_dft_new, n_timestamps) # Show the results for the first time series plt.figure(figsize=(6, 4)) plt.plot(X[0], 'o--', ms=4, label='Original') plt.plot(X_irfft[0], 'o--', ms=4, label='DFT - {0} coefs'.format(n_coefs)) plt.legend(loc='best', fontsize=10) plt.xlabel('Time', fontsize=14) plt.title('Discrete Fourier Transform', fontsize=16) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.155 seconds) .. _sphx_glr_download_auto_examples_approximation_plot_dft.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dft.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dft.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_