# How an algorithm for measuring the analytic signal (Hilbert transform) gives us complex value at the end?

I have a question about the following codes for measuring analytic signal (Hilbert transform). Here,the time domain signal (x) is a real value signal. After making fft it becomes complex (X). Then it is multiplied by h (a real vector with the same size as X, and having real number in upper half and 0 in the bottom half). X*.h is complex obviously. Then we made ifft on X*.h , but surprisingly the output is again a complex signal (while we expected it to be real). How did that happen? Can anybody please explain it to me? Is h doing something special? In other words, we multiply the real signal with exp-j(2*pi/k) during fft, and multiply it one more time with exp j(2*pi/k) during ifft. These two should kill each other and give us a real output, isn't it?

def main():
t = np.arange(start=0,stop=0.5,step=0.001)
x = np.sin(2*np.pi*10*t)

z = analytic_signal(x)

def analytic_signal(x):
from scipy.fftpack import fft,ifft
N = len(x)
X = fft(x,N)
h = np.zeros(N)
h[0] = 1
h[1:N//2] = 2*np.ones(N//2-1)
h[N//2] = 1
Z = X*h
z = ifft(Z,N)
return z

if __name__ == '__main__':
main()

• Why do you expect ifft(X*h) to be real? Keep in mind that for ifft(Z) to be real, Z must satifsy certain specific properties. – MBaz May 29 at 20:24

(while we expected it to be real).

We certainly did not expect this to be real. Quite on the contrary: a real time domain signal has a spectrum that is conjugate symmetric. Be zeroing out the negative frequency we clearly broke this symmetry and that means that the analytical signal MUST be complex (other than the trivial case a $$x[n] = const$$)

• I understand the point you made.My question is how that mathematically happens? Shouldn't the exp-j(2*pi/k) during fft, and exp j(2*pi/k) during ifft remove each other? – Masoud Za May 30 at 20:32
• See Dan's answer. I think he explains this well – Hilmar May 31 at 11:43

In general for the FT, in order for the result for the DFT or IDFT to be real, the waveform MUST be real and even (symmetric about vertical axis) or imaginary and odd (anti-symmetric about the vertical axis), or made up of the sum of such components.

A simple example with a cosine and continuous-time Fourier Transform as detailed below should clear this up as a generalized Fourier property, and this readily extends to the DFT by showing that frequency indexes in the upper half of the spectrum are equivalent to negative frequencies: $$e^{-jnk/N} = e^{j(N-n)k/N}$$.

Consider the waveform $$cos(\omega_1 t)$$ which with Euler's formula is also $$0.5e^{j\omega_1 t} + 0.5e^{-j\omega_1 t}$$ each as a complex phasor rotating in time as depicted in the plot below. Each of these phasors represents a single impulse in Frequency (as the transform of $$e^{j\omega_1 t} \rightarrow 2\pi\delta(\omega-\omega_1)$$) but importantly note how in time they add to always be real (consistent with $$cos(\omega t)$$ as real. Each phasor below maps to a positive and negative frequency according to the direction of their rotation, and by rotating in equal and opposite directions, the sum will always land on the real axis.

If you remove one of these components in the frequency domain, the inverse transform will equivalently result in only one spinning phasor, which will be a complex number. For example if we remove the negative frequency component in the Fourier Transform, the result will be $$e^{j\omega t} = \cos(\omega t) + j \sin (\omega t)$$.

• Hilmar and Dan, thank you so much. Now I realized! – Masoud Za Jun 1 at 15:50
• @MasoudZa Glad it all helped. Unless you disagree now with Hilmar's answer you should mark it as correct to properly close out this question (he answered first and concisely, I just added more color.) Also his point about a constant being a trivial case is very good! – Dan Boschen Jun 1 at 23:09