I have a multidimensional array A. Fourier[A] finds the discrete Fourier transform over all dimensions. How to find discrete Fourier transform over a custom dimension like fft(A,[],dim) function in MATLAB?
My current solution:
FourierDim[list_, dim_] :=
Module[{dims, ord, depth = ArrayDepth[list], tr, f},
dims = If[IntegerQ[dim], If[dim > 0, {dim}, {1 + depth + dim}], dim];
ord = Join[Complement[Range[depth], dims], dims];
tr = Transpose[list, Ordering[ord]];
f = ConstantArray[0.0 + 0.0 I, Dimensions[tr]];
With[{nn = Sequence @@ Table[Unique["n"], {depth - Length@dims}]},
With[{lim = Sequence @@ ({{nn}, Dimensions[tr][[1 ;; -1 - Length@dims]]}\[Transpose])},
Do[f[[nn]] = Fourier@tr[[nn]], lim]]];
Transpose[f, ord]
];
It transposes the array, does Fourier transform in the loop, and transposes the array backward.
Examples:
FourierDim[A,2]; (* Fourier over the second dimension *)
FourierDim[A,-1]; (* Fourier over the last dimension *)
FourierDim[A,{1,3}]; (* Fourier over the first and the third dimensions *)