Understanding Analytic Signal and Hilbert Transform

Key focus of this article: Understand the relationship between analytic signal, Hilbert transform and FFT. Hands-on demonstration using Python and Matlab.

Introduction

Fourier Transform of a real-valued signal is complex-symmetric. It implies that the content at negative frequencies are redundant with respect to the positive frequencies. In their works, Gabor [1] and Ville [2], aimed to create an analytic signal by removing redundant negative frequency content resulting from the Fourier transform. The analytic signal is complex-valued but its spectrum will be one-sided (only positive frequencies) that preserved the spectral content of the original real-valued signal. Using an analytic signal instead of the original real-valued signal, has proven to be useful in many signal processing applications. For example, in spectral analysis, use of analytic signal in-lieu of the original real-valued signal mitigates estimation biases and eliminates cross-term artifacts due to negative and positive frequency components [3].

This article is part of the following books
Digital Modulations using Matlab : Build Simulation Models from Scratch, ISBN: 978-1521493885
Digital Modulations using Python ISBN: 978-1712321638
Wireless communication systems in Matlab ISBN: 979-8648350779
All books available in ebook (PDF) and Paperback formats

Continuous-time analytic signal

Let x(t) be a real-valued non-bandlimited finite energy signal, for which we wish to construct a corresponding analytic signal z(t).  The Continuous Time Fourier Transform of x(t) is given by

X(f) = \displaystyle{ \int_{-\infty}^{\infty}} x(t) e^{-j 2 \pi f t} dt  \quad (1)

Lets say the magnitude spectrum of X(f) is as shown in Figure 1(a). We note that the signal x(t) is a real-valued and its magnitude spectrum |X(f)| is symmetric and extends infinitely in the frequency domain.

(a) Spectrum of continuous signal x(t) and (b) spectrum of analytic signal z(t)
Figure 1: (a) Spectrum of continuous signal x(t) and (b) spectrum of analytic signal z(t)

As mentioned in the introduction, an analytic signal can be formed by suppressing the negative frequency contents of the Fourier Transform of the real-valued signal. That is, in frequency domain, the spectral content Z(f) of the analytic signal z(t) is given by

Z(f) = \begin{cases} \;X(0) & for \; f=0 \\ 2X(f) & for \; f>0 \\ \;\;\;0 & for \; f<0 \end{cases}  \quad (2)  

The corresponding spectrum of the resulting analytic signal is shown in Figure 1(b).

Since the spectrum of the analytic signal is one-sided, the analytic signal will be complex valued in the time domain, hence the analytic signal can be represented in terms of real and imaginary components as z(t) = z_r(t) + j z_i(t). Since the spectral content is preserved in an analytic signal, it turns out that the real part of the analytic signal in time domain is essentially the original real-valued signal itself (z_r(t) = x(t)). Then, what takes place of the imaginary part ? Who is the companion to x(t) that occupies the imaginary part in the resulting analytic signal ? Summarizing as equation,

z(t) = z_r(t) + j z_i(t) \quad\quad (3) \\ z_r(t) = x(t) \quad z_i(t) = \; ??

It is interesting to note that Hilbert transform [4] can be used to find a companion function (imaginary part in the equation above) to a real-valued signal such that the real signal can be analytically extended from the real axis to the upper half of the complex plane . Denoting Hilbert transform as HT\{\}, the analytic signal is given by

z(t) = z_r(t) + j z_i(t) = x(t) + j HT{x(t)}  \quad (4)  

From these discussion, we can see that an analytic signal z(t) for a real-valued signal x(t), can be constructed using two approaches.

●  Frequency domain approach: The one-sided spectrum of z(t) is formed from the two-sided spectrum of the real-valued signal x(t) by applying equation (2)
● Time domain approach: Using Hilbert transform approach given in equation (4)

One of the important property of an analytic signal is that its real and imaginary components are orthogonal

\displaystyle{\int_{-\infty}^{\infty} z_i(t) z_r(t) = 0}\quad (5)

Discrete-time analytic signal

Since we are in digital era, we are more interested in discrete-time signal processing. Consider a continuous real-valued signal x(t) gets sampled at interval T seconds and results in N real-valued discrete samples x[n], i.e, x[n] = x(nT) . The spectrum of the continuous signal is shown in Figure 2(a). The spectrum of x[n] that results from the process of periodic sampling is given in Figure 2(b) (Refer here more details on the process of sampling).  The spectrum of discrete-time signal x[n] can be obtained by Discrete-Time Fourier Transform (DTFT).

X(f) = \displaystyle{ T \sum_{n=0}^{N-1} x[n] e^{-j 2 \pi f n T} }\quad (6)  

(a) CTFT of continuous signal x(t), (b) Spectrum of x[n] resulted due to periodic sampling and (c) Periodic one-sided spectrum of analytical signal z[n]
Figure 2: (a) CTFT of continuous signal x(t), (b) Spectrum of x[n] resulted due to periodic sampling and (c) Periodic one-sided spectrum of analytical signal z[n]

