Q:

Matched filter and cross correlation in digital processing

I am very new to signal processing, and a bit confused with the matched filter. 
Assume that I have a time series and a specific waveform I need to identify in it. By definition:

a matched filter is obtained by correlating a known signal, or
  template, with an unknown signal to detect the presence of the
  template in the unknown signal.

So I take the window from the sequence, generate the test signal with the same sampling rate, cross-correlate them, find the maximum and threshold it.
Is cross-correlating my measured time sequence with the generated waveform a matched filter (for example, using xcorr in matlab), or there is a specific procedure I must follow to produce a "more correct" matched filter.
Thank you.

A:

I'll illustrate with an example: detecting a rectangular pulse in noise, in Matlab.
Let's start with defining the signal that contains the pulse.
fs = 1/1000;
t = 0:fs:1;
s = [zeros(1,400), ones(1,100), zeros(1,501)];

First let's do detection in noise with small power. We calculate the received (noisy) signal:
np = 0.1;  % noise power (variance)
n = sqrt(np)*randn(1,1001); % noise signal
r = s + n; % received signal
plot(t,r); title('Received signal, small noise power');

Now let's run the correlation:
c = xcorr(r,s);
plot(c); title('Correlation, small noise power');

You can see how the correlation produced a large output, indicating that the pulse was there. What is amazing is that the correlator can find the pulse even when it is impossible for one to see it. Let us increase the noise power and repeat the process.
np = 4;
n = sqrt(np)*randn(1,1001);
r1 = s + n;  % received signal with large noise

As you can see, the correlator has an incredible ability to detect signals that are embedded in strong noise.
One thing you can do for yourself is run the correlation when the pulse is not present. The signal will be hard to distinguish from the noisy pulse shown above, but you'll see that the correlation does not produce a sharp maximum.
So, the implementation is quite easy; what is not so easy to do is:

Prove that this technique produces the best possible signal to noise ratio at the correlation's peak.
Determine the optimum threshold given some desired constraints on the probability of wrong detection (false positive or false negative).

