# CDMA using BPSK - Demodulation

I am very much a rookie, so apologies for the terrible knowledge or misconceptions. I'm looking at this mostly in the context of GPS (or sending a signal with a very low SNR ratio).

I understand BPSK for the most part. Shifting phase by 180 degrees which is essentially just flipping it (atleast in my case).

I am just trying to understand how to do CDMA demodulation. The modulation makes enough sense, essentially just encoding each bit into the spread spectrum code.

But I don't understand how to demodulate it, and all the sources I've been looking at are very vague about it (sources would be greatly appreciated!)

Pretty much what I've gathered is this:

First correlate (multiply and then integrate) the recieved signal with the PRN code. And then apply BPSK to that result.

And my understanding of it is that the PRN code is going to cause the signal to flip at the appropriate intervals. If you essentially just...apply it again, then it's going to undo the effect it had in the first place. And leave you with the BPSK signal.

The other thing I'm thinking it might be is that rather than directly correlating with the PRN, you correlate with a wave copy. This copied wave would just be the carrier wave following the PRN code. And then any correlations should perfectly line up.

But I couldn't get either of these to work very well in the presence of noise!

What I'm curious about doesn't even necessarily have to work in the presence of other transmitted signals overlaid on top. Just the one signal is good right now. I guess I'm just not understanding how GPS can work so well in the presence of noise.

GPS C/A uses Gold codes that are each 1 ms long (1023 chips of a pseudo-random sequence) and transmits data by sending the code directly 20 times (20 ms) to send a data "1" bit, or completely inverted 180 degrees 20 times to send a zero "bit". The BPSK part simply means the carrier (such as L1 at 1575.42 MHz) is either transmitted in phase or out of phase depending on each individual chip. To demodulate, we multiply by the synchronized carrier and multiply by the synchronized code sequence (and that can be done in either order) and optimally in the case of white noise and, the result of the product is integrated (summed) over the data symbol duration (here 20 ms).

The multiply and accumulate process (which is a correlation) provides the maximum SNR in the demodulated result but requires the received signal to be sufficiently stationary over the 20 ms integration time. Reducing the integration time will reduce the SNR result but also reduce the requirements on signal quality (for example, a noisy sampling clock or local oscillator can degrade the duration for which the signal can be assumed stationary). It is very easy to acquire the GPS signal typically received in just the 1 ms correlation time, so I recommend starting with that, and then once acquired increase the time duration up to the 20 ms maximum interval and observe the recovered SNR - this will also provide a measure of the local signal quality.

Understanding how CDMA such as GPS "pulls signals out of the noise floor" can be understood by first understanding what happens when we add $$N$$ samples of uncorrelated noise together vs multiple correlated samples I'll refer to as the signal: The standard deviation of the signal $$\sigma_s$$ will increase by $$N$$, while the standard deviation of the noise $$\sigma_n$$ increase by $$\sqrt{N}$$. SNR in dB is $$20\log{10}(\sigma_s/\sigma_n)$$ so the net increase in SNR, which is referred to as "Processing Gain" is $$10\log{N}$$.

I like to use the short Barker Code 11 chip sequence [1,0,1,1,0,1,1,1,0,0,0] as a quick intro to understanding Direct Sequence CDMA (which is what GPS is), how to modulate it, spread it, demodulate and depsread it (without getting into all the deeper receiver details of synchronization). The short Barker Code is so convenient as you can quickly test on your own on a napkin to convince yourself of one of it's convenient properties: "Correlating" samples is the process of multiplying and accumulating (and how we get the Processing Gain described above). Like Gold Codes in GPS, Barker Codes have the property of correlating to very large values when we correlate the code to itself aligned in time, and very small values otherwise. This allows us to use such codes for synchronization and timing.

Modulating to BPSK just means using a "+1" when the chip in the sequence is a "1" and "-1" when the chip in the sequence is a "0". (This can be at any carrier frequency including "DC", so to be simple and consistent with baseband equivalent simulation techniques I will keep everything at DC.) So the BPSK modulated Barker Code given above will then be [1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1]

Consider if we transmitted this sequence repeatably one after another, and in the receiver we perform a sliding correlation, which would appear as follows when our received sequence is code-aligned (in code phase) with the local copy of our code:

If multiplied the sequenced and summed that result (correlation), each product would be 1 and the sum would then be 11. So the signal level, which is the amplitude of the code sequence would grow by 11, while the noise on each sample (assuming this signal was "buried in noise") would only go up by $$\sqrt{11}$$ (and this would be the standard deviation of the noise specifically). And thus we would in this case expect to see a processing gain of $$10\log(11)=10.4$$ dB assuming the noise on each sample is independent.

Consider another case when the received sequence is not in code phase with the local copy, as depicted below when we are off by 1 chip:

Now when we multiply the sequences sample by sample, 6 times the product will be -1 when the sequences don't match, and 5 times the product will be 1 when the sequences do match, resulting in a sum of -1. If we continued this resulting in a sliding correlation over all possible offsets (which is one way to demodulate GPS), we would get the following result versus time offset. Every offset results in -1 except when we have code phase alignment:

Python script to replicate this result:

barker = np.array([1,-1,1,1,-1,1,1,1,-1,-1,-1])
# generating a sequence of barker codes in sucession
tx = np.concatenate([barker,barker,barker,barker,barker,barker])
# sliding correlation of the transmitted sequence with the reference code:
result = np.correlate(tx,barker, mode='full')


Below is an animation demonstrating this same result with an actual GPS signal. The top plot shows the received signal after being translated back to baseband (by multiplying with a phase aligned carrier at 1575.42 MHz) being slid past the code. When shift=0 the code is aligned with the received signal. The middle plot is showing the resulting sample by sample product of the received sequence with the code in blue along with the average of these samples in green. The third plot shows the average alone showing that we can clearly distinguish when a code alignment occurs. Watch how the third plot increase significantly as shift moves through zero:

The blue line in the second plot as the sample by sample product still appears very noise like: if we took a histogram of the values, it would very much be a Gaussian bell curve with almost as many samples going positive as going negative. If there was no noise, after the time-aligned product all of the samples would be positive. Because of the underlying demodulated code, buried in that noise, there will be slightly more positive than negative values (the bell curve will be shifted ever so slightly to the right). When we sum all the samples (or equivalently average which only differs by a scaling constant) this reduces or filters out the noise and we will see the result as a higher average value (which is what the third plot is). Specifically the standard deviation as shown by the bell curve mentioned above will reduce by $$\sqrt{N}$$ where $$N$$ is the total number of samples (here 1 sample per chip or 1023).

This is the same result (with a different vertical scale) plotting the correlation result versus time offset over 50 ms, showing 50 correlation peaks for each 1 ms when the code is aligned. The signal prior to correlation is indistinguishable from noise if we had no knowledge of the code!

I mentioned in the first paragraph that GPS sends the C/A code 20 times for each data bit. In the above plot we are correlating over 1 ms (one 1023 chip C/A code sequence interval) and thus see 20 peaks for each data bit, and we see the reversal associated with sending a zero instead of a 1. In this 50 ms capture, we are seeing a portion of the data sequence 1 - 1 - 0!

This demonstrates the most basic operations of despreading CDMA signals and how the signals are "pulled out of the noise". Actual reception requires synchronization such as carrier frequency and phase recovery. I go into greater detail on receiver design practices including carrier and timing recovery for practical implementation in the upcoming online course "DSP for Software Radio" that will be running again in Feb 2024! https://dsprelated.com/courses