|
| 1 | +export NFFTOpImpl |
| 2 | +import Base.adjoint |
| 3 | + |
| 4 | +function LinearOperatorCollection.constructLinearOperator(::Type{Op}; |
| 5 | + shape::Tuple, nodes::AbstractMatrix{T}, toeplitz=false, oversamplingFactor=1.25, |
| 6 | + kernelSize=3, kargs...) where Op <: NFFTOp{T} where T <: Number |
| 7 | + return NFFTOpImpl(T, shape, nodes; toeplitz, oversamplingFactor, kernelSize, kargs... ) |
| 8 | +end |
| 9 | + |
| 10 | +mutable struct NFFTOpImpl{T} <: NFFTOp{T} |
| 11 | + nrow :: Int |
| 12 | + ncol :: Int |
| 13 | + symmetric :: Bool |
| 14 | + hermitian :: Bool |
| 15 | + prod! :: Function |
| 16 | + tprod! :: Nothing |
| 17 | + ctprod! :: Function |
| 18 | + nprod :: Int |
| 19 | + ntprod :: Int |
| 20 | + nctprod :: Int |
| 21 | + args5 :: Bool |
| 22 | + use_prod5! :: Bool |
| 23 | + allocated5 :: Bool |
| 24 | + Mv5 :: Vector{T} |
| 25 | + Mtu5 :: Vector{T} |
| 26 | + plan |
| 27 | + toeplitz :: Bool |
| 28 | +end |
| 29 | + |
| 30 | +LinearOperators.storage_type(op::NFFTOpImpl) = typeof(op.Mv5) |
| 31 | + |
| 32 | +""" |
| 33 | + NFFTOpImpl(shape::Tuple, tr::Trajectory; kargs...) |
| 34 | + NFFTOpImpl(shape::Tuple, tr::AbstractMatrix; kargs...) |
| 35 | +
|
| 36 | +generates a `NFFTOpImpl` which evaluates the MRI Fourier signal encoding operator using the NFFT. |
| 37 | +
|
| 38 | +# Arguments: |
| 39 | +* `shape::NTuple{D,Int64}` - size of image to encode/reconstruct |
| 40 | +* `tr` - Either a `Trajectory` object, or a `ND x Nsamples` matrix for an ND-dimenensional (e.g. 2D or 3D) NFFT with `Nsamples` k-space samples |
| 41 | +* (`nodes=nothing`) - Array containg the trajectory nodes (redundant) |
| 42 | +* (`kargs`) - additional keyword arguments |
| 43 | +""" |
| 44 | +function NFFTOpImpl(shape::Tuple, tr::AbstractMatrix{T}; toeplitz=false, oversamplingFactor=1.25, kernelSize=3, kargs...) where {T} |
| 45 | + |
| 46 | + plan = plan_nfft(tr, shape, m=kernelSize, σ=oversamplingFactor, precompute=NFFT.TENSOR, |
| 47 | + fftflags=FFTW.ESTIMATE, blocking=true) |
| 48 | + |
| 49 | + return NFFTOpImpl{Complex{T}}(size(tr,2), prod(shape), false, false |
| 50 | + , (res,x) -> produ!(res,plan,x) |
| 51 | + , nothing |
| 52 | + , (res,y) -> ctprodu!(res,plan,y) |
| 53 | + , 0, 0, 0, false, false, false, Complex{T}[], Complex{T}[] |
| 54 | + , plan, toeplitz) |
| 55 | +end |
| 56 | + |
| 57 | +function produ!(y::AbstractVector, plan::NFFT.NFFTPlan, x::AbstractVector) |
| 58 | + mul!(y, plan, reshape(x,plan.N)) |
| 59 | +end |
| 60 | + |
| 61 | +function ctprodu!(x::AbstractVector, plan::NFFT.NFFTPlan, y::AbstractVector) |
| 62 | + mul!(reshape(x, plan.N), adjoint(plan), y) |
| 63 | +end |
| 64 | + |
| 65 | + |
| 66 | +function Base.copy(S::NFFTOpImpl{T}) where {T} |
| 67 | + plan = copy(S.plan) |
| 68 | + return NFFTOpImpl{T}(size(plan.k,2), prod(plan.N), false, false |
| 69 | + , (res,x) -> produ!(res,plan,x) |
| 70 | + , nothing |
| 71 | + , (res,y) -> ctprodu!(res,plan,y) |
| 72 | + , 0, 0, 0, false, false, false, T[], T[] |
| 73 | + , plan, S.toeplitz) |
| 74 | +end |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +######################################################################### |
| 79 | +### Toeplitz Operator ### |
| 80 | +######################################################################### |
| 81 | + |
| 82 | +mutable struct NFFTToeplitzNormalOp{T,D,W} <: AbstractLinearOperator{T} |
| 83 | + nrow :: Int |
| 84 | + ncol :: Int |
| 85 | + symmetric :: Bool |
| 86 | + hermitian :: Bool |
| 87 | + prod! :: Function |
| 88 | + tprod! :: Nothing |
| 89 | + ctprod! :: Nothing |
| 90 | + nprod :: Int |
| 91 | + ntprod :: Int |
| 92 | + nctprod :: Int |
| 93 | + args5 :: Bool |
| 94 | + use_prod5! :: Bool |
| 95 | + allocated5 :: Bool |
| 96 | + Mv5 :: Vector{T} |
| 97 | + Mtu5 :: Vector{T} |
| 98 | + shape::NTuple{D,Int} |
| 99 | + weights::W |
| 100 | + fftplan |
| 101 | + ifftplan |
| 102 | + λ::Array{T} |
| 103 | + xL1::Array{T,D} |
| 104 | + xL2::Array{T,D} |
| 105 | +end |
| 106 | + |
| 107 | +LinearOperators.storage_type(op::NFFTToeplitzNormalOp) = typeof(op.Mv5) |
| 108 | + |
| 109 | +function NFFTToeplitzNormalOp(shape, W, fftplan, ifftplan, λ, xL1::Array{T,D}, xL2::Array{T,D}) where {T,D} |
| 110 | + |
| 111 | + function produ!(y, shape, fftplan, ifftplan, λ, xL1, xL2, x) |
| 112 | + xL1 .= 0 |
| 113 | + x = reshape(x, shape) |
| 114 | + |
| 115 | + xL1[CartesianIndices(x)] .= x |
| 116 | + mul!(xL2, fftplan, xL1) |
| 117 | + xL2 .*= λ |
| 118 | + mul!(xL1, ifftplan, xL2) |
| 119 | + |
| 120 | + y .= vec(xL1[CartesianIndices(x)]) |
| 121 | + return y |
| 122 | + end |
| 123 | + |
| 124 | + return NFFTToeplitzNormalOp(prod(shape), prod(shape), false, false |
| 125 | + , (res,x) -> produ!(res, shape, fftplan, ifftplan, λ, xL1, xL2, x) |
| 126 | + , nothing |
| 127 | + , nothing |
| 128 | + , 0, 0, 0, false, false, false, T[], T[] |
| 129 | + , shape, W, fftplan, ifftplan, λ, xL1, xL2) |
| 130 | +end |
| 131 | + |
| 132 | +function NFFTToeplitzNormalOp(S::NFFTOp{T}, W=opEye(T,size(S,1))) where {T} |
| 133 | + shape = S.plan.N |
| 134 | + |
| 135 | + # plan the FFTs |
| 136 | + fftplan = plan_fft( zeros(T, 2 .* shape);flags=FFTW.MEASURE) |
| 137 | + ifftplan = plan_ifft(zeros(T, 2 .* shape);flags=FFTW.MEASURE) |
| 138 | + |
| 139 | + # TODO extend the following function by weights |
| 140 | + # λ = calculateToeplitzKernel(shape, S.plan.k; m = S.plan.params.m, σ = S.plan.params.σ, window = S.plan.params.window, LUTSize = S.plan.params.LUTSize, fftplan = fftplan) |
| 141 | + |
| 142 | + shape_os = 2 .* shape |
| 143 | + p = plan_nfft(typeof(S.plan.k), S.plan.k, shape_os; m = S.plan.params.m, σ = S.plan.params.σ, |
| 144 | + precompute=NFFT.POLYNOMIAL, fftflags=FFTW.ESTIMATE, blocking=true) |
| 145 | + eigMat = adjoint(p) * ( W * ones(T, size(S.plan.k,2))) |
| 146 | + λ = fftplan * fftshift(eigMat) |
| 147 | + |
| 148 | + xL1 = Array{T}(undef, 2 .* shape) |
| 149 | + xL2 = similar(xL1) |
| 150 | + |
| 151 | + return NFFTToeplitzNormalOp(shape, W, fftplan, ifftplan, λ, xL1, xL2) |
| 152 | +end |
| 153 | + |
| 154 | +function LinearOperatorCollection.normalOperator(S::NFFTOpImpl{T}, W=opEye(T,size(S,1))) where T |
| 155 | + if S.toeplitz |
| 156 | + return NFFTToeplitzNormalOp(S,W) |
| 157 | + else |
| 158 | + return NormalOp(S,W) |
| 159 | + end |
| 160 | +end |
| 161 | + |
| 162 | +function Base.copy(A::NFFTToeplitzNormalOp{T,D,W}) where {T,D,W} |
| 163 | + fftplan = plan_fft( zeros(T, 2 .* A.shape); flags=FFTW.MEASURE) |
| 164 | + ifftplan = plan_ifft(zeros(T, 2 .* A.shape); flags=FFTW.MEASURE) |
| 165 | + return NFFTToeplitzNormalOp(A.shape, A.weights, fftplan, ifftplan, A.λ, copy(A.xL1), copy(A.xL2)) |
| 166 | +end |
0 commit comments