# How to get a real signal from complex ifft values?

I am trying to understand the difference between DCT and FFT in the context of compression. There is a nice practical example given here. (near the end of the page)

When I try it in matlab I am stuck at plotting the ifft values (which are complex). The plot command cannot plot the complex signal. One natural question comes to my mind, when is the ifft value complex, and when is it real? If it is complex, is it fine to discard the imaginary parts and keep only the real part?

What is the right way to reconstruct the real signal x from ifft complex values?

• you should use only real values after taking ifft, discard the imaginary part and plot. – arpit jain Jan 29 '17 at 18:25
• Can you please state a specific question? It's difficult to see what you're having trouble with. Also: why do you zero some elements of the transform? Why are you using 2-D transforms on 1-D signals? – MBaz Jan 29 '17 at 18:26

You can plot the real and imaginary components from the FFT result separately (or the magnitude and the phase results separately, as two plots). Then you can recombine the data from the two plots into a complex spectrum required to do a reconstruction IFFT. Both components are required, otherwise you've thrown away half the information about your signal, and can't reconstruct it (unless the original signal was purely symmetric or purely anti-symmetric to begin with).

The plot data isn't really doubled when you do two plots because the result of an FFT of strictly real inputs in conjugate symmetric. Thus you can create the input for your reconstruction IFFT from only the first half of the data for the real plot and for the imaginary plot (by conjugate mirroring it to the other half).

• Can I simply ignore the complex parts and consider the real parts as my reconstructed signal? – Sounak Jan 29 '17 at 18:36
• Only if your original signal was purely symmetric. Otherwise any variance from perfect symmetry will be thrown away (that's what's in the imaginary component of the complex FFT result), and thus the signal can no longer be reconstructed if you ignore that part. – hotpaw2 Jan 29 '17 at 18:39
• Then, how do I reconstruct the real signal if it is asymmetric? – Sounak Jan 29 '17 at 18:45
• Symmetry, for fft() input x, means that, for every i > 0, x[i] = x[N-i], where N is the fft length. – hotpaw2 Jan 29 '17 at 19:42
• That the ifft(fft(x)) result ends up complex might be because your ifft input of strictly real fft input wasn't kept conjugate symmetric, e.g. you messed up your ifft input by truncating or zeroing half of it or something. – hotpaw2 Jan 29 '17 at 19:51

You can plot the Inverse Fourier Transform by taking the real-part using the following code (on MATLAB):

image = imread('image.jpg');
image = rgb2gray(image);
figure; imshow(image); title('Original Image')

Fourier_Transform = fft2(double(image));
Fourier_Transform_shift = log(abs(fftshift(Fourier_Transform)+1));
Fourier_Transform_normalized = Fourier_Transform_shift/max(max(max(Fourier_Transform_shift)));
figure; imshow(Fourier_Transform_normalized); title('Fourier Transform of the Original Image')

Inverse_Fourier_Transform = ifft2(Fourier_Transform);
%figure; imshow(iff); title ('Inverse Fourier Transform')

Inverse_Fourier_Transform_Real = uint8(real(Inverse_Fourier_Transform));
figure; imshow(Inverse_Fourier_Transform_Real); title ('Restored Image (Real Part of IFFT)')


For most parts, the restored signal or image will look exactly like the original.

I would like to redraw the context. Most digital data (signals or images) where DCT or FFT is useful is discrete, both in index (time, space) and in amplitude (bit-depth in integers, $0-255$ for grayscale images). For compression purposes, very little useful linear transformations map $8$-bit data to sparse/compressible $n$-bit coefficients. For DCT and FFT, involving sine/cosine transcendental numbers, hence non dyadic rationals, its is quite difficult to get strict invertible or lossless transform. Te remainder is often quite small. Very often, doing a DCT on integer data, then IDCT yields non-integer data that should be rounded.

The same story happens with complex transforms. From a real data, the transformed and inverse may look complex, but generally the imaginary part is small enough. If it is not, it is a sign that there is a mistake in the code.

Small enough is complicated. My option is to choose, for $B$-bit data, a threshold on the order of $1/2^B$ in the ratio of the real over the imaginary part. If meet, consider the imaginary part negligible, and invert only from the real one.