# How to decode a Barker code

Let's suppose that I send the following sequence of bits:

$$x = [1, 0]$$

I use a 7 length Barker code ($$b = [−1,−1,−1,1,1,−1,1]$$) in a BPSK modulation scheme, resulting in the following sequence:

$$x_B =\\ [−1,−1,−1,\:\:1,\:\:1,−1,\:\:1\\ \:\:\:1,\:\:\:1,\:\:1,-1,-1,\:\:\:1,-1]$$

We send it, through a AWGN channel, and after decoding there are some errors in the sequence: $$y_B =\\ [\:\:1,−1,\:\:1,\:\:1,\:\:1,−1,\:\:1\\ \:\:\:1,-1,\:\:1,-1,-1,\:\:\:1,-1]$$

How would I decode this sequence?

Under the white Gaussian noise assumption, the optimal decoder would be to minimize each window of $$7$$ bits, that is take you first 7 symbols $$s_1 = [\:\:1,−1,\:\:1,\:\:1,\:\:1,−1,\:\:1]$$ and compute two metrics $$c_0 = \Vert s_1 - b_0 \Vert_2^2 = 2^2 + 2^2 + 2^2 + 2^2 + 2^2 = 20$$ $$c_1 = \Vert s_1 - b_1 \Vert_2^2 = 2^2 + 2^2 = 4$$ where $$b_1 = [−1,−1,−1,1,1,−1,1]$$ is the Barker code corresponding to $$1$$ and $$b_0 = [1,1,1,-1,-1,1,-1]$$ is the Barker code corresponding to $$0$$. So if $$c_0 < c_1$$ then the first received bits is $$0$$, else $$1$$.
Next, grab the other $$7$$ symbols $$s_2 = [1,-1,1,-1,-1,1,-1]$$ and do the same thing.