The personal website of Scott W Harden
June 24th, 2010

Detrending Data in Python with Numpy

⚠️ SEE UPDATED POST: Signal Filtering in Python

While continuing my quest into the world of linear data analysis and signal processing, I came to a point where I wanted to emphasize variations in FFT traces. While I am keeping my original data for scientific reference, visually I want to represent it emphasizing variations rather than concentrating on trends. I wrote a detrending function which I'm sure will be useful for many applications:

def detrend(data,degree=10):
    detrended=[None]*degree
    for i in range(degree,len(data)-degree):
        chunk=data[i-degree:i+degree]
        chunk=sum(chunk)/len(chunk)
        detrended.append(data[i]-chunk)
    return detrended+[None]*degree

However, this method is extremely slow. I need to think of a way to accomplish this same thing much faster. [ponders]

UPDATE: It looks like I've once again re-invented the wheel. All of this has been done already, and FAR more efficiently I might add. For more see scipy.signal.detrend.html

import scipy.signal
ffty=scipy.signal.detrend(ffty)
Markdown source code last modified on January 18th, 2021
---
title: Detrending Data in Python with Numpy
date: 2010-06-24 08:38:52
tags: python, old
---

# Detrending Data in Python with Numpy

> **⚠️ SEE UPDATED POST:** [**Signal Filtering in Python**](https://swharden.com/blog/2020-09-23-signal-filtering-in-python/)

__While continuing my quest__ into the world of linear data analysis and signal processing, I came to a point where I wanted to emphasize variations in FFT traces. While I am keeping my original data for scientific reference, visually I want to represent it emphasizing variations rather than concentrating on trends. I wrote a detrending function which I'm sure will be useful for many applications:

```python
def detrend(data,degree=10):
	detrended=[None]*degree
	for i in range(degree,len(data)-degree):
		chunk=data[i-degree:i+degree]
		chunk=sum(chunk)/len(chunk)
		detrended.append(data[i]-chunk)
	return detrended+[None]*degree
```

<div class="text-center">

[![](detrend_fft_thumb.jpg)](detrend_fft.png)

</div>

However, this method is extremely slow. I need to think of a way to accomplish this same thing much faster. \[ponders\]

__UPDATE:__ It looks like I've once again re-invented the wheel. All of this has been done already, and FAR more efficiently I might add. For more see [scipy.signal.detrend.html](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.detrend.html)

```python
import scipy.signal
ffty=scipy.signal.detrend(ffty)
```