# FFT mismatch ITK/matlab

**URL:** <https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776>\
**Category:** Algorithms\
**Created:** [March 2, 2020, 11:28am UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776 "2020-03-02T11:28:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 2, 2020, 11:28am UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/1 "2020-03-02T11:28:58Z")

</div>

Hi,

I would like to know how to match the FFT implementation of matlab fftn function using ITK.

I tried both itkForwardFFTImageFilter alone and the PadIlmageFilter + ForwardFFTImageFilter. taken from [https://itk.org/ITKExamples/src/Filtering/FFT/ComputeForwardFFT/Documentation.html](https://itk.org/ITKExamples/src/Filtering/FFT/ComputeForwardFFT/Documentation.html)

So far the results are different in both cases for a 3D image. I’m in the process of implementing a filter, and I’m worried about these coefficient differences. I couldn’t find explicit references of potential difference in the documentation nor on Mathwork…

thanks

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [March 2, 2020, 2:32pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/2 "2020-03-02T14:32:45Z")

</div>

How big are the differences? Could you provide an example?

Also, try to compile ITK with `ITK_USE_FFTWD` and/or `ITK_USE_FFTWF`, and check if there are differences too.  
I think Matlab uses the `FFTW` library.

---

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 2, 2020, 3:54pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/3 "2020-03-02T15:54:02Z")

</div>

I checked my last CMakeCache.txt. FFTW is already activated on my system.

> //Use double precision fftw if found  
> ITK\_USE\_FFTWD:BOOL=ON
> 
> //Use single precision fftw if found  
> ITK\_USE\_FFTWF:BOOL=ON

For now I’m testing my code on a tiny volume of (20,20,20).

 ![tinyCylinder](https://discourse.itk.org/uploads/default/original/2X/5/57518a4c14552c385ec698159ba855fd875ff761.png)

I’m sorry for the data format but this is what I get from the matlab prompt

 ![fft_matlab](https://discourse.itk.org/uploads/default/original/2X/e/e7b6a5deac5a14ccbbb079278267d94c5fb3df74.png)

As for the itk output it is in the text file.The code used is the one from the FFT example. The format is not easy to read once again… Emacs in csv-mode gives a bit more readability. Results are printed using simple nested loops.

[itk\_fft\_examples.txt](https://discourse.itk.org/uploads/short-url/3PgLVBIE23XOIGibr3zJ8dXmE0b.txt) (230.3 KB)

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [March 2, 2020, 5:06pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/4 "2020-03-02T17:06:31Z")

</div>

How are you comparing the results then? You are providing a screenshot and a csv file. I am afraid this is unreadable.  
You can visualize images in the frequency domain with any visualization software. Please upload the input image and the frequency images (after FFT) generated with both, Matlab and ITK, and point to C++ ITK script used to generate it.

---

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 3, 2020, 3:21pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/5 "2020-03-03T15:21:34Z")

</div>

Sorry for the late reply,

Here are more infos :  
For matlab, I’m using the tools for reading NIFTI/Analyse images, which is annoying because it switches axes in the matrix transform compared to the itk implementation. But I did manage to have equivalent axis now.

Here is the first slice visualization on slicer of the real part image after FFT with automatic range for grey level. imaginary parts are also different.

 ![fft_differences](https://discourse.itk.org/uploads/default/original/2X/a/a2ab5859d95ecb08f5054a8add41e8e42ec0dfc9.png)  
[testFFT.cpp](https://discourse.itk.org/uploads/short-url/wXiYsNNG31Q0ykgnBzscgGIlTDe.cpp) (3.7 KB)  
[tinyCylinder.nii](https://discourse.itk.org/uploads/short-url/c1EwoufPHmSU4QP8PbLooz5lOA0.nii) (31.6 KB)  
[itk\_fft\_imagPart.nii](https://discourse.itk.org/uploads/short-url/Y2DrX7DJvud5cZysr4BbaUeauJ.nii) (31.6 KB)  
[itk\_fft\_realPart.nii](https://discourse.itk.org/uploads/short-url/cSfGRE7XZ27CaZgERNmYaw8pLLp.nii) (31.6 KB)  
[matlab\_fftReal\_flippedLRTD.nii](https://discourse.itk.org/uploads/short-url/sXRHGryNdVnxhdL0ZGk7gk48BLR.nii) (31.6 KB)  
[fftImag\_flippedLRTD.nii](https://discourse.itk.org/uploads/short-url/6hQ26EeLwhkx4ivFCGcGlIoZo2g.nii) (31.6 KB)

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [March 4, 2020, 12:07pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/6 "2020-03-04T12:07:36Z")

</div>

Thanks for providing more details to reproduce it.

> [@Jonas](#):
>
> But I did manage to have equivalent axis now.

Please post the minimal set of matlab commands you are using to `read` the image, apply the `fftn`, and `write` the output image, with the switch of axis you commented.  
The output image generated by a `fft` has positive and negative frequencies, and also Nyquist frequencies depending on the oddness in size of the axis, so after switching of axis, the output has to be interpreted with care.

---

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 4, 2020, 1:19pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/7 "2020-03-04T13:19:42Z")

</div>

> data = load\_nii(‘ImageName.nii’);

> fftImg = fftn(data.img);

> realPart = make\_nii(real(fftmg),[1 1 1]);  
> imagPart = make\_nii(imag(fftImg),[1 1 1]);

> save\_nii(realPart,‘fftRealPart.nii’);  
> save\_nii(imagPart,‘fftImagPart.nii’);

> flip\_lrtd('fftRealPart.nii",“fftRealPart\_flipped\_lrtd”);  
> flip\_lrtd('fftImagPart.nii",“fftImagPart\_flipped\_lrtd”);

Here are the infos on the toolbox.

> **[Tools for NIfTI and ANALYZE image](https://fr.mathworks.com/matlabcentral/fileexchange/8797-tools-for-nifti-and-analyze-image)**
>
> Load, save, make, reslice, view (and edit) both NIfTI and ANALYZE data on any platform

Last information that might be useful, ITK transformation matrix (displayed by slicer in the volume module) is [-1 0 0; 0 -1 0; 0 0 1]. The matlab transformation matrix generated before the flip is [1 0 0; 0 1 0; 0 0 1].

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [March 4, 2020, 4:06pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/8 "2020-03-04T16:06:35Z")

</div>

I don’t know what kind of transformation that plugin is doing with the function `load_nii`, but it doesn’t seem right, some comments in that plugin page seems to share this opinion.

You get the same results in ITK and Matlab if you use the `load_untouch_nii` function instead, which seems to not perform any extra magic.

 ![compare_matlab_itk_FFT](https://discourse.itk.org/uploads/default/original/2X/3/39fccb4158ca9e399e38f9a3addc20887484127b.png)

[fftReal\_untouch\_tinyCylinder.nii](https://discourse.itk.org/uploads/short-url/1wvy5IYLp4SCIktTBcUDIfDCtta.nii) (31.6 KB) [itk\_fftReal\_tinyCylinder\_nowrappad.nii](https://discourse.itk.org/uploads/short-url/k79GFNfTYMe10EsDKxS53arZ3Ss.nii) (31.6 KB)

Also, I don’t think you are using the WrapPadImageFilter in your itk script correctly. Use the [itkFFTPadImageFilter](https://github.com/InsightSoftwareConsortium/ITK/blob/2302f3ca5ffa7cb38d4d87bc559060bff030133d/Modules/Filtering/FFT/include/itkFFTPadImageFilter.h) instead.  
I have used no pad filter (to match your matlab usage of fftn) and both, the VNL fft and the FFTW fft in ITK, with same results.

I am sure it is an issue in how that plugin is reading/wrtiting the image in matlab. Not in the ITK side.

---

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 4, 2020, 5:26pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/9 "2020-03-04T17:26:26Z")

</div>

I was going to ask about those prime numbers, the link is very welcomed.

I could replicate the results of your answer. Thank you very much !

Unfortunately this toolbox is the only one available to read nifti with a matlab version inferior to 2019 which we don’t have here…

I will be able to continue my work. Thanks again for the time you spent on this.

---

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 6, 2020, 4:09pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/10 "2020-03-06T16:09:53Z")

</div>

Just a quick update.

I was working on making a C++ version of Optimally oriented flux. The algorithm provided by Benmansour [http://insight-journal.org/browse/publication/885](http://insight-journal.org/browse/publication/885) had some weird artefacts that I couldn’t explain. It was probably my attempt at updating the filter from itk V.3 ish to 5.1 that introduced weird things (middle view of the image).

Following the original matlab implementation from Law, seems much better (left view). Thanks again for the help.

A last question, is there an ITK implementation of Bessels functions with float parameters ? VNL bessels implementation only have int parameters. I would like to avoid using Boost::math for this filter.

 ![OOF_comparisons](https://discourse.itk.org/uploads/default/original/2X/2/2ded21ebb82b5ea41e01b341ee176f02b10cd715.png)

PS: The Insight Journal comments are full of spam bot messages, it is becoming worst and worst.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 6, 2020, 4:15pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/11 "2020-03-06T16:15:33Z")

</div>

> [@Jonas](#):
>
> A last question, is there an ITK implementation of Bessels functions with float parameters ?

C++17 now has several Bessel function:  
[https://en.cppreference.com/w/cpp/numeric/special\_functions](https://en.cppreference.com/w/cpp/numeric/special_functions)

Do any of them meet your needs?

Corrected: Those are C++17 functions not C++11. ITK currently only requires C++11, but perhaps for your application C++17 as a requirement would be OK.

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [March 6, 2020, 4:53pm UTC](https://discourse.itk.org/t/fft-mismatch-itk-matlab/2776/12 "2020-03-06T16:53:28Z")

</div>

Unrelated to the original topic, please open a new one.
