juls
June 23, 2021, 4:09pm
1
I am trying to implement the below matrix computation from matlab in julia. But the ifft function gives different output than obtained in matlab
a = zeros(5,10);
for i = 1:5
a(i,:) = ifft((c(i,:).*(d)));
end
where ‘c’ is a non- zero 510 matrix and ‘d’ is a non- zero 1 10 matrix.
jling
June 23, 2021, 4:14pm
2
please provide a minimal working example to show the difference
1 Like
mgkuhn
June 23, 2021, 4:48pm
3
I can’t reproduce the problem.
Julia:
using FFTW
c = [-0.3 0.7 0.4 1.1 -1.3 -0.1 -1.4 0.3 0.7 -0.1;
0.1 -2.0 -1.9 -1.4 -0.6 -2.4 -0.8 -0.9 -2.4 -2.2;
0.2 -1.5 -2.5 2.2 -0.2 -1.0 -0.1 2.3 1.1 -1.9;
-1.5 0.5 0.7 2.3 1.5 2.3 1.0 1.3 1.3 2.1;
1.8 1.5 -0.6 0.4 2.0 0.4 -0.2 2.0 0.8 -1.6 ];
d = [ 0.9 -0.0 -1.0 0.6 -0.2 -0.6 0.7 -1.7 -0.9 -0.6 ]';
a = zeros(Complex, 5, 10);
for i = 1:5
a[i,:] = ifft(c[i,:] .* d);
end
round.(a, digits=4)
5×10 Matrix{ComplexF64}:
-0.175+0.0im -0.0064+0.2025im 0.0298-0.1789im 0.0384+0.0299im 0.0052+0.013im -0.229+0.0im 0.0052-0.013im 0.0384-0.0299im 0.0298+0.1789im -0.0064-0.2025im
0.716+0.0im 0.1115-0.2877im -0.2041-0.0662im -0.462+0.0937im 0.2286-0.3182im 0.026+0.0im 0.2286+0.3182im -0.462-0.0937im -0.2041+0.0662im 0.1115+0.2877im
0.081+0.0im 0.1794+0.7688im 0.1997-0.2212im -0.4099-0.6105im -0.0452+0.092im 0.251+0.0im -0.0452-0.092im -0.4099+0.6105im 0.1997+0.2212im 0.1794-0.7688im
-0.629+0.0im -0.1634+0.4014im -0.0811+0.0315im 0.1384-0.2139im -0.2869+0.4296im 0.065+0.0im -0.2869-0.4296im 0.1384+0.2139im -0.0811-0.0315im -0.1634-0.4014im
-0.148+0.0im 0.4013+0.4im 0.4163-0.2029im -0.1063-0.4076im 0.0027+0.1795im 0.34+0.0im 0.0027-0.1795im -0.1063+0.4076im 0.4163+0.2029im 0.4013-0.4im
Same output in MATLAB as your code with the same input matrix/vector:
c = [-0.3 0.7 0.4 1.1 -1.3 -0.1 -1.4 0.3 0.7 -0.1;
0.1 -2.0 -1.9 -1.4 -0.6 -2.4 -0.8 -0.9 -2.4 -2.2;
0.2 -1.5 -2.5 2.2 -0.2 -1.0 -0.1 2.3 1.1 -1.9;
-1.5 0.5 0.7 2.3 1.5 2.3 1.0 1.3 1.3 2.1;
1.8 1.5 -0.6 0.4 2.0 0.4 -0.2 2.0 0.8 -1.6 ];
d = [ 0.9 -0.0 -1.0 0.6 -0.2 -0.6 0.7 -1.7 -0.9 -0.6 ];
a = zeros(5,10);
for i = 1:5
a(i,:) = ifft((c(i,:).*(d)));
end
a
a =
Columns 1 through 8
-0.1750 + 0.0000i -0.0064 + 0.2025i 0.0298 - 0.1789i 0.0384 + 0.0299i 0.0052 + 0.0130i -0.2290 + 0.0000i 0.0052 - 0.0130i 0.0384 - 0.0299i
0.7160 + 0.0000i 0.1115 - 0.2877i -0.2041 - 0.0662i -0.4620 + 0.0937i 0.2286 - 0.3182i 0.0260 + 0.0000i 0.2286 + 0.3182i -0.4620 - 0.0937i
0.0810 + 0.0000i 0.1794 + 0.7688i 0.1997 - 0.2212i -0.4099 - 0.6105i -0.0452 + 0.0920i 0.2510 + 0.0000i -0.0452 - 0.0920i -0.4099 + 0.6105i
-0.6290 + 0.0000i -0.1634 + 0.4014i -0.0811 + 0.0315i 0.1384 - 0.2139i -0.2869 + 0.4296i 0.0650 + 0.0000i -0.2869 - 0.4296i 0.1384 + 0.2139i
-0.1480 + 0.0000i 0.4013 + 0.4000i 0.4163 - 0.2029i -0.1063 - 0.4076i 0.0027 + 0.1795i 0.3400 + 0.0000i 0.0027 - 0.1795i -0.1063 + 0.4076i
1 Like
mgkuhn
June 23, 2021, 4:57pm
4
Does the above answer your question?
juls
June 23, 2021, 5:27pm
5
Thank you, the c and d input matrices I am using are complex and am I getting little difference
c =
Columns 1 through 5
97.9713 + 0.0000i 3.7743 -90.6967i -70.9956 - 6.0008i -5.8363 +44.4675i 18.1929 + 3.5097i
97.9619 + 0.0000i 3.7759 -90.6841i -70.9751 - 6.0029i -5.8372 +44.4393i 18.1620 + 3.5090i
97.9337 + 0.0000i 3.7791 -90.6464i -70.9149 - 6.0057i -5.8356 +44.3569i 18.0727 + 3.5015i
97.8861 + 0.0000i 3.7812 -90.5838i -70.8161 - 6.0049i -5.8257 +44.2231i 17.9287 + 3.4810i
97.8153 + 0.0000i 3.7835 -90.4930i -70.6775 - 6.0029i -5.8118 +44.0387i 17.7319 + 3.4534i
Columns 6 through 10
0.1821 + 1.6917i 11.9049 + 2.6249i 3.7467 -12.7256i -7.3258 - 2.8909i -0.7121 + 0.1283i
0.1806 + 1.7176i 11.9192 + 2.6249i 3.7430 -12.7262i -7.3161 - 2.8828i -0.7013 + 0.1158i
0.1706 + 1.7907i 11.9568 + 2.6305i 3.7372 -12.7221i -7.2808 - 2.8632i -0.6726 + 0.0715i
0.1455 + 1.9077i 12.0153 + 2.6483i 3.7360 -12.7122i -7.2198 - 2.8388i -0.6323 - 0.0040i
0.1124 + 2.0667i 12.0940 + 2.6710i 3.7329 -12.6976i -7.1353 - 2.8048i -0.5774 - 0.1089i
d=
Columns 1 through 5
3.1922 + 0.0000i 0.3826 - 6.7552i -15.0138 - 1.4812i -3.4435 +23.5588i 30.5882 + 6.0793i
Columns 6 through 10
9.3782 -37.2328i -44.3985 -13.4759i -18.3235 +51.3590i 57.3788 +23.7564i 29.7857 -62.8610i
a = zeros(5,10);
for i = 1:5
a(i,:) = ifft(c(i,:) .* d);
end
a in MATLAB:
a =
1.0e+02 *
Columns 1 through 5
0.0578 - 0.0712i 0.0089 + 0.0020i -0.1765 + 0.2240i -0.9484 + 0.7314i 2.4748 - 3.7660i
0.0576 - 0.0718i 0.0085 + 0.0015i -0.1773 + 0.2235i -0.9504 + 0.7310i 2.4760 - 3.7627i
0.0567 - 0.0735i 0.0070 - 0.0001i -0.1800 + 0.2221i -0.9567 + 0.7301i 2.4797 - 3.7510i
0.0549 - 0.0762i 0.0043 - 0.0023i -0.1845 + 0.2200i -0.9670 + 0.7292i 2.4863 - 3.7306i
0.0525 - 0.0799i 0.0005 - 0.0054i -0.1908 + 0.2173i -0.9809 + 0.7281i 2.4951 - 3.7025i
Columns 6 through 10
2.0623 - 0.3361i -0.6152 + 3.8925i -0.0011 - 0.3264i 0.1463 - 0.2339i 0.1185 - 0.1163i
2.0594 - 0.3350i -0.6109 + 3.8924i -0.0006 - 0.3275i 0.1464 - 0.2346i 0.1184 - 0.1169i
2.0529 - 0.3325i -0.5984 + 3.8916i 0.0005 - 0.3309i 0.1464 - 0.2369i 0.1180 - 0.1189i
2.0440 - 0.3306i -0.5787 + 3.8899i 0.0020 - 0.3367i 0.1463 - 0.2408i 0.1171 - 0.1220i
2.0318 - 0.3277i -0.5516 + 3.8872i 0.0040 - 0.3446i 0.1460 - 0.2461i 0.1158 - 0.1264i
In Julia,
a =
5×10 Array{Complex,2}:
6.10126-7.5036im 1.08646-0.29492im … 12.2904-11.8982im
6.0691-7.54846im 1.03751-0.332524im 12.2716-11.9514im
5.95686-7.69857im 0.870022-0.454864im 12.205-12.1323im
5.76391-7.94203im 0.587843-0.649582im 12.0858-12.4299im
5.49364-8.27828im 0.194955-0.91658im 11.9176-12.8427im
mgkuhn
June 23, 2021, 5:50pm
6
Do these examples have to be so big? Can you not show the problem with e.g. just a 2x4 matrix? And can you please include both the full MATLAB and the full Julia code that you used (including the assignments to c and d, as well as the using statement), such that we have nothing other to do than to copy&paste to reproduce exactly what you are getting? Otherwise trying to reproduce your matrices in Julia from your MATLAB outputs is quite labour intensive.
1 Like
Julia always calculates the fft over all dimensions.
Maybe Matlab does that differently and you maybe need so specify over which dimensions you want to transform?
3 Likes
apo383
June 23, 2021, 7:56pm
8
@Sukera already answered almost the same question for fft
, which boiled down to the fact that Matlab by default computes the fft
for each column, whereas Julia computes a multi-dimensional one.
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 …
The same applies to ifft
. I’d like to gently suggest looking at the documentation, ?ifft
in Julia and help ifft
in Matlab.
1 Like