Getting different output for fft function in julia and matlab

In JULIA,
2×5 Array{Float64,2}:
0.801514 1.21987 1.1503 1.18761 1.2485
0.807469 1.22325 1.14981 1.1877 1.24861

when fft function from FFTW.jl on the above array gives output:

2×5 Array{Complex{Float64},2}:
11.2246+0.0im -0.646892+0.0955452im … -0.646892-0.0955452im
-0.00903206+0.0im -0.0073713+0.00276898im -0.0073713-0.00276898im

In MATLAB, for the same 2D matrix :
t =

0.8015    1.2199    1.1503    1.1876    1.2485
0.8075    1.2233    1.1498    1.1877    1.2486

the fft function gives:
u =

1.6090    2.4431    2.3001    2.3753    2.4971

-0.0060 -0.0034 0.0005 -0.0001 -0.0001

Why is there this difference? How can I get the same output as MATLAB in Julia ?

They should be used differently. Matlab’s docs:

If X is a matrix, then fft(X) treats the columns of X as vectors and returns the Fourier transform of each column.

Julia’s fft docs:

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.

You didn’t specify whether you want a multidimensional fft, or multiple 1-dimensional ffts. Have you tried calling them according to what you want?

2 Likes

Thanks
I am trying for fft along each column. I havent been able to find how to call multidimensional and multiple 1D ffts differently

You can just give fft the dimensions:

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.
   [...]

julia> m                                                                      
2×5 Matrix{Float64}:                                                          
 0.801514  1.21987  1.1503   1.18761  1.2485                                  
 0.807469  1.22325  1.14981  1.1877   1.24861                                 
                                                                              
julia> fft(m, (1,)) # first dimension is the column, since julia is column major
2×5 Matrix{ComplexF64}:                                                       
   1.60898+0.0im   2.44312+0.0im  2.30011+0.0im  2.37531+0.0im   2.49711+0.0im
 -0.005955+0.0im  -0.00338+0.0im  0.00049+0.0im  -9.0e-5+0.0im  -0.00011+0.0im

In general, if you type ? at the julia REPL, you’ll enter help mode, where you can enter e.g. the name of a function and it’ll print any associated documentation attached to that function (if any is made available by the package developers).


As an aside, it’s a little hard to read what output you expected, please surround your code in the future with triple backticks (```) to mark them as code :slight_smile: For more information about making it easier for people to help you, please refer to this thread: Please read: make it easier to help you

6 Likes

This helped.
Thanks and I’ll make sure to get better at creating posts from next time

3 Likes