Skip to content

Commit 08b95f3

Browse files
add test for uniform points/frequencies
- change `cg` to `cg_for_inufft` in case the package is imported. (It’s less likely that this name would be used elsewhere.) - add `cg` vectors to the `iNUFFTPlan`
1 parent bea24db commit 08b95f3

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/inufft.jl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ For best performance, choose the right number of threads by `FFTW.set_num_thread
66
immutable iNUFFTPlan{N,T,S,PT} <: Base.DFT.Plan{T}
77
pt::PT
88
TP::Toeplitz{T}
9+
r::Vector{T}
10+
p::Vector{T}
11+
Ap::Vector{T}
912
ϵ::S
1013
end
1114

@@ -22,8 +25,11 @@ function plan_inufft1{T<:AbstractFloat}(ω::AbstractVector{T}, ϵ::T)
2225
r[1] = avg
2326
c[1] = avg
2427
TP = Toeplitz(c, r)
28+
r = zero(c)
29+
p = zero(c)
30+
Ap = zero(c)
2531

26-
iNUFFTPlan{1, eltype(TP), typeof(ϵ), typeof(pt)}(pt, TP, ϵ)
32+
iNUFFTPlan{1, eltype(TP), typeof(ϵ), typeof(pt)}(pt, TP, r, p, Ap, ϵ)
2733
end
2834

2935
doc"""
@@ -38,23 +44,26 @@ function plan_inufft2{T<:AbstractFloat}(x::AbstractVector{T}, ϵ::T)
3844
r[1] = avg
3945
c[1] = avg
4046
TP = Toeplitz(c, r)
47+
r = zero(c)
48+
p = zero(c)
49+
Ap = zero(c)
4150

42-
iNUFFTPlan{2, eltype(TP), typeof(ϵ), typeof(pt)}(pt, TP, ϵ)
51+
iNUFFTPlan{2, eltype(TP), typeof(ϵ), typeof(pt)}(pt, TP, r, p, Ap, ϵ)
4352
end
4453

4554
function (*){N,T,V}(p::iNUFFTPlan{N,T}, x::AbstractVector{V})
4655
A_mul_B!(zeros(promote_type(T,V), length(x)), p, x)
4756
end
4857

4958
function Base.A_mul_B!{T}(c::AbstractVector{T}, P::iNUFFTPlan{1,T}, f::AbstractVector{T})
50-
pt, TP, ϵ = P.pt, P.TP, P.ϵ
51-
cg(TP, c, f, 50, 100ϵ)
59+
pt, TP, r, p, Ap, ϵ = P.pt, P.TP, P.r, P.p, P.Ap, P.ϵ
60+
cg_for_inufft(TP, c, f, r, p, Ap, 50, 100ϵ)
5261
conj!(A_mul_B!(c, pt, conj!(c)))
5362
end
5463

5564
function Base.A_mul_B!{T}(c::AbstractVector{T}, P::iNUFFTPlan{2,T}, f::AbstractVector{T})
56-
pt, TP, ϵ = P.pt, P.TP, P.ϵ
57-
cg(TP, c, conj!(pt*conj!(f)), 50, 100ϵ)
65+
pt, TP, r, p, Ap, ϵ = P.pt, P.TP, P.r, P.p, P.Ap, P.ϵ
66+
cg_for_inufft(TP, c, conj!(pt*conj!(f)), r, p, Ap, 50, 100ϵ)
5867
conj!(f)
5968
c
6069
end
@@ -69,16 +78,16 @@ Computes an inverse nonuniform fast Fourier transform of type II.
6978
"""
7079
inufft2{T<:AbstractFloat}(c::AbstractVector, x::AbstractVector{T}, ϵ::T) = plan_inufft2(x, ϵ)*c
7180

72-
function cg{T}(A::ToeplitzMatrices.AbstractToeplitz{T}, x::AbstractVector{T}, b::AbstractVector{T}, max_it::Integer, tol::Real)
81+
function cg_for_inufft{T}(A::ToeplitzMatrices.AbstractToeplitz{T}, x::AbstractVector{T}, b::AbstractVector{T}, r::AbstractVector{T}, p::AbstractVector{T}, Ap::AbstractVector{T}, max_it::Integer, tol::Real)
7382
n = length(b)
7483
n1, n2 = size(A)
7584
n == n1 == n2 || throw(DimensionMismatch(""))
7685
nrmb = norm(b)
7786
if nrmb == 0 nrmb = one(typeof(nrmb)) end
7887
copy!(x, b)
79-
r = zero(x)
80-
p = zero(x)
81-
Ap = zero(x)
88+
fill!(r, zero(T))
89+
fill!(p, zero(T))
90+
fill!(Ap, zero(T))
8291
# r = b - A*x
8392
copy!(r, b)
8493
A_mul_B!(-one(T), A, x, one(T), r)

src/nufft.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ F_{i,j} = \sum_{k=0}^{M-1}\sum_{\ell=0}^{N-1} C_{k,\ell} e^{-2\pi{\rm i} (x_i k
301301
nufft2{T<:AbstractFloat}(C::Matrix, x::AbstractVector{T}, y::AbstractVector{T}, ϵ::T) = plan_nufft2(x, y, ϵ)*C
302302

303303

304-
FindK{T<:AbstractFloat}::T, ϵ::T) = Int(ceil(5*γ*exp(lambertw(log(10/ϵ)/γ/7))))
304+
FindK{T<:AbstractFloat}::T, ϵ::T) = γ < ϵ ? 1 : Int(ceil(5*γ*exp(lambertw(log(10/ϵ)/γ/7))))
305305
AssignClosestEquispacedGridpoint{T<:AbstractFloat}(x::AbstractVector{T})::AbstractVector{T} = round.([Int], size(x, 1)*x)
306306
AssignClosestEquispacedFFTpoint{T<:AbstractFloat}(x::AbstractVector{T})::Array{Int,1} = mod.(round.([Int], size(x, 1)*x), size(x, 1)) + 1
307307
PerturbationParameter{T<:AbstractFloat}(x::AbstractVector{T}, s_vec::AbstractVector{T})::AbstractFloat = norm(size(x, 1)*x - s_vec, Inf)

test/nuffttests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ using FastTransforms, Base.Test
6868
@test norm(exact - fast, Inf) < err_bnd
6969
end
7070

71+
# Check that if points/frequencies are indeed uniform, then it's equal to the fft.
72+
73+
n = 1000
74+
c = complex(rand(n))
75+
ω = collect(0.0:n-1)
76+
x = ω/n
77+
fftc = fft(c)
78+
@test norm(nufft1(c, ω, eps()) - fftc) == 0
79+
@test norm(nufft2(c, x, eps()) - fftc) == 0
80+
@test norm(nufft3(c, x, ω, eps()) - fftc) == 0
81+
82+
7183
function nudft1{T<:AbstractFloat}(C::Matrix{Complex{T}}, ω1::AbstractVector{T}, ω2::AbstractVector{T})
7284
# Nonuniform discrete Fourier transform of type I-I
7385

0 commit comments

Comments
 (0)