# CE147 - Signal Matching using Coherence Function Cross Spectral Density

#### Microchip Technology

In this example, ADC is configured to simultaneously sample two signals using AN0 and AN1 through channel 0 and channel 1 at 250KHz. The two discrete signals thus sampled and discretized are assembled in to two 512 element buffers and are spectrally matched with each other to find by how much percentage the two signals match continuously over time.

## About this Code Example

In this example, ADC is configured to simultaneously sample two signals using AN0 and AN1 through channel 0 and channel 1 at 250KHz. The two discrete signals thus sampled and discretized are assembled in to two 512 element buffers and are spectrally matched with each other to find by how much percentage the two signals match continuously over time. The percentage match is displayed on the LCD screen on Explorer 16 development board.

When there are two random time series signals (x1 and x2) and we want to find the degree of match between them, the coherence metric is computed from cross spectral density function (CSD) of two signals. Coherence is given by,

g(x1,x2,f) = S(x1,x2,f)/sqrt[S(x1,x1,f)*S(x2,x2,f)]

where g is the coherence function; S(x1,x2,f) is the cross spectral density between x1 and x2; S(x1,x1,f) and S(x2,x2,f) are autospectral densities of x1 and x2.

In practice, since we have time limited random signals the magnitude square coherence is used. It is given by,

C(x1,x2,f) = square(|g(x1,x2,f)|) = square( |sum(X1(f) * conj(X2(f)))| )/( square(sum(square(|X1|))) * square(sum(square(|X2|))) )

where C is the magnitude square coherence, X1 and X2 are disctrete fourier transforms of x1 and x2.

Timer 3 is setup to time-out every 4 microseconds (250KHz Rate). As a result, the module will stop sampling and trigger a 10-bit A/D conversion on every Timer3 time-out, i.e., Ts=4us. At that time, the conversion process starts and completes Tc=12*Tad periods later. When the conversion completes, the module starts sampling again. However, since Timer3 is already on and counting, about (Ts-Tc)us later, Timer3 will expire again and trigger next conversion.

ADC module clock time period is configured as Tad=Tcy*(ADCS+1)= (1/40M)*4 = 100ns. Hence the conversion time for 12-bit A/D Conversion Time Tc=12*Tad = 1.2us.

## Categories

## Release History

- Jul 22, 2011
### Version: 1.0.0

First upload to ECS

- Current Version:1.0.0
- Created:Jul 22, 2011
- Updated:Feb 26, 2015
- Downloads:4445
- MPLAB Version:latest
- C Compiler:MPLAB C30
- Development Tools:Explorer16 Dev Board with dsPIC33FJ256GP710 controller
- Supported Devices:dsPIC33F