sehHeiden
Is there a cross correlation function in Nx?
If not I would try to implement it, but still have some problems with Nx.
I wanted to start with this funtion:
defmodule CrossCorrelation do
import Nx.Defn
defn inner(x, tensor1, tensor2) do
length1 = elem(Nx.shape(tensor1), 0)
length2 = elem(Nx.shape(tensor2), 0)
f = tensor1[0..(length1 - length2 + x - 1)]
g = Nx.reverse(tensor2[0..(length1 - length2 + x - 1)])
Nx.multiply(f, g)
end
end
For example, I could run the line f = t1[0..(length1 - length2 + x - 1)] outside the function, but not with it. I assume, its because the terms in the brackets are a tensor and not a number, but Nx.to_number(cannot be called in defn). How do I solve that?
2) How do I broadcast, vectorize the function, so that I call it with: `result_tensor = inner(x_tensor, tensor1, tensor2)? Because I have to sum the result_tensor for cross correlation.
Next step would be, to use the inner function for the cross correlation. Last step to find the optimal offset.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
billylanchantin
I didn’t see cross correlation implemented in Nx. However, I believe cross correlation can be implemented in terms of convolution, which is implemented.
(If your tensors are real-valued, you can skip
conjugate.)This is a bit of a drive-by response. I’m leaving the implementation unspecified, though hopefully that can get you started.
Also if I’m incorrect about the cross corr. being equivalent to convolution, someone please correct me!
polvalente
Also, I believe that the definition of convolution in Nx is such that you don’t need to apply reflection to get the correct orientation of the cross-correlation. Feel free to open an issue so we can discuss this further.
sehHeiden
Okay, after finding out, that cross corrrelation does not exists. One of the first thoughts of mine were convolution. But I did not try further because it needs three dimension.
@billylanchantin because you said I could use conv I tried it, with adding the needed dimensions.
I tried three things, computing on paper, on elixir and python.
I have no complex data.
In elixir I tried:
Returns 35, which is what also computed by hand.
On the other hand with numpy
Which is strange. Because it seams that array was has not be reversed, but the convolve is the same as I expected. But in the end. But functions can compute the all offsets with the
mode=full option.I tried to implement this behaviour with:
But the I could not test it, as the function full_cross_corr raises breaks with the error:
** (ArgumentError) cannot invoke to_binary/2 on Nx.Defn.Expr.From within the Nx.to_number function.But not to capsule x with Nx.to_number throws:
So x is tensor of type integer with value 0. But what is the problem with Nx.to_number than?
polvalente
This implementation isn’t advisable. Please open an issue so we can discuss implementing cross_correlation in Nx itself.
general problems:
polvalente
In your issue, please describe in general what you expect that the cross correlation function should do, with example inputs and their corresponding outputs.
If there’s similar functionality in Jax or PyTorch, a link would be helpful!
I’m generally familiar with cross-correlation, convolution and so on, but there are many conflicting definitions for those mathematical operations.
sehHeiden
I think, I can now better articulate a problem I have with Nx.conv.
In Nx I added
|> Nx.reshape({1, 1, 9})I just compared the results from Numpy and Nx:
Should is that correct? Should I open a ticket?
I updated my implemention. Because I had some problem with Nx. I used more standard Elixir.
Problem I have is: that the result is good when compare the simple autocorrelation with the Tensor [[[1, 2, 3, 4, 5]]].
CrossCorrelation.full_cross_corrcompares good tonumpy.correlate(t1, t2, mode=full). But for the more realistic, time series t1 and t2. The result differs greatly.I do not know, whether I did something wrong, or its something different. There is not something similar to
numpy.correlate(t1, t2, mode=full)yet in Nx, right?P.S.: We wrote at the same time. I added some links the table.
I also wanted to add some test results:
numpy:
Returns:
0.03,0.07,0.15,0.29000000000000004,0.46,0.6300000000000001,0.8699999999999999,1.1900000000000002,1.6,1.9699999999999998,2.1300000000000003,1.9500000000000002,1.48,0.9099999999999999,0.44,0.15,0.03
My Elixir implementation:
returns:
[0.030000001192092896, 0.15000000596046448, 0.4700000286102295, 0.9600000381469727,
1.3200000524520874, 1.4800000190734863, 1.5399999618530273, 1.5699999332427979, 1.600000023841858,
1.5699999332427979, 1.4500000476837158, 1.1299999952316284, 0.6399999856948853, 0.2800000309944153,
0.12000000476837158, 0.06000000238418579, 0.030000001192092896]
polvalente
Yeah, so this is one of the issues I meant with conflicting definitions.
Mathematically, convolution and correlation are the same idea, a series of sliding inner products, with the difference being that in one of those you reflect one of the tensors and in the other, you don’t.
The thing is, in computational graphics, the convolution operation tends to not reflect the kernel, so it really is a correlation. This is why you’re getting “swapped” results there.
I believe if you use
Nx.reverseon the second operand (the kernel) and usepadding: :samefor the convolution, you’ll get the results you’re looking for.polvalente
Ok, so after some analysis, the call you’re looking for needs to add some zero-padding to your tensors:
If you look closely, the result from convolving with
Nx.conv(..., padding: :same)is contained in the middle of your results from numpysehHeiden
Yes, it is! I also saw, that same padding: returns the inner part of what I wanted.

Nx.conv^^Update:
@polvalente which your help I could solve the problem. Thanks alot!
polvalente
I think the padding used can be size t2 - 1, not sure about that. You’d have to check what happens when the tensors are of different sizes. It might actually work to do size1 + size2 - 1 on a single side only.
As for adding the axes, you can use Nx.new_axis