Signal detection
Let $r[n]$ be a noisy signal that is either:
$$ H_0: R[n] = W[n] $$
$$ H_1: R[n] = s[n] + W[n] $$
where $s[n]$ is the signal that we are trying to detect, and $W[n]$ is an i.i.d. zero-mean Gaussian process with variance $\sigma^2$.
The maximum a posteriori rule can be written as:
$$ \frac{f(r[0], r[1], \dots, r[L-1] | H_1)}{f(r[0], r[1], \dots, r[L-1] | H_0)} \overbrace{\gt}^{'H_1'} \underbrace{\lt}_{'H_0'} \frac{p_0}{p_1}$$
Given that $W[n]$ is Gaussian, this can be rewritten as:
$$ \frac{\prod_{n = 0}^{L - 1} \left( \frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(r[n] - s[n])^2}{2 \sigma^2}} \right)}{\prod_{n = 0}^{L - 1} \left( \frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(r[n])^2}{2 \sigma^2}} \right)} \overbrace{\gt}^{'H_1'} \underbrace{\lt}_{'H_0'} \frac{p_0}{p_1} $$
After some simplifications, we get:
$$ g = \sum_{n = 0}^{L - 1} r[n] s[n] \overbrace{\gt}^{'H_1'} \underbrace{\lt}_{'H_0'} \sigma^2 \ln \eta + \frac{\varepsilon}{2} = \gamma $$
where $\eta = \frac{p_0}{p_1}$ and $\varepsilon = \sum_{n = 0}^{L - 1} s^2[n]$ (Energy)
Let $G$ be the random variable of which $g$ is a realized value. Similarly, $R[n]$ is the random process of which $r[n]$ is a realized instance. Then,
$$ G = \sum_{n = 0}^{L - 1} R[n]s[n] $$
The distributions of $G$ are:
$$ H_0: G \sim \mathcal{N}(0, \sigma^2 \varepsilon) $$ $$ H_1: G \sim \mathcal{N}(\varepsilon, \sigma^2 \varepsilon) $$
Note that the variance is the same in both cases.
Matched filter
A matched filter is used to detect a known signal $s[n]$ in white Gaussian noise.
The filter is the time reverse of the signal:
$$ h[n] = s[-n] $$
In the frequency domain:
$$ H(e^{j\Omega}) = S(e^{-j\Omega}) = |S(e^{j\Omega})| e^{-j\angle S(e^{j\Omega})} $$
Consider filtering a noisy signal $r[n]$ with the matched filter $h[n]$:
$$ g[n] = (h \ast r)[n] = (\overleftarrow{s} \ast r)[n] $$
In the ideal case where $r[n] = s[n]$, the output is deteministic autocorrelation:
$$ g[n] = (h \ast r)[n] = (s \ast \overleftarrow{s})[n] = \bar{R}_{ss}[n] $$
The matched filter maximizes the spread between the $H_0$ and $H_1$ cases.
Compare the $g[n]$ with the threshold $\gamma = \sigma_W^2 \ln \frac{p_0}{p_1} + \frac{\varepsilon}{2}$. If $g[n] > \gamma$, declare $'H_1'$. Otherwise, declare $'H_0'$.
Probability of error
The conditional probability of false alarm is:
$$ P_{FA} = Q\left(\frac{\gamma}{\sigma\sqrt{\varepsilon}}\right) $$
$$ P_M = 1 - Q\left(\frac{\gamma - \varepsilon}{\sigma\sqrt{\varepsilon}}\right) $$
Total probability is:
$$ P_e = p_0 P_{FA} + p_1 P_M $$