# Compare ifft in Matlab with Julia for complex input

**URL:** <https://discourse.julialang.org/t/compare-ifft-in-matlab-with-julia-for-complex-input/116002>\
**Category:** Signal and Image Processing\
**Created:** [June 22, 2024, 2:17am UTC](https://discourse.julialang.org/t/compare-ifft-in-matlab-with-julia-for-complex-input/116002 "2024-06-22T02:17:18Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [June 22, 2024, 1:45pm UTC](https://discourse.julialang.org/t/compare-ifft-in-matlab-with-julia-for-complex-input/116002/4 "2024-06-22T13:45:18Z")

</div>

Thanks for the challenge of making a MWE. It turns out that some of my thinking was wrong. This is what I should have been doing:

- Transform to frequency domain
- Set negative frequency components to zero
- Multiply positive frequency components by 2 (do not change zero frequency component and take care with the Nyquist frequency component)
- Transform back to time domain
- Take real part of the analytic signal

Ergo, you have the original signal.

As an example

```julia
using FFTW

# Create signal
dt = 0.2
t = 0:dt:1
y = sin.(2*pi*t)
df = 1/t[end]
yf = fft(y)

# Make signal one sided like rfft
n = length(t)
np = div(n, 2) + 1
yf[np+1:end] = zeros(ComplexF64, n-np)

# Put energy lost from zeroing -ve frequency back in 
if iseven(n)
    yf[2:np-1] = 2*yf[2:np-1]
else
    yf[2:np] = 2*yf[2:np]
end

# Inverse FFT
ynew = ifft(yf)
[y ynew] # compare y with real part of ynew

```

MATLAB does the same thing

---

_[View the full topic](https://discourse.julialang.org/t/compare-ifft-in-matlab-with-julia-for-complex-input/116002)._
