I also feel that the question needs a little bit more clarity but with what is stated so far, I think I can have a go at a response and edit it later if more information is added to the question.
What I'd like to do is...
I do not intend to 'put you on the spot' with my question here: Have you checked how other people have tackled this? It seems that what you are trying to do is get an estimate of received signal quality (?). One of the simplest ways to achieve this is Received Signal Strength Indication (RSSI).
The estimation of both Signal To Noise Ratio (SNR) and Bit Error Ratio (BER) imply the use of a 'reference signal'. We will come back to this.
What's going on is when we get our signal, we multiply it with the previous bit to get a correlation, and from that we sum it with the correlation from the previous bit to get our detection signal. This detection signal is what I'm taking the mean and variance of. If the mid-bit point of this detection signal is above a threshold (0 for now) then we say the bit is a 1, if it's below the threshold, we say it's a 0. I'm taking the mean and variance of all the 1's and the mean and variance of all the 0's, and am trying to come up with an average SNR type quantity.
This sounds like a differential codec (e.g. this one). Two questions emerge naturally here: The first one is, how do you know that the bit your are comparing against was decoded correctly, in order to re-use it to guide the decoding of your next bit (if I am getting the scheme correctly here) and the second question takes us back to RSSI and is "If you are trying to estimate statistics of the received signal timeseries with the objective of estimating the SNR, then why don't you use the RSSI?".
OK, so, let's see how are we going to do this. What is Signal to Noise Ratio?. It is the ratio between the strength of a "desired" signal versus the strength of an "undesired" signal (obviously, in the same context!). What is our "desired" signal here? It is the output of the modulator which is coupled to our medium somehow (antenna, speaker, light, whatever). What is the "undesired" signal here? It is the level of background "noise", undesired perturbations depending on the medium, which distort reception.
So, a way of guessing how bad our reception is going to be would be to "listen" to the background noise when there is NO reception (for some time) and create the N and then compare this with the strength of the signal S when we know that the transmitter is transmitting. Here, we form a ratio between a reference (our background noise) and a signal (received signal strength). The closer these two numbers are, the harder it is to accurately demodulate and eventually decode the signal.
If you absolutely must derive an SNR and a BER estimate, then you can examine the use of pilot signals or sequence. Communication systems that use a carrier signal, usually have a Phase Locked Loop (PLL) to lock on the carrier and track it. To allow for the PLL to achieve "lock", prior to transmitting the actual "information" signal, you can transmit a "dummy" sequence of some length, for the PLL to lock (e.g. listen to this clip). After the PLL "locks", the local oscillator (of the demodulator) is synchronised with the remote oscillator (of the modulator, at the transmitter side) and we can now start demodulating symbols. BUT we now do not know, in this bitstream, where does the actual information starts and where does it end. The pilot sequence may have been 100 symbols long. Maybe the PLL locked after 4 cycles, in which case, we have to throw away 96 "dummy" symbols, but maybe it locked after 50 symbols, in which case we have to throw away 50 "dummy" symbols. The point is that we don't know this 4 or 50. All we know is a line coming out of the PLL that is raised to tell us "I have now achieved lock".
So what we do, after the pilot tone, is to insert a start sequence. For example 01101001100101100
. Once the system detects that, it knows that it can start decoding the actual information.
So, what you can do, is insert a long pilot sequence the structure of which you know (reference signal). For example 10101010101010101010101010101010101010101010101010101[BLOCK_START_SEQUENCE]
. Once your PLL achieves lock, you don't throw away the symbols but you store them until you find the start sequence at which point you stop. Now, the pilot sequence could be 100 symbols long, the PLL achieved lock after 20 which means that we have decoded 80 symbols until the block start sequence. You know that these 80 symbols should be 10101010101010101010
(reference signal) and you count how many of them have been decoded in the right way. For example, 80 symbols decoded, 40 of them decoded right, that is a bit error rate of 0.5. For more information, you might want to see this paper.
Presumably, you can then use the BER in some adaptive scheme to select a strong or weak code. In addition, you might also want to look at equalisation and/or adaptive coding
Hope this helps.