# 对周期的向量值函数使用FFT

function f(x)
[sin(2*pi*x[1])*cos(2*pi*x[2]), cos(2*pi*x[1])*sin(2*pi*x[2])]
end
N=200
data = [f([i/N,j/N]) for i in 0:N-1, j in 0:N-1]


function v_fft(A::Matrix{Vector{Float64}})
n=size(A,2)
b=Matrix{Float64}(undef, n, n)
c=Matrix{Float64}(undef, n, n)
@inbounds @simd for i in 1:n^2
b[i]=A[i][1]
c[i]=A[i][2]
end
fb=fft(b)
fc=fft(c)
result=Matrix{Vector{ComplexF64}}(undef, n, n)
@inbounds @simd for i in 1:n^2
result[i]=[fb[i],fc[i]]
end
result
end


julia> fft(randn(4, 4), 1)  # FFT over the first dimension
4×4 Matrix{ComplexF64}:
-0.169743+0.0im         1.38273+0.0im         2.7259+0.0im        1.73068+0.0im
-1.29979+0.593027im   -1.57606+1.05342im  -0.825811-0.636893im  0.292878+0.408652im
0.48972+0.0im       -0.957822+0.0im      -0.573209+0.0im        2.40699+0.0im
-1.29979-0.593027im   -1.57606-1.05342im  -0.825811+0.636893im  0.292878-0.408652im

julia> fft(randn(4, 4))   # 2D FFT
4×4 Matrix{ComplexF64}:
1.07097+0.0im       0.502392-1.92969im   -4.29076+0.0im        0.502392+1.92969im
-3.4929-2.11008im   -2.20324-1.97117im    5.33781+0.329706im  -0.722525+0.459805im
8.37998+0.0im       -4.41875-2.98268im    2.09503+0.0im        -4.41875+2.98268im
-3.4929+2.11008im  -0.722525-0.459805im   5.33781-0.329706im   -2.20324+1.97117im

help?> fft
fft(A [, dims])

Performs a multidimensional FFT of the array A. The optional dims argument specifies an iterable subset of
dimensions (e.g. an integer, range, tuple, or array) to transform along. Most efficient if the size of A along
the transformed dimensions is a product of small primes; see Base.nextprod. See also plan_fft() for even greater
efficiency.

A one-dimensional FFT computes the one-dimensional discrete Fourier transform (DFT) as defined by

\operatorname{DFT}(A)[k] =
\sum_{n=1}^{\operatorname{length}(A)}
\exp\left(-i\frac{2\pi
(n-1)(k-1)}{\operatorname{length}(A)} \right) A[n].

A multidimensional FFT simply performs this operation along each transformed dimension of A.

│ Note
│
│  This performs a multidimensional FFT by default. FFT libraries in other languages such as Python and
│  Octave perform a one-dimensional FFT along the first non-singleton dimension of the array. This is
│  worth noting while performing comparisons.



1 个赞

function f(x)
[sin(2*pi*x[1])*cos(2*pi*x[2]), cos(2*pi*x[1])*sin(2*pi*x[2])]
end
N=200
data = [f([i/N,j/N])[k] for i in 0:N-1, j in 0:N-1, k in 1:2]

using FFTW
fft(data, 1:2) # 沿着前两个维度进行 Fourier 变换, 即分别会对 data[:,:,1],data[:,:,2] 进行 Fourier 变换.