Monday 8 June 2015

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



Considering there are two signals and the signals are real time in nature. Visibly when one signal does up the other signal does down and vice versa. Sometimes both the signals moved towards the same direction and sometimes moves away from each other. How to determine the juncture when both the signals change their direction.



Answer



Correlation is a multiply and accumulate process. So to do what you describe, simply multiply the two signals and accumulate (integrate) the resulting output. The integration time is set based on your knowledge of how long the waveforms could be correlated; a longer integration time will give you more processing gain against background noise but will also effect how rapidly you can observe changes in the correlation. If the integration time is longer than the interval over which the correlation changes the resulting correlation will be averaged out (therefore not observable).


Here is a specific approach that would minimize processing required while giving you a new result after every incoming sample, when we can assume synchronous inputs:


Multiply the two inputs, and place the result in a "sliding FIFO buffer" (FIFO = First In First Out). The buffer is of length N where N is the number of samples in our "integration time". This is only for purpose of maintaining what the resulting multiplication was N samples ago, as there is no need to do operations on every element in the buffer on each update or to keep a buffer of the input samples.


For each input, multiply the two and add the resulting product p to the buffer, which causes the last sample in the buffer to "fall off". Add the result to an accumulator register r while subtracting the product from N samples ago (now in the last position in the buffer):


$r(n) = p(n) - p(n-N) + r(n-1)$


Where:


$r(n)$ is the current value to store in the accumulator


$p(n)$ is the product of incoming samples $x(n)$ and $y(n)$



$r(n-1)$ is the previous value that was in the accumulator before this update


$p(n-N)$ is the value of the product from N samples ago (get this from the FIFO buffer).


Thus at any given time r(n) represents the (unnormalized) correlation of x and y over N samples.


Just to note, and not important if you don't understand this part: This is a CIC (Cascade Integrator Comb) implementation of a moving average filter. A block diagram showing this process is shown below:


Correlator processing implementation


The above will give us a result that is not normalized. If a true normalized cross correlation was needed, we would need to scale the result by the number of samples as well as the standard deviation of x and y. Note that same approach can be used to determine the variance for both x and y: Use the same block diagram with x as both inputs to determine the variance of x (since the variance is the sum of the squares divided by N), and similarly y as both inputs to determine the variance of y. In most cases we would want to avoid this if we can be content with a result that is proportional to the correlation given the increased processing requiried due to the need to divide and do square roots (and three of the above block diagrams). If you have to go down this path, look into the Cordic Rotator for computing the square root if you can spare doing multiple operations between each incoming sample, and use an N that is a power of 2 so that the divide by N is a simple bit shift.


No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...