Correlation-based pattern detector.

Namespace: dsp::fourier

Prototype

sptr<Detecteur> detector_new(const DetectorConfig &config=DetectorConfig())

Parameters

configConfiguration structure

Returns

Filter cfloat \(\to\) float, that take as input samples, and produces as output, the normalized correlation with the pattern.

Description

This block will compute the normalized correlation between a streaming signal \((x_k)\) and a fixed pattern \((h_k)\) of dimension \(M\) : \[ y_n = \frac{\left|\displaystyle{\sum_{k=0}^{M-1} x_{n+k-(M-1)} \cdot h_{k}^\star}\right|}{\displaystyle{ \sqrt{\left(\sum_{k=0}^{M-1}\left|x_{n+k-(M-1)}\right|^2\right) \left(\sum_{k=0}^{M-1}\left|h_k\right|^2\right)}}} \]

The \(y_n\) are thus between 0 and 1, and will be equal to 1 only if the signal is exactly equal to the pattern (up to some scale factor) on the interval \([n-M+1\dots n]\).

The computing being done in the frequency domain through the Overlap-And-Add technique (see filter_fft()), the complexity is of order \(\log_2 M\) operations per sample (if \(M\) is a power of 2).

This block will:

  • Return, as output, the correlation signal (green signal in the example below),
  • Call a user callback whenever the correlation is above a configurable threshold. As parameters of this callback, the following informations are given (see the structure Detection):

    • Peak value of the normalized correlation,
    • Real gain of the sinal (amplitude ratio between the received signal and the expected pattern),
    • Relative phase of the received signal (compared to the expected pattern),
    • Noise standard deviation,
    • Estimated SNR.

Example

  soit M = 512;  // Dimension du motif
  soit N = 16*M; // Dimension du signal

  // Motif de synchronisation
  soit motif = sigchirp(0.001, 0.2, M, 'q');

  // Signal = bruit
  soit x = (0.1 * randn(N)).as<cfloat>();

  // + motif à détecter au milieu
  x.segment(900, M) = motif;

  soit pos_detection = 0;

  DetecteurConfig config;
  config.motif  = motif;
  config.Ne     = M;
  config.gere_detection = [&](const Detection &det)
  {
    msg("Detection : {}.", det);
    pos_detection = det.position;
  };
  soit det = détecteur_création(config);
  soit y = det->step(x);

  Figures f;
  f.subplot().plot(real(motif), "", "Motif à détecter");
  f.subplot().plot(real(x), "", "x");
  f.gcf().def_rdi({0, -1, 8000, 2});

  f.gcf().canva().set_couleur(tsd::vue::Couleur::Vert);
  f.gcf().canva().set_epaisseur(3);
  f.gcf().canva().set_remplissage(non);
  f.gcf().canva().rectangle(pos_detection, 1, pos_detection + M - 1, -1);