# Detection over AWGN analysis disagrees with Monte Carlo simulation

I have a fixed vector $\mathbf{s} \in \{\pm1\}^m$ and a random vector $\mathbf{d} \in \{\pm1\}^m$.

I send either $\mathbf{x} = \mathbf{x_1} = [\mathbf{s}\space\mathbf{d}]$ or $\mathbf{x} = \mathbf{x_2} = [\mathbf{d}\space\mathbf{s}]$ over an AWGN channel to get $\mathbf{y}$.

Then $\mathbf{x}, \mathbf{y}\in \{\pm1\}^{2m}$.

From the receive vector $\mathbf{y}$, I want to detect which $\mathbf{x}$ has been sent.

Although it is suboptimal, I want to calculate the error probability of correlation detector.

The error probability of correlation detector

$$\mathbb{P}[\epsilon] = \mathbb{P}\{\textrm{declare } \mathbf{x_2} | \mathbf{x_1} \textrm{ sent}\} =\mathbb{P}\left[\sum_{i=0}^{m-1}(s_i+n_i)s_i<\sum_{i=0}^{m-1}(d_i+w_i)s_i\right] \tag{1}$$

where $s_i,d_i \in \{\pm1\}$ and $w_i, n_i \sim \mathcal{N}(0,\sigma^2)$ and they are i.i.d.

\begin{align} \mathbb{P}[\epsilon]&=\mathbb{P}\left[\sum_{i=0}^{m-1}(s_in_i - s_iw_i)< \left(\sum_{i=0}^{m-1}d_is_i\right) -m\right]\\ &=p_\mathbf{D}(\mathbf{d}) \sum_{\mathbf{d}} \mathbb{P}_{\textrm{given } \mathbf{d}}\left[\sum_{i=0}^{m-1}(s_in_i - s_iw_i) < \left(\sum_{i=0}^{m-1}d_is_i\right) -m\right]\\ &=\frac{1}{2^m} \sum_{\mathbf{d}} \mathbb{P}_{\textrm{given } \mathbf{d}}\left[\sum_{i=0}^{m-1}(s_in_i - s_iw_i) < \left(\sum_{i=0}^{m-1}d_is_i\right) -m\right] \tag{2} \end{align}

The random variable $\sum_{i=0}^{m-1}(s_in_i - s_iw_i)$ is Gaussian $\mathcal{N}(0,2m\sigma^2)$. Thus

$$\mathbb{P}[\epsilon] = \frac{1}{2^m} \sum_{\mathbf{d}} \mathrm{F}_z\left( \frac{\sum_{i=0}^{m-1}d_is_i -m}{\sigma\sqrt{2m}}\right) \tag{3}$$

where $\mathrm{F}_z$ is the CDF of $z \sim \mathcal{N}(0,1)$

To check my result, I run a simulation. First I check the conditional probability "given $\mathbf{d}$"

ntest = 1000;
sigma = 1;

m = 10;
s = 2*(rand(m,1) > 0.5) - 1;
d = 2*(rand(m,1) > 0.5) - 1;

nerr = 0;
for i=1:ntest
ys = s + sigma * randn(size(s));
yd = d + sigma * randn(size(s));
nerr = nerr + (yd.'*s > ys.'*s);
end

perr = nerr/ntest;  %simulated error
terr = normcdf((s.'*d - m)/sqrt(2*m)/sigma); %theory


And the values of perr and terr are similar. Good.

Next I want to check the result of (3).

ntest = 10000;
sigma = 1;

m = 10;
s = 2*(rand(m,1) > 0.5) - 1;

nerr = 0;
for i=1:ntest
d = 2*(rand(m,1) > 0.5) - 1;
ys = s + sigma * randn(size(s));
yd = d + sigma * randn(size(s));
nerr = nerr + (yd.'*s > ys.'*s);
end
perr = nerr/ntest;  %simulated error

terr = 0;
for i=0:2^(m-1)
d = (dec2bin(i,m) - '0').';
terr = terr + normcdf((s.'*d - m)/sqrt(2*m)/sigma);
end
terr = terr / 2^m; %theory


Theory terr = 0.0065 while simulated result perr = 0.0318. Very bad !!!

My question is that where were my mistakes ? Is my Equation (3) not correct ? Or my simulation way are wrong ? Or I have been wrong since Equation (1) and (2) ?

Thanks