Pitch Detection: avoiding frequency doubling / halving
so Adamski, what you have here is what we call "the Octave Problem" and is well-known and oft discussed with people who work on the pitch detection problem.

first, some definitions.  given a quasi-periodic signal, $x(t)$, where

$$ x(t) \approx x(t+T) $$

in some local region where $t \approx t_0$, we normally define "pitch", measured in octaves as the base-2 logarithm of the local fundamental frequency, $f_0$, of a note relative to some reference frequency $f_R$.  "pitch" is always about an interval, which means it's expressed relative to something.

$$ p = p_R + \log_2 \left( \frac{f_0}{f_R} \right) $$

the local fundamental frequency is the reciprocal of the local period

$$ f_0 \triangleq \frac{1}{T}$$

so if $p = p_R$ it's because the fundamental frequency $f_0$ is equal to the reference frequency $f_R$.  sometimes we use A440 as the reference, most often i use MIDI note 0 as the reference and measure pitch in semitones which are $\frac{1}{12}$ octave.  in the MIDI convention, middle C is MIDI note 60 and A440 is MIDI note 69.  then the definition of pitch (relative to MIDI note 0) is

$$ \begin{align}
p &amp; = 69 + 12 \log_2 \left( \frac{f_0}{440 \text{Hz}} \right) \\
&amp; = 0 + 12 \log_2 \left( \frac{f_0}{8.1758 \text{Hz}} \right) \\
\end{align} $$

so when $f_0$ = 8.1758 Hz (5 octaves below middle C), the pitch is "0" and when $f_0$ is an octave above that, the pitch is "12".

alright, so the basic issue regarding the octave problem is this:

(1)  given that $x(t)$ is fully periodic with period $T$, that is

$$ x(t) = x(t+T)  \quad \quad \forall t $$

then the same is also true that $x(t)$ is periodic with period $2T$ or $3T$ or any integer multiple of $T$.  that is easy to show.  the same is nearly true for quasi-periodic $x(t)$, so there are many candidates of possible periods $T$ to choose from, which one do we choose?  usually the candidate period $T$ that is smaller than all the other candidate periods is the one we choose.  in other words, A440 (A just above middle C) sounds like A440 with period $\frac{1}{440}$ second, but mathematically it is also periodic with period $\frac{2}{440}=\frac{1}{220}$ second, but we don't hear it as A220 (the A just below middle C).

(2)  now here's the rub:  suppose we add just a teeny bit of a 220 Hz waveform to our A440.  but this 220 Hz waveform is, say, 60 dB lower in level than the A440.  what are we going to hear and, mathematically, what is the period of this waveform?

so, your assignment for the day is to think about that and, maybe tomorrow i'll come back and tell you maybe one or two "secrets" (they belong to me, so i think i can tell them, but lacking contracting with me to develop an alg for you, i won't be too explicit) about how to deal with this, but you first have to understand the nature of the Octave Problem which you get a hint of by answering for yourself the question in (2).

stay tuned.