# Inverse of Cross Correlation?

**URL:** https://discourse.julialang.org/t/inverse-of-cross-correlation/64357
**Category:** Signal and Image Processing
**Tags:** question, dsp
**Created:** [July 9, 2021, 2:08pm UTC](https://discourse.julialang.org/t/inverse-of-cross-correlation/64357 "2021-07-09T14:08:57Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [July 14, 2021, 2:09pm UTC](https://discourse.julialang.org/t/inverse-of-cross-correlation/64357/7 "2021-07-14T14:09:08Z")

</div>

A simple example to illustrate this problem using two time signals with different bandwidths, with and without noise added.

```julia
using DSP, FFTW

sweep(t,t0,t1,f0,f1) = (t0 < t < t1) ? sin(2π*(t-t0)*(f0 + 0.5*(f1 - f0)/(t1 - t0)*(t - t0))) : 0.0

Δt = 0.004 # sample period (s)
n = 500
t = 0:Δt:(n-1)*Δt
nl = [0.05, 0.0] # 5% noise level and noise-free
ϵ = 0.1; # white noise for deconvolution
s1 = [sweep.(t,0.5,1.5,8.,64.) .+ nl*(2*rand(n) .- 1) for nl in nl] # add random noise
s2 = [sweep.(t,0.,0.5,8.,36.) .+ nl*(2*rand(n) .- 1) for nl in nl] # add random noise

xc = [xcorr(s1,s2)[n:end] for (s1,s2) in zip(s1,s2)] # xcorr's length = 2*n - 1, cut to length n
f = LinRange(-0.5/Δt, 0.5/Δt, n+1)[n÷2+1:end]
S1 = fft.(s1); S1a = [fftshift(abs.(S1))[n÷2:end] for S1 in S1]
S2 = fft.(s2); S2a = [fftshift(abs.(S2))[n÷2:end] for S2 in S2]
Xc = fft.(xc); Xca = [fftshift(abs.(Xc))[n÷2:end] for Xc in Xc]

# deconvolve Xc for S2
S2_decon = [@. conj(Xc)*S1 / (S1*conj(S1) + ϵ^2) for (S1,Xc) in zip(S1,Xc)]
S2a_decon = [fftshift(abs.(S2_decon))[n÷2:end] for S2_decon in S2_decon]
s2_decon = [real(ifft(S2_decon)) for S2_decon in S2_decon]

# deconvolve Xc for S1
S1_decon = [@. Xc*S2 / (S2*conj(S2) + ϵ^2) for (S2,Xc) in zip(S2,Xc)]
S1a_decon = [fftshift(abs.(S1_decon))[n÷2:end] for S1_decon in S1_decon]
s1_decon = [real(ifft(S1_decon)) for S1_decon in S1_decon]

```

Input signals with and without noise, their cross-correlations and respective spectra:

 ![xcorr_signals_and_spectra](https://global.discourse-cdn.com/julialang/original/3X/e/6/e6ad320a2549ec1d580aca3f739f647e2f2940a8.png)

Deconvolution to obtain narrower band S2 (left with noise, right without):

 ![xcorr_deconvolve_s2](https://global.discourse-cdn.com/julialang/original/3X/6/8/6870a2898f64480a31b68a1f0f3ec78033670802.png)

Deconvolution to obtain wider band S1 (left with noise, right without):

 ![xcorr_deconvolve_s2](https://global.discourse-cdn.com/julialang/original/3X/0/f/0f674a28435e2f49bd381dba66d06ef8bbffc235.png)

---

_[View the full topic](https://discourse.julialang.org/t/inverse-of-cross-correlation/64357)._