At this point, we would like to construct a discrete-time analytic signal z[n] from the real-valued sampled signal x[n]. We wish the analytic signal is complex valued z[n] = z_r[n] + j z_i[n] and should satisfy the following two desired properties

● The real part of the analytic signal should be same as the original real-valued signal. z_r[n] = x[n]
● The real and imaginary part of the analytic signal should satisfy the following property of orthogonality

\displaystyle{ \sum_{n=0}^{N-1} z_r[n] z_i[n] = 0 }

In Frequency domain approach for the continuous-time case, we saw that an analytic signal is constructed  by suppressing the negative frequency components from the spectrum of the real signal. We cannot do this for our periodically sampled signal x[n]. Periodic mirroring nature of the spectrum prevents one from suppressing the negative components. If we do so, it will vanish the entire spectrum. One solution to this problem is to set the negative half of each spectral period to zero. The resulting spectrum of the analytic signal is shown in Figure 2(c).

Given a record of samples x[n] of even length N, the procedure to construct the analytic signal z[n] is as follows. This method satisfies both the desired properties listed above.

● Compute the N-point DTFT of x[n] using FFT
● N-point periodic one-sided analytic signal is computed by the following transform

equation for constructing analytic signal in frequency domain

● Finally, the analytic signal (z[n]) is obtained by taking the inverse DTFT of Z[m]

z[n] = \displaystyle{ \frac{1}{NT} \sum_{m=0}^{N-1} z[m] \; exp \left( j 2 \pi mn/N\right)}

Matlab

The given procedure can be coded in Matlab using the FFT function. Given a record of N real-valued samples x[n], the corresponding analytic signal z[n] can be constructed as given next. Note that the Matlab has an inbuilt function to compute the analytic signal. The in-built function is called hilbert.

function z = analytic_signal(x)
%x is a real-valued record of length N, where N is even %returns the analytic signal z[n]
x = x(:); %serialize
N = length(x);
X = fft(x,N);
z = ifft([X(1); 2*X(2:N/2); X(N/2+1); zeros(N/2-1,1)],N);
end

To test this function, we create a 5 seconds record of a real-valued sine signal. The analytic signal is constructed and the orthogonal components are plotted in Figure 3. From the plot, we can see that the real part of the analytic signal is exactly same as the original signal (which is the cosine signal) and the imaginary part of the analytic signal is -90 ^\circ phase shifted version of the original signal. We note that the imaginary part of the analytic signal is a cosine function with amplitude scaled by -1 which is none other than the Hilbert transform of sine function.

t=0:0.001:0.5-0.001;
x = sin(2*pi*10*t); %real-valued f = 10 Hz
subplot(2,1,1); plot(t,x);%plot the original signal
title('x[n] - original signal'); xlabel('n'); ylabel('x[n]');

z = analytic_signal(x); %construct analytic signal
subplot(2,1,2); plot(t, real(z), 'k'); hold on;
plot(t, imag(z), 'r');
title('Components of Analytic signal'); 
xlabel('n'); ylabel('z_r[n] and z_i[n]');
legend('Real(z[n])','Imag(z[n])');

Python

Equivalent code in Python is given below (tested with Python 3.6.0)

import numpy as np
def main():
    t = np.arange(start=0,stop=0.5,step=0.001)
    x = np.sin(2*np.pi*10*t)
    
    import matplotlib.pyplot as plt
    plt.subplot(2,1,1)
    plt.plot(t,x)
    plt.title('x[n] - original signal')
    plt.xlabel('n')
    plt.ylabel('x[n]')
    
    z = analytic_signal(x)
    
    plt.subplot(2,1,2)
    plt.plot(t,z.real,'k',label='Real(z[n])')
    plt.plot(t,z.imag,'r',label='Imag(z[n])')
    plt.title('Components of Analytic signal')
    plt.xlabel('n')
    plt.ylabel('z_r[n] and z_i[n]')
    plt.legend()

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()
Components of analytic signal for a real-valued cosine function via Hilbert transform
Figure 3: Components of analytic signal for a real-valued sine function

Hilbert Transform using FFT

We should note that the hilbert function in Matlab returns the analytic signal $latex z[n]$ not the hilbert transform of the signal x[n]. To get the hilbert transform, we should simply get the imaginary part of the analytic signal. Since we have written our own function to compute the analytic signal, getting the hilbert transform of a real-valued signal goes like this.

x_hilbert = imag(analytic_signal(x))

In the coming posts, we will some of the applications of constructing an analytic signal. For example: Find the instantaneous amplitude and phase of a signal, envelope detector for an amplitude modulated signal, detecting phase changes in a sine wave.

Rate this article: PoorBelow averageAverageGoodExcellent (26 votes, average: 3.85 out of 5)

References:

