Cross-Correlation and Match Filters
by Shehrzad Qureshi


Listing One
#include "ipp.h"
bool xcorr(Ipp32f *x, int nx,   // x[n]
           Ipp32f *y, int ny,   // y[n]
           Ipp32f **r, int *nr, // rxy
           int *startLag)      // 1st lag we compute 
{
    // this function assumes that x & y are of power-of-2 lengths!
    // the lags we care about
    int lagLo = -(nx-1),
        lagHi = ny-1;
    // # of elements in the output array
    *nr = lagHi-lagLo+1;
    // allocate output array (use Intel SP function for mem allocation)
    *r = ippsMalloc_32f(*nr);
    IppStatus st = ippsCrossCorr_32f(x, nx, y, ny, *r, *nr, lagLo);
    *startLag = lagLo;
    return (st==ippStsNoErr);
}

Listing Two
int matched_filter(Ipp32f *x, int nx, // x[n]
                   Ipp32f *y, int ny) // y[n]
{
    // matched filter, return value is the lag where
    // the maximum cross-correlation by sequences x and y occurs.
    // both of the following to be initialized by the function xcorr()
    Ipp32f *crossCorrelation = 0;
    int nxcorr = 0,
        startLag = 0;
    // perform the cross-correlation of x[n] & y[n]
    xcorr(x, nx, y, ny, &crossCorrelation, &nxcorr, &startLag);
   // | crossCorrelation |
    ippsAbs_32f_I(crossCorrelation, nxcorr);
    // max value of | crossCorrelation |
    int maxLag = 0;
    Ipp32f maxCrossCorr = 0.0;
    ippsMaxIndx_32f(crossCorrelation, nxcorr, &maxCrossCorr, &maxLag);
    return (maxLag+startLag);
}

Listing Three
function [l, rxy] = cross_corr(x, y)
%
% CROSS_CORR   compute the cross-correlation of two sequences
%    [lags, rxy] = CROSS_CORR(x,y) returns the cross-correlation
%    of x[n] and y[n] in rxy.  The output l contains the
%    lag indices.
%
% time-reverse x[n]
x = fliplr(x);
% construct lag indices
l = 1-length(x):length(y)-1;
% use built-in Matlab function conv to perform cross-correlation
rxy = conv(y, x);






2


