I'm experimenting with the Hilbert transform in Python and just now understanding how potentially severe the edge effects are. Here is the code I'm using:
import numpy as np
import scipy.signal
import matplotlib.pyplot as plt
option = 1
if option == 1:
x = np.sin(np.arange(100))
elif option == 2:
x = np.sin(np.arange(100)*np.pi/10)
elif option == 3:
x = np.sin(np.arange(100)/4)
hilbert_data = scipy.signal.hilbert(x, axis=0)
real = hilbert_data.real
imag = hilbert_data.imag
amp_data = np.absolute(hilbert_data)
phase_data = np.unwrap(np.angle(hilbert_data), axis=0)
freq_data = np.diff(phase_data, axis=0)
fig, axs = plt.subplots(2, 2, figsize=(12,5))
axs[0, 0].plot(x)
axs[0, 0].set_title('Signal')
axs[0, 1].plot(amp_data, 'tab:orange')
axs[0, 1].set_title('Amplitude')
axs[0, 1].set_ylim([0.8,1.2])
axs[1, 0].plot(phase_data, 'tab:green')
axs[1, 0].set_title('Phase')
axs[1, 1].plot(freq_data, 'tab:red')
axs[1, 1].set_title('Frequency')
These are the outputs when I run it with options 1, 2, and 3, respectively:
What's noticeable is how different the edge effects are in each example. With option 2, there are essentially no edge effects. With option 1, the edge effects on amplitude are oscillatory and frequency spikes up at the ends. With option 3, it's the opposite - amplitude spikes and frequency is oscillatory.
Assuming that there is reason to believe that a signal is relatively consistent (e.g., the example above is based on signal with uniform amplitude/frequency over time), is there a way to use the outputs in the middle to correct the outputs on the ends? In the example above, it might make sense to just take the amplitude/frequency from the the centermost point, however, for real signals, that would likely be inaccurate. The difficulty is that the edge effects appear to be highly sensitive to small changes in the input as with the above.
Are there ways to determine the instantaneous amplitude, frequency, and phase other than the Hilbert transform? Any thoughts are appreciated.






scipy.signal.hilbertreturns the analytic signal and then the instantaneous values are determined using numpy functions. – SuperCodeBrah Jul 29 '22 at 16:41