# Shouldn't xcorr(a, b) be same as conv(reverse(a), b)?

**URL:** https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975
**Category:** General Usage
**Tags:** dsp
**Created:** [February 10, 2018, 2:13pm UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975 "2018-02-10T14:13:27Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)
#### Post date: [February 10, 2018, 2:13pm UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975/1 "2018-02-10T14:13:27Z")

</div>

When a=[1, 2, 3] and b=[1, 2, 3, 3, 2, 1], yc = conv(reverse(a), b) returns  
[3, 8 14, 17, 15, 10, 4, 1],  
and yx = xcorr(a, b) returns  
[1, 4, 10, 15, 17, 14, 8, 3, 0, 0, 0].

Shouldn’t yx be same as yc?

yx1 = xcorr(b, a) returns  
[0, 0, 0, 3, 8 14, 17, 15, 10, 4, 1]. yx1 seems to be right if it means cross-correlation (a, b) except the leading zeros. Where do these zeros come from?

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [February 10, 2018, 4:39pm UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975/2 "2018-02-10T16:39:22Z")

</div>

The choice of which signal to reverse is just a convention. Julia follows Matlab here, likewise for the zero padding.

The challenge is to document this properly without violating copyright.

---

<div class="post-metadata">

### Author: ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)
#### Post date: [February 11, 2018, 6:37am UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975/3 "2018-02-11T06:37:29Z")

</div>

Thank you for your comment, Ralph.

Using \* and ⦿ for convolution and cross-correlation respectively, I’ve found  
xcorr(a, b) calculates b[p] ⦿ a[p] = b[-p] \* a[p] instead of a[p] ⦿ b[p] = a[-p] \* b[p]. Cross-correlation is not commutative, and b[p] ⦿ a[p] ≠ a[p] ⦿ b[p], of course.

I have to say that this xcorr’s “convention” is unnecessarily confusing, and one has to be very careful when applying this function as cross-correlation. It’d be kind, or almost necessary, to mention this in its manual/document, and I’m afraid I haven’t found such documents yet.

In Mathematica, there’s no such confusion when using ListCorrelate/ListConvolve. (I don’t have access to Matlab, I’m afraid.)

I still don’t understand the reason for the leading zeros…

I didn’t understand the issue on copyright, either… Because it’s just a matter of explaining the order of arguments in the xcorr function in Julia.

– Teru

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 11, 2018, 7:13am UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975/4 "2018-02-11T07:13:49Z")

</div>

> [@suiato](#):
>
> mention this in its manual/document, and I’m afraid I haven’t found such documents yet.

When I find that something is underdocumented or confusing, I usually try to find out the relevant details, then make a PR adding documentation.

In this case, since `xcorr` has been moved to [DSP.jl](https://github.com/JuliaDSP/DSP.jl/blob/8570a71120e67e7790a968e9ab4c8ee3696ae784/src/dspbase.jl#L188-L201), so you could make a PR there. Or at least open an issue.

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [February 11, 2018, 3:36pm UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975/5 "2018-02-11T15:36:56Z")

</div>

I agree that it’s confusing: that’s why I’ve had to look up the Matlab documentation enough times to worry about copyright. Someone without the Matlab background could write out the formula and explain the padding in a PR (as Tamas suggests), based on the current substitute for detailed documentation:

```julia
julia> less(xcorr)

```

---

<div class="post-metadata">

### Author: ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)
#### Post date: [February 12, 2018, 5:17pm UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975/6 "2018-02-12T17:17:08Z")

</div>

> [@Ralph\_Smith](#):
>
> julia\> less(xcorr)

Thanks. Helpful 🙂

> julia\> less(xcorr)  
> if su \< sv  
> u = [u;zeros(eltype(u),sv-su)]  
> elseif sv \< su  
> v = [v;zeros(eltype(v),su-sv)]  
> end  
> flipdim(conv(flipdim(u, 1), v), 1)  
> end
> 
> end # module

I now understand the arrays are zero-padded to be equal length, which caused the leading zeros in the result.

I don’t understand the last line, though… Why is conv(flipdim(u, 1), v) reversed? If the last line were conv(flipdim(u, 1), v), it’d be consistent with a regular definition of cross-correlation.

I’ve looked at a manual of Matlab’s xcorr [here](http://cens.ioc.ee/local/man/matlab/toolbox/signal/xcorr.html), and found that xcorr is not exactly a plain function for cross-correlation. It assumes the arrays are of equal length, and, if not, zero-pad to be equal. It isn’t required in cross-correlation usually. I noticed as well that the definition of cross-correlation written there is somehow a conjugate of a regular cross-correlation. Weird to me…

Even after reading the manual, I don’t see the reason for applying flipdim as mentioned above if the xcorr function in Julia is adopting that in Matlab.

I’m not a Matlab user, but now got curious of the issue and installed a trial version of Matlab… which ended up occupying 17GB of my disk space 😮

– Teru

---

<div class="post-metadata">

### Author: ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)
#### Post date: [February 12, 2018, 11:11pm UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975/7 "2018-02-12T23:11:52Z")

</div>

Thanks for your advice.

Because the issue seems to originate in Matlab rather than Julia, I may have to look for information in the Matlab community.

– Teru

---

<div class="post-metadata">

### Author: ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)
#### Post date: [February 13, 2018, 12:49pm UTC](https://discourse.julialang.org/t/shouldnt-xcorr-a-b-be-same-as-conv-reverse-a-b/8975/8 "2018-02-13T12:49:17Z")

</div>

I tried xcorr in Matlab, and found the same result as in Julia’ xcorr.

– Teru
