I have an array A of 2-SVectors
. I’d like to apply fft
component-wise, namely, something like fft(A[:,:][1])
and fft(A[:,:][2])
and put them together again as nested array of vectors. It is best if this can be done in-place, namely, through something like plan_fft!
. Is there a natural way of doing this? I can use getindex.(A,1)
but this cannot generate a “view” to allow in-place operations.
I found a solution
A_r = reinterpret(reshape,eltype(A[1]),A)
fft!(A_r,(2,3))
Of course eltype(A)
should be of some complex float type to allow in-place fft.
Update: here is an example: BTW, reinterpret(reshape, ... )
only works for Julia 1.6 and above
julia> using LinearAlgebra
julia> using StaticArrays
julia> using FFTW
julia> N=10; M=20;
julia> vf=[@SVector rand(ComplexF64,2) for n in 1:N, m=1:M];
julia> vf_copy=deepcopy(vf);
julia> vfr=reinterpret(reshape,eltype(eltype(vf)),vf);
julia> fft!(vfr,(2,3));
julia> fft_vf1=fft(getindex.(vf_copy,1));
julia> norm(fft_vf1-getindex.(vf,1))
0.0
julia> fft_vf2=fft(getindex.(vf_copy,2));
julia> norm(fft_vf2-getindex.(vf,2))
0.0
1 Like
@jinml, could you complete your example with some fake input data, to make it useful to others?
Thanks.