[1] D. Gabor, “Theory of communications”, Journal of the Inst. Electr. Eng., vol. 93, pt. 111, pp. 42-57, 1946. See definition of complex signal on p. 432.↗
[2] J. A. Ville, “Theorie et application de la notion du signal analytique”, Cables el Transmission, vol. 2, pp. 61-74, 1948.↗
[3] S. M. Kay, “Maximum entropy spectral estimation using the analytical signal”, IEEE transactions on Acoustics, Speech, and Signal Processing, vol. 26, pp. 467-469, October 1978.↗
[4] Frank R. Kschischang, “The Hilbert Transform”, University of Toronto, October 22, 2006.↗
[5] S. L. Marple, “Computing the discrete-time ‘analytic’ signal via FFT,” Conference Record of the Thirty-First Asilomar Conference on Signals, Systems and Computers , Pacific Grove, CA, USA, 1997, pp. 1322-1325 vol.2.↗

Topics in this chapter

Essentials of Signal Processing
● Generating standard test signals
 □ Sinusoidal signals
 □ Square wave
 □ Rectangular pulse
 □ Gaussian pulse
 □ Chirp signal
Interpreting FFT results - complex DFT, frequency bins and FFTShift
 □ Real and complex DFT
 □ Fast Fourier Transform (FFT)
 □ Interpreting the FFT results
 □ FFTShift
 □ IFFTShift
Obtaining magnitude and phase information from FFT
 □ Discrete-time domain representation
 □ Representing the signal in frequency domain using FFT
 □ Reconstructing the time domain signal from the frequency domain samples
● Power spectral density
Power and energy of a signal
 □ Energy of a signal
 □ Power of a signal
 □ Classification of signals
 □ Computation of power of a signal - simulation and verification
Polynomials, convolution and Toeplitz matrices
 □ Polynomial functions
 □ Representing single variable polynomial functions
 □ Multiplication of polynomials and linear convolution
 □ Toeplitz matrix and convolution
Methods to compute convolution
 □ Method 1: Brute-force method
 □ Method 2: Using Toeplitz matrix
 □ Method 3: Using FFT to compute convolution
 □ Miscellaneous methods
Analytic signal and its applications
 □ Analytic signal and Fourier transform
 □ Extracting instantaneous amplitude, phase, frequency
 □ Phase demodulation using Hilbert transform
Choosing a filter : FIR or IIR : understanding the design perspective
 □ Design specification
 □ General considerations in design

Books by the author

Wireless Communication Systems in Matlab
Wireless Communication Systems in Matlab
Second Edition(PDF)

PoorBelow averageAverageGoodExcellent (162 votes, average: 3.78 out of 5)

Digital modulations using Python
Digital Modulations using Python
(PDF ebook)

PoorBelow averageAverageGoodExcellent (123 votes, average: 3.60 out of 5)

digital_modulations_using_matlab_book_cover
Digital Modulations using Matlab
(PDF ebook)

PoorBelow averageAverageGoodExcellent (126 votes, average: 3.70 out of 5)

Hand-picked Best books on Communication Engineering
Best books on Signal Processing

4 thoughts on “Understanding Analytic Signal and Hilbert Transform”

  1. Hi. Thank you so much for this interesting post. I just have a question about the python script.
    It seems you made fft on a real value signal and got a complex vector as a result. Then at the end you reversed it by ifft. I expected to get again a real value vector, but surprisingly the output was complex too. I guess the zeros you added at the bottom of “h” caused it, but how? can you please explain a bit about how we got a complex vector at the output?

    Thanks a million!

    Reply
  2. Many people writing CODE for signal processing use FFTs and HILBERT Transforms.
    I have read descriptions of the HILBERT Transform. And I think that a little less math (and more words about how to operate on the complex numbers of the FFT bin locations , would be more useful. As an example, a HILBERT transform can be implemented by : taking the FFT of a timedomain signal, visit every bin of the FFT array, (set BIN 0] to ZERO. Then, visit each BIN , one at a time. swap the REALP value with the IMAGP (and then multiply the REALP by -1). Do this at each BIN. Then TAKE the INVERSE FFT of the modified frequency domain array to get the 90-degree phase shifted signal. Or take the FFT of a timedomain signal, (set BIN 0] to ZERO. Then, visit each BIN , one at a time, from BIN(N/2 +1) to BIN (N) and set the REALP to ZERO and the IMAGP to zero. Then TAKE the INVERSE FFT of the modified frequency domain array to get the 90-degree phase shifted signal. It is difficult to progress from integrals to what to do (in C or PYTHON) with the AMPLITUDE SAMPLES of a timedomain signal or what to do with the REALP and IMAGP of every complex number of the FFT BIN. It would be nice if the authors wrote the INTEGRALS and some pseudo-code. Thank you. Cesar in California

    Reply
  3. Hello, thank you for sharing this. I am having a problem with the instantaneous phase I am getting out of the hilbert(.) function in MATLAB, as well as when I do not use fft/ifft to get the analytical phase. So the general question is, does my phase get distorted when I use Hilbert Transform on a random signal?

    More elaborate example on this is, when I generate a random signal of length T, and then keep this random signal fixed, then I generate, say, few more random signal of length S, and then concatenate it with my original signal of length T, I do see that there is slight phase distortion at the beginning of my signal. In other words, why concatenating some signal to the end of my original signal distorts the instantaneous phase, in particular, at the beginning?

    Reply

Post your valuable comments !!!