{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Detrending a signal\n\n:func:`scipy.signal.detrend` removes a linear trend.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate a random signal with a trend\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nt = np.linspace(0, 5, 100)\nrng = np.random.default_rng()\nx = t + rng.normal(size=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Detrend\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import scipy as sp\n\nx_detrended = sp.signal.detrend(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nplt.figure(figsize=(5, 4))\nplt.plot(t, x, label=\"x\")\nplt.plot(t, x_detrended, label=\"x_detrended\")\nplt.legend(loc=\"best\")\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.12.11" } }, "nbformat": 4, "nbformat_minor": 0 }