🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

signal.detrend()

AI & DATA SCIENCE // signal-detrend

scipy.signal.detrend() removes a linear (or constant) trend from data, isolating the fluctuations around that trend.

Syntax

scipy.signal.detrend(data, type='linear')

Deep Dive Course

Real-world signals often ride on top of an underlying trend, a slow drift upward or downward, that can obscure the more interesting fluctuations you actually want to analyze; detrend() fits and subtracts that trend, leaving just the residual variation around it. type='linear', the default, fits and removes a straight-line trend, while type='constant' simply subtracts the mean, removing only a constant offset rather than any slope.

1Understanding signal.detrend()

Real-world signals often ride on top of an underlying trend, a slow drift upward or downward, that can obscure the more interesting fluctuations you actually want to analyze; detrend() fits and subtracts that trend, leaving just the residual variation around it. type='linear', the default, fits and removes a straight-line trend, while type='constant' simply subtracts the mean, removing only a constant offset rather than any slope.

💡

Use type='constant' instead of the default type='linear' when you only want to center the data around zero by removing its mean, without also removing any actual linear trend/drift the data might have.

editor.html
from scipy import signal
import numpy as np

data = np.array([1.1, 2.8, 5.1, 6.9, 9.1])
detrended = signal.detrend(data)
print(np.round(detrended, 2))
localhost:3000

2Practical Example

Here is a real-world application of signal.detrend() showing how it is used in production SciPy code.

editor.html
from scipy import signal
import numpy as np

data = np.array([10.0, 10.0, 10.0, 10.0])
detrended = signal.detrend(data, type="constant")
print(detrended)
localhost:3000

3Best Practices

Follow these guidelines when working with signal.detrend():

1. Detrend a signal before frequency-domain analysis, like an FFT, since a strong low-frequency trend can dominate and obscure the higher-frequency components you actually care about

2. Choose type='constant' vs type='linear' deliberately based on whether the data has an actual slope worth removing, or just needs centering around its mean

3. Visually inspect the detrended result against the original to confirm the trend removal looks reasonable, rather than assuming it always behaves as expected

⚠️

Tip: Use type='constant' instead of the default type='linear' when you only want to center the data around zero by removing its mean, without also removing any actual linear trend/drift the data might have.

editor.html
from scipy import signal
import numpy as np

data = np.array([1.1, 2.8, 5.1, 6.9, 9.1])
detrended = signal.detrend(data)
print(np.round(detrended, 2))
localhost:3000

Examples

Example 01Basic Usage
from scipy import signal
import numpy as np

data = np.array([1.1, 2.8, 5.1, 6.9, 9.1])
detrended = signal.detrend(data)
print(np.round(detrended, 2))
Example 02Advanced Example
from scipy import signal
import numpy as np

data = np.array([10.0, 10.0, 10.0, 10.0])
detrended = signal.detrend(data, type="constant")
print(detrended)

Best Practices

  • Detrend a signal before frequency-domain analysis, like an FFT, since a strong low-frequency trend can dominate and obscure the higher-frequency components you actually care about
  • Choose type='constant' vs type='linear' deliberately based on whether the data has an actual slope worth removing, or just needs centering around its mean
  • Visually inspect the detrended result against the original to confirm the trend removal looks reasonable, rather than assuming it always behaves as expected

Interview Question

Why might you choose type='constant' over the default type='linear' when detrending a signal?

Hint: Think about what each type actually removes, and situations where removing a slope wouldn't be appropriate.

type='linear' fits and removes an actual sloped trend line, which is appropriate when the data genuinely drifts upward or downward over time. type='constant' instead only removes the overall mean, leaving any real slope in the data completely intact. Choosing 'constant' makes sense when you want to simply center a signal around zero without assuming, or accidentally removing, an actual meaningful trend that a linear fit would otherwise subtract out, which matters if the slope itself is part of what you're trying to analyze rather than noise to discard.

Exercises

MediumPractice using signal.detrend() in a real scenario.
View Solution
from scipy import signal
import numpy as np

data = np.array([1.1, 2.8, 5.1, 6.9, 9.1])
detrended = signal.detrend(data)
print(np.round(detrended, 2))

Frequently Asked Questions

Why might you choose type='constant' over the default type='linear' when detrending a signal?

type='linear' fits and removes an actual sloped trend line, which is appropriate when the data genuinely drifts upward or downward over time. type='constant' instead only removes the overall mean, leaving any real slope in the data completely intact. Choosing 'constant' makes sense when you want to simply center a signal around zero without assuming, or accidentally removing, an actual meaningful trend that a linear fit would otherwise subtract out, which matters if the slope itself is part of what you're trying to analyze rather than noise to discard.

Related Functions

signal-lfilternp-meanstats-describe