# CIC Filter implementation for Normalized Correlation

This is a follow-on to the original post ...

I am trying to implement real-time correlation of two signals. I noticed the CIC Filter discussion here:

How to find correlation/cross-correlation of two signals in real time?

... however I need to use a normalized correlation for detection. What would the complete block diagram or equation be for a normalized CIC Filter?

Additional: I don't have to use the CIC - it just appeared to be computationally cheap and simple to implement. Would a moving window sum of the signal product, divided by the product of stdevs (to normalize) also work?

I.e., if I took the most recent N samples of each signal, and computed the normalized cross correlation, would that be the typical approach?

Follow-on: I have experimented with various ways of measuring signal similarity, and am getting some odd results. My scenario is two signals, X and Y, aligned in time. The X series counts from 0 to 20 over 21 samples (N). The Y series counts from 20 to 0.

I calculate the Pearson product-moment correlation coefficient over the entire series and get an R value of '-1', as expected. But since I'm interested in getting (near) real-time indication of similarity, I implemented the CIC filter approach, as discussed below, and normalized the value by dividing by the square root of the product of two other CIC filters (that each square the X and Y inputs, respectively).

So I am computing the normalized cross correlation of X and Y, using a 'window' of 3, by:

R(normal) = Rxy / (Rxx * Ryy)^0.5, using three CIC structures suggested by Boschen in:

How to find correlation/cross-correlation of two signals in real time?

Note I am not scaling this by N, as suggested by the answer below (I'm still not clear on this).

Now the problem ... the normalized value I get is '+1', not '-1'. Also, the result using the three CIC filter structures seems to be sensitive to when the data series is near, or crosses, zero. My guess is I'm still not implementing this correctly ... or is the CIC approach just sensitive this way?

As a further experiment, I implemented a 'windowed' Pearson correlation calculation, computing normalized value 'R' for the most recent 3 samples, and obtained correlation values near '-1', as expected, and it does this consistently regardless of whether the data series is near or crosses zero.

The CIC approach would seem to be preferred as a faster computation, but is giving inconsistent results. • The CIC example from that answered was an implementation of a moving average filter, not for a generic correlator (but it is a very clever way of getting a correlator output). could you define more closely what you need? Sep 13, 2019 at 19:03
• I need to monitor two (or more) sensor signals, and evaluate their normalized cross correlation in real time. I am using correlation as a means of determining signal similarity. The real time requirement is key. Sep 13, 2019 at 19:27
• ah, OK, if you can assume your two streams to be aligned already, so that the output you get is the correlation coefficient (for fixed zero shift), then all you need for normalization is to divide by the energy of the sequence. Let me type that down as answer. Sep 14, 2019 at 14:20

So, assuming you already know that the two streams you're multiplying point-wise are aligned, and you want the output of your system to be the correlation coefficient of zero shift for the length of the CIC-built averager, this is the way to go.

So, the output of your CIC will be the sum of all point-wise products of the last N samples flying through.

Now, if either input stream becomes overly "strong", that product might become very large, regardless of whether the correlation is actually strong or not.

To avoid that, you'd usually just divide by the square root of the product of energies of both sequences. Assuming the signals are stochastic stationary signals, that would be the product of the square roots of N times the variance, which is simply N times the product of the standard deviations. If you have knowledge of these, a simple multiplication of an input or the output by 1/(N·std1·std2) would do.

Assuming you have no knowledge, you can measure: Just use the same CIC structure on the two squared inputs (instead of their product) to get the instantaneous energy, and divide the result of your CIC correlator by the square root of that.

• So if the two signals were X and Y, and one used three CIC structures such that the output of the first, A, was a result of point-wise product of X and Y, the output of the second CIC, B, the product of X and X, and the third, C the product of Y and Y ... would the normalized cross correlation be A/(BC), or A/(NBC) ? Sep 14, 2019 at 14:45
• it'd be A/(BC), because that's the division by the energies Sep 14, 2019 at 14:54
• @Richard just found one mistake: needs to be the square root, because in case B=C, you want the overall result to be 1 Sep 14, 2019 at 16:47