  21cm tomography promises to be a powerful tool for estimating cosmological
parameters, constraining the epoch of reionization, and probing the so-called
dark ages. However, realizing this promise will require the extraction of a
cosmological power spectrum from beneath overwhelmingly large sources of
foreground contamination. In this paper, we develop a unified matrix-based
framework for foreground subtraction and power spectrum estimation, which
allows us to quantify the errors and biases that arise in the power spectrum as
a result of foreground subtraction. We find that existing line-of-sight
foreground subtraction proposals can lead to substantial mode-mixing as well as
residual noise and foreground biases, whereas our proposed inverse variance
foreground subtraction eliminates noise and foreground biases, gives smaller
error bars, and produces less correlated measurements of the power spectrum. We
also numerically confirm the intuitive belief in the literature that 21cm
foreground subtraction is best done using frequency rather than angular
information.
