DC Offset in Python

How to remove DC Offset for fft calculations in python?

1 Like

I recommend asking this question of the numpy/scipy communities, or in dsp.stackexchange.com.

Assuming you’re using numpy for fft calculations, removing the DC offset is a matter of subtracting the mean. For a 1-D signal, it would look something like this

import numpy as np
balanced_signal = biased_signal - np.mean(biased_signal)
spectrum = np.fft.rfft(balanced_signal)

For a windowed signal stored as a 2-D ndarray, it isn’t very different. Assuming you have a time axis as the 0th axis and frames as the 1st axis, you could do

import numpy as np
balanced_signal = biased_signal - np.mean(biased_signal, axis=-1)
spectrogram = np.fft.rfft(balanced_signal, axis=-1)

This of course assumes you’ve already made a lot of decisions about how to window the signal, etc.

1 Like

Dear Stefan,

Thank you so much for your suggestions.

Do we have to take absolute value while we subtract the mean from the data points?

No, you should not use the absolute value. The DC offset can be positive or negative. You have to keep the sign to really make the measurement have mean value 0.