# How to 0-center a signal that fades?

I'm trying to calculate the frequency of the following signal that has an offset:

So I first try to center it around 0 :

m = max(signal);
mi = min(signal);
n = signal - (mi + (m-mi)/2);


However as the signal fades over time, it eventually falls below 0 towards the end, resulting in a large maxima at 0 on the Fourier transform.

Although the second largest maxima does give me the correct frequency, Is there a better way to center the signal so that it doesn't fall below zero?

• Do you know if the drift is linear? – A_A Mar 1 '19 at 16:21
• I think I can assume it is. I know the signal strength is always decreasing, however with a different amount each time. – Tejas Kale Mar 1 '19 at 16:24

To calculate the frequency you don't need to worry about the offset. To get rid of the offset and get a "clean" damped sine wave you can calculate the amplitude of the first and final peaks and calculate the an offset line. Then just subtract this line to your initial signal.

• I ended up doing something similar. Subtracting a moving average with a window size of approximately 1f seems to work well. – Tejas Kale Mar 1 '19 at 19:32

It is a common pre-processing step in harmonic analysis to remove long term trends prior to assessing the periodic part.

This is known as detrending and there are lots of methods to detrend a time series, depending on its characteristics.

But if you know that the trend is linear then the task is straightforward. All that you have to do is "fit a line" through your data, invert it and then remove its effect.

While you can do this simply by fitting a first degree polynomial (for example), there are also functions in platforms like MATLAB or Python (and others) that will handle the complete detrending process and return to you the detrended waveform.

Hope this helps.

• Thanks, pointed me in the correct direction. – Tejas Kale Mar 1 '19 at 19:30

• Use a simple peak-detection algorithm (eg by a sliding window of a size $$N$$ where you fit a parabola to the $$N$$ points. Detecting the apex from the coefficient of the squared term should be easy.