# Cross Correlation

The general form of the cross correlation with integration:
\begin{equation}
{\displaystyle (f\star g)(\tau )\ = \int _{-\infty }^{\infty }{{f^{\star}(t)}}g(t+\tau )\,dt}
\end{equation}
This can be written in discrete form as:
\begin{equation}
{\displaystyle (f\star g)[n]\ = \sum _{m=-\infty }^{\infty }{{f^{\star}[m]}}g[m+n]}
\end{equation}


```python
%pylab inline
pylab.rcParams['savefig.dpi'] = 300
from gps_helper.prn import PRN
from sk_dsp_comm import sigsys as ss
from sk_dsp_comm import digitalcom as dc
from caf_verilog.quantizer import quantize
```

## Test Signals


```python
prn = PRN(10)
prn2 = PRN(20)
fs = 625e3
Ns = fs / 125e3
prn_seq = prn.prn_seq()
prn_seq2 = prn2.prn_seq()
prn_seq,b = ss.NRZ_bits2(array(prn_seq), Ns)
prn_seq2,b2 = ss.NRZ_bits2(array(prn_seq2), Ns)
```


```python
Px,f = psd(prn_seq, 2**12, Fs=fs)
plot(f, 10*np.log10(Px))
```

## Autocorrelation


```python
r, lags = dc.xcorr(prn_seq, prn_seq, 100)
plot(lags, abs(r)) # r -> abs
```

## Time Shifted Signals


```python
r, lags = dc.xcorr(roll(prn_seq, 50), prn_seq, 100)
plot(lags, abs(r))
```


```python
r, lags = dc.xcorr(roll(prn_seq, -50), prn_seq, 100)
plot(lags, abs(r))
```

## No Correlation


```python
r_nc, lags_nc = dc.xcorr(prn_seq, prn_seq2, 100)
plot(lags_nc, abs(r_nc))
ylim([0, 1])
```

## Calculation Space Visualization


```python
from caf_verilog.xcorr import size_visualization
size_visualization(prn_seq[:10], prn_seq[:10], 5)
```

## Simple Cross Correlation


```python
from caf_verilog.xcorr import simple_xcorr
r, lags = simple_xcorr(prn_seq, prn_seq, 100)
plot(lags, r)
```

### Time Shifted Signals


```python
r, lags = simple_xcorr(prn_seq, roll(prn_seq, 50), 100)
plot(lags, abs(array(r)))
```


```python
r, lags = simple_xcorr(prn_seq, roll(prn_seq, -50), 100)
plot(lags, abs(array(r)))
```

### No Correlation


```python
r, lags = simple_xcorr(prn_seq, prn_seq2, 100)
plot(lags, abs(array(r)))
ylim([0, 5000])
```

## Dot Product Method

To ensure the integration time is filled, the secondary or received signal must be twice the length of the reference signal.


```python
from caf_verilog.sim_helper import sim_shift
center = 300
corr_length = 250
shift = 25
ref, rec = sim_shift(prn_seq, center, corr_length, shift=shift, padding=True)
```


```python
f, axarr = plt.subplots(2, sharex=True, gridspec_kw={'hspace': 0})
axarr[0].plot(ref)
axarr[1].plot(rec)
plt.xlabel("Sample Number")
savefig('prn_seq.png')
```


```python
from caf_verilog.xcorr import dot_xcorr
ref, rec = sim_shift(prn_seq, center, corr_length, shift=shift)
rr = dot_xcorr(ref, rec)
rr = array(rr)
```


```python
rxy, lags = dc.xcorr(ref, rec, 1000)
plot(abs(rxy))
xlabel("Center Offset (Samples)")
grid();
savefig('xcorr.png')
```


```python
fig, axs = plt.subplots(2, sharey=True)
axs[0].plot(abs(rr))
axs[0].grid(True)
axs[1].plot(abs(rr))
axs[1].set_xlim([80, 120])
axs[1].set_xlabel('Inverse Center Offset (Samples)')
axs[1].grid(True)
fig.savefig('xcorr_250.png')
```


```python
argmax(rr)
```


```python
from caf_verilog.xcorr import XCorr
xc = XCorr(ref, rec, output_dir='.')
```


```python
xc.gen_tb()
```

507802954661af1830cf08109d15775fb8f3f479
ipynb
Jupyter Notebook
docs/source/nb_examples/XCorr.ipynb
chiranthsiddappa/caf_verilog
cd3cfd00459dc03518fcce53d5d6ac5194fb2adc
MIT
2019-06-04T22:05:12.000Z
2019-06-04T22:05:12.000Z
docs/source/nb_examples/XCorr.ipynb
chiranthsiddappa/caf_verilog
cd3cfd00459dc03518fcce53d5d6ac5194fb2adc
MIT
2019-04-17T17:21:42.000Z
2019-09-11T16:15:28.000Z
docs/source/nb_examples/XCorr.ipynb
chiranthsiddappa/caf_verilog
cd3cfd00459dc03518fcce53d5d6ac5194fb2adc
MIT
Qwen/Qwen-72B
1. YES
2. YES