Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guide fft #1559

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open

Guide fft #1559

wants to merge 38 commits into from

Conversation

ndrean
Copy link
Contributor

@ndrean ndrean commented Nov 13, 2024

First commit

Copy link
Contributor

@polvalente polvalente left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going in a great direction. We might want to separate the DFT part onto an NxSignal livebook instead, but let's keep working on this as a whole here before we decide!

@@ -258,7 +258,7 @@ The picture below will help to understand the aggregations.

<!-- livebook:{"break_markdown":true} -->

$$
```math
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't rendering properly inside livebook anymore unfortunately. Please undo the ```math changes

{:nx, "~> 0.9.1"},
{:kino, "~> 0.14.2"},
{:kino_vega_lite, "~> 0.1.11"},
{:exla, "~> 0.9.1"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use EXLA here since it's a simpler example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was an excuse to put the correct way to use EXLA as a backend. Up to you!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove it

nx/guides/advanced/complex_fft.livemd Outdated Show resolved Hide resolved
nx/guides/advanced/complex_fft.livemd Outdated Show resolved Hide resolved
nx/guides/advanced/complex_fft.livemd Outdated Show resolved Hide resolved

Let's visualize our incoming signal over 400ms. This correspond to 2 periods of our 5Hz signal.

We compute 200 points to have a smooth curve, thus every (400/200=) 2ms.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We compute 200 points to have a smooth curve, thus every (400/200=) 2ms.
We'll sample 200 equidistant points to so we can see a smooth curve (i.e. every 400ms/200 = 2ms, or a sampling frequency of 500Hz).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll sample 200 equidistant points to have a smooth curve, every 400ms/200 = 2ms, or equivalently with a sampling frequency of 500Hz.

nx/guides/advanced/complex_fft.livemd Outdated Show resolved Hide resolved
nx/guides/advanced/complex_fft.livemd Outdated Show resolved Hide resolved
nx/guides/advanced/complex_fft.livemd Outdated Show resolved Hide resolved
nx/guides/advanced/complex_fft.livemd Outdated Show resolved Hide resolved
ndrean and others added 23 commits November 21, 2024 20:01
@ndrean
Copy link
Contributor Author

ndrean commented Nov 21, 2024

I wonder why FFT and conv are in Nx and not in NxSignal ??

Now that this aperitif is almost done, I feel it is more interesting to play with the convolution, which most probably uses FFT, I will (try to) check. I read the doc, pretty terse. You have things like padding and sliding windows with strides but not sure how it wroks. I would build a little example for example applied on a simple and small image and use 3 or 4 kernels (say horizontal edge, vertical edge, gaussian blur). I don't think its huge, not even sure if the image will render correctly. Is this interesting for you? I may need examples to see and test what this function does exactly. MAybe if you point me directly where they are, could save me time.

@polvalente
Copy link
Contributor

I wonder why FFT and conv are in Nx and not in NxSignal ??

Now that this aperitif is almost done, I feel it is more interesting to play with the convolution, which most probably uses FFT, I will (try to) check. I read the doc, pretty terse. You have things like padding and sliding windows with strides but not sure how it wroks. I would build a little example for example applied on a simple and small image and use 3 or 4 kernels (say horizontal edge, vertical edge, gaussian blur). I don't think its huge, not even sure if the image will render correctly. Is this interesting for you? I may need examples to see and test what this function does exactly. MAybe if you point me directly where they are, could save me time.

Awesome. I will ping you regarding convolutional filters, as I've just given a talk on that! This is the repo if you want to take a look already: https://github.com/polvalente/code-beam-lite-nyc-2024

Regarding fft and conv being in Nx, ultimately the reason is that they are present in the XLA API, really.

And NxSignal is a library that currently only implements things through calling Nx itself. Think of Nx as being analogous to NumPy, while NxSignal is a subsection of SciPy.

Finally, conv is an operation in itself and can be implemented in the time domain. Nx doesn't build it on top of FFT.

@ndrean
Copy link
Contributor Author

ndrean commented Nov 21, 2024

Ah! I recall I had a little home work on cyclotomic polynomials and FFT for polynomial products (a convolution in fact) but was a long time ago.

So if you already did it, I can only will bring in a kinda noob POV! Thanks for the link, I will run it.

@polvalente
Copy link
Contributor

Ah! I recall I had a little home work on cyclotomic polynomials and FFT for polynomial products (a convolution in fact) but was a long time ago.

So if you already did it, I can only will bring in a kinda noob POV! Thanks for the link, I will run it.

The talk wasn't recorded, and putting it in a livebook format would be great!

To be clear, FFT can execute the convolution (with some restrictions) but it's important to keep in mind that each is its own primitive in Nx

@ndrean
Copy link
Contributor Author

ndrean commented Nov 21, 2024

In Scipy, I just had a look, there make some quick calculations to predict which method, direct or via FFT, is faster.

https://github.com/scipy/scipy/blob/92d2a8592782ee19a1161d0bf3fc2241ba78bb63/scipy/signal/_signaltools.py#L1165

and conv = IFFT( FFT x FFT(conjugate).

But for later!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants