# How to find DC bias of periodic signal from samples that include a non-integer number of periods?

I'm using an ADC to sample a periodic signal and I want to calculate the DC bias of the signal from the samples. The obvious solution is to take the average of the samples. This works fine if there are an integer number of periods of the signal in the sample set. But if the sample set contains an additional fraction of a period then those extra samples can throw the average off a bit.

The signal frequency can vary (from one sample set to another) and is independent of the sampling rate. The sampling is asynchronous to the signal so sampling could begin anywhere in the period of the signal. Assume there is at least a couple periods of the signal in the sample set although it could be many more.

Do you know of a better way to calculate the DC bias of the signal from a sample set that contains a non-integer number of periods of the signal?

Update: For my application this is not a real-time calculation. My device collects a sample set occasionally and I'm trying to do post-processing of the samples for various measurements.

I don't know the period of the signal by any other information. All I have is the sample set.

This is admittedly an XY problem. I want to know the DC bias so I can remove it from the samples (and then I plan to integrate the signal). I'm hoping for a way to improve my original implementation (which is easy for me to understand) before trying something more complicated (for me).

• since this appears to be real time, i guess you can make a DC blocking filter and subtract the output of your DC blocking filter from the input. the net left-overs will be the DC. – robert bristow-johnson Jul 11 '17 at 2:39
• why do you need to know the DC bias? perhaps just to remove (subtract) the DC bias? if so, your question should be about DC blocking filters. – robert bristow-johnson Jul 11 '17 at 2:40
• here is a similar question. – robert bristow-johnson Jul 11 '17 at 2:51
• Do you also know the period of the signal? – msm Jul 11 '17 at 3:38
• @robertbristow-johnson I've updated the question – kkrambo Jul 11 '17 at 14:33

Low-pass filter with DC gain = 1 (or 0 dB).

i s'pose, if you want to be sophisticated and want a rock-solid DC that might vary very little, then the output of your LPF can go into a median filter.

Let's say you have $N$ samples of the periodic signal $\tilde{x}(t)$ whose period is $\tau$ seconds and is sampled at sampling frequency $f_s$ where $\tau \cdot f_s$ is an integer (otherwise the discrete signal wouldn't become periodic).

Obviously, the sampled signal $\tilde{x}[n]$ repeats every $\tau \cdot f_s$ samples. That is $\tilde{x}[m]=\tilde{x}[m+\tau \cdot f_s]$. So the number of complete cycles is

$$K= \left\lfloor\frac{N}{\tau \cdot f_s} \right\rfloor$$

where $\lfloor \cdot \rfloor$ is the floor function that returns the largest integer no greater than the argument.

The DC Bias can be given by

$$\frac{1}{K\cdot\tau\cdot f_s}\sum_{n=0}^{K\cdot\tau\cdot f_s-1}x[n]$$

In case if $\tau \cdot f_s$ is not an integer also a similar approach can be used, except you may need to upsample to a new sampling frequency to make it integer.

If you don't know the period, there are classic approaches (based on autocorrelation, DFT, etc.) to estimate it from a set of samples.