When you toggle a pin at a certain rate, the resulting waveform will have a frequency at half of the toggle rate. So if your output waveform was a 8kHz square wave, then your ADC sample rate is actually 16kHz, which explains the factor of 2 error.

Unless your signal has zero mean, I would avoid zeroing the missing samples because that would introduce bias in the estimation of the mean, the power and the spectrum of the signal. Instead, the best estimation of the missing samples is the average value of your time series.

However, using the mean isn't good either. The subset of replaced samples would have a constant value. That would solve the estimation bias problem for the average value (f=0), but not for the remaining spectrum. In other words: the replaced samples don't have the statistical and spectral characteristics required.

How can we improve this? Interpolation of the missing samples is an option.

The subset of interpolated samples will also have the same average value of the signal of interest, along with some variability around it. If the wrong samples are randomly spaced, then the subset of interpolated samples will have a variance (power) similar to that of the signal of interest.

Interpolation implies some degree of smoothing. We can think of that as some kind of low-pass filtering on the subset of interpolated samples. Thus, we could mitigate the interpolation-induced distortion with oversampling.

So far, we've dealt with the problem at sample level. But there are other options. If we are estimating the spectrum using non-parametric methods based on signal segments (Barlett, Welch...), then we could discard the segments containing the wrong samples. Discarding segments will increase the variance of the estimation, but will not introduce estimation bias at any frequency.

## Best Answer

Many MCU implement the acquisition of the signal with a dual switch system.

^{simulate this circuit – Schematic created using CircuitLab}That is, the two switching elements are operated with a single signal, so as to have opposite conduction states.

While a state applies the signal to the capacitor, when the corresponding signal is activated, it stops applying the signal to the capacitor, and the voltage across the capacitor is available to be quantized.

The time during which the switch SW1 is closed, applying to capacitor signal is called the

sample timeortime of opening(\$t_s\$). The time during which the switch SW1 is open and SW2 is closed, is thetime of quantization(\$t_q\$). Usually \$t_q\gg t_s\$