# Examples of FINUFFT?

**URL:** https://discourse.julialang.org/t/examples-of-finufft/103708
**Category:** Numerics
**Tags:** nfft
**Created:** [September 9, 2023, 7:28pm UTC](https://discourse.julialang.org/t/examples-of-finufft/103708 "2023-09-09T19:28:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![erny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erny123/32/52255_2.png) [@erny123](https://discourse.julialang.org/u/erny123)
#### Post date: [September 9, 2023, 7:28pm UTC](https://discourse.julialang.org/t/examples-of-finufft/103708/1 "2023-09-09T19:28:35Z")

</div>

I’m trying to learn some basic Non-Uniform FFT.

I want to see if I can get a frequency spectrum of a laser pulse with a NUFFT type 1 using the FINUFFT.jl library.

Here’s the example:

```julia
using FFTW
using FINUFFT

c = 2.998e8; #m/s speed of light
wl = 525.0e-9; #m wavelength 
pl = wl/c; #period
wlfreq = c/wl; #2pi*Hz frequency

fwhm = 500.0e-15; #temporal fwhm
tau0 = fwhm/sqrt(2*log(2)); #gaussian fwhm
tlbw = 0.44/fwhm #transform limited bandwidth
	
maxf = c/wl +tlbw
minf = c/wl-tlbw
df = (maxf - minf)/1.0e3
	
	
#simulation
dt = pl/5.0;
t0 = -2.0*fwhm
tend = 2.0*fwhm
ttot = ceil(Int64,(tend-t0)/dt)

E = [exp(-t^2/tau0^2)*cos(2.0*pi*wlfreq*t) for t in t0:dt:tend];
t = [i for i in t0:dt:tend];
lent = length(E)
Nt = length(t);

```

The pulse looks like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/b/6b1421465e596c6796cb01e0c6a6616b6df4e55f.png)

Taking the fft:

```julia

freq = FFTW.fftshift(FFTW.fftfreq(lent,1/dt))
Ef = FFTW.fftshift(FFTW.fft(E));
If = abs2.(Ef); #intensity spectrum

```

Looks like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/2/322f0939a12b017aeee34d3be2fbe405cb7768d7.png)

Which is exactly what I expect.

Now I want to us a NUFFT type 1 according to:  
[https://finufft.readthedocs.io/en/latest/math.html](https://finufft.readthedocs.io/en/latest/math.html)

I decide to take nonuniform points in the form of a tangent curve so as to get most of the FWHM of the pulse:

```julia
#nonuniform indices to extract sampling points
#using tangent curve
maxpi = pi*22.0/100.0;
dpi = 2.0*maxpi/(lent/2);
l = [i for i in -maxpi:dpi:maxpi];
tt = @. tan(1.95*l);
ttt = @. -tt[1]+tt;
ttt = @. ttt*lent/8.81+1;
t4 = @. ceil(Int64,ttt);

#extract nonuniform points
Enu = convert.(ComplexF64,E[t4]); #field at nonuniform points
tnu = t[t4]; #time at noniniform points
lentnu = length(Enu)

```

This looks like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d7cc62791279e55ffd292eaea7e9e0d4129a903.png)

```julia
wmax = freq[end]; #max freq get from previous FFTW, around 1.5e15
Nw = length(freq); #number of total points|
dw = 2*wmax/Nw; # spacing of target k grid|
w = dw .* [i for i in ceil(Int64,-Nw/2):ceil(Int64,Nw/2-1)]; # % a particular uniform M-grid of this spacing|
#normalize
Enunorm = Enu ./ maximum(E)

fhat = FINUFFT.nufft1d1(tnu, Enunorm, 1, 1.0e-12, Nw); #% type 1, requesting M modes

```

And the intensity spectrum looks like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/f/8f951a942f060885bb7b455bd1c1064d83c54b7a.png)

Not sure what to make of it. The amplitudes change but by an extremely small bit?  
The main pulse frequency is off by more than 2.

---

<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: [September 9, 2023, 8:34pm UTC](https://discourse.julialang.org/t/examples-of-finufft/103708/2 "2023-09-09T20:34:44Z")

</div>

Fyi, the code posted doesn’t run: what are `pl` and `wlfreq`?

---

<div class="post-metadata">

### Author: ![erny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erny123/32/52255_2.png) [@erny123](https://discourse.julialang.org/u/erny123)
#### Post date: [September 9, 2023, 9:18pm UTC](https://discourse.julialang.org/t/examples-of-finufft/103708/3 "2023-09-09T21:18:00Z")

</div>

Oops sorry, edited my original post

---

<div class="post-metadata">

### Author: ![erny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erny123/32/52255_2.png) [@erny123](https://discourse.julialang.org/u/erny123)
#### Post date: [September 9, 2023, 9:30pm UTC](https://discourse.julialang.org/t/examples-of-finufft/103708/4 "2023-09-09T21:30:09Z")

</div>

Ok, looks like I still need more sampling points to get a better NUFFT spectrum.

For example increasing the sampling frequency to 1/10 the period of the wavelength rather than 1/5 in the original signal:

```julia
#simulation
dt = pl/10.0;
t0 = -2.0*fwhm
tend = 2.0*fwhm
ttot = ceil(Int64,(tend-t0)/dt)

E = [exp(-t^2/tau0^2)*cos(2.0*pi*wlfreq*t) for t in t0:dt:tend];
t = [i for i in t0:dt:tend];
lent = length(E)
Nt = length(t);

```

And then changing the nonuniform tangent sampling to match the number of points from indices 1 to length(t):

```julia
#nonuniform points
maxpi = pi*22.0/100.0;
dpi = 2.0*maxpi/(lent/2);
l = [i for i in -maxpi:dpi:maxpi];
tt = @. tan(1.95*l);
ttt = @. -tt[1]+tt;
ttt = @. ttt*lent/8.82+1;
t4 = @. ceil(Int64,ttt);

```

and I had to normalize the timed samples and the pulse amplitudes;

```julia
wmax = freq[end]; #max freq get from previous FFTW, around 1.5e15
Nw = length(freq); #number of total points|
dw = 2*wmax/Nw; # spacing of target k grid|
w = dw .* [i for i in ceil(Int64,-Nw/2):ceil(Int64,Nw/2-1)]; # % a particular uniform M-grid of this spacing
#normalize
Enunorm = Enu ./ maximum(E)
tnunorm = tnu ./ tend

fhat = FINUFFT.nufft1d1(tnunorm, Enunorm, 1, 1.0e-12, Nw); #% type 1, requesting M modes

```

With the rest of the code unchanged, my spectrum is now much cleaner:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/9/39983a519e488810b13f0aa6106b1015f619135a.png)

The only issue I have now is that the frequency isn’t on the center frequency of approximately 5.7e14

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc2c01e4149ef6491579add15a3aa6c2093c25b6.png)

So I’m not sure whether the frequencies I chose are exactly the way to do it

---

<div class="post-metadata">

### Author: ![erny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erny123/32/52255_2.png) [@erny123](https://discourse.julialang.org/u/erny123)
#### Post date: [September 9, 2023, 9:51pm UTC](https://discourse.julialang.org/t/examples-of-finufft/103708/5 "2023-09-09T21:51:16Z")

</div>

Ok figured it out. The nufft1 takes time samples normalized from [-pi , pi] and the amplitudes need to be complex normalized to 1. So the time sampling needs to be normalized as follows:

```julia
#normalize
Enunorm = Enu ./ maximum(E)
tnunorm = tnu ./ tend .* (pi)

```

With this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/3/630129e27c83ccfed5d6ffb550b5cf26ebe08dd7.png)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/f/7ff49718f33c93abde91c618b1263c7e2870147f.png)

Which is extremely close to the actual value.
