Signal- und Messdatenverarbeitung


Testcodes für Kapitel 14: Hilbert-Transformation

systematische Zusammenstellung von kombinierbaren Programmabschnitten zur Signal- und Messdatenverarbeitung

Python Matlab/Octave
Vorbereitung (Laden von Modulen/Paketen)
#python -m pip install --upgrade numpy
#python -m pip install --upgrade matplotlib
#python -m pip install --upgrade scipy
#pip install --upgrade numpy
#pip install --upgrade matplotlib
#pip install --upgrade scipy
from numpy import *
from numpy.fft import *
from matplotlib.pyplot import *
from scipy.signal import *
harmonisches Signal mit Gauß-Einhüllender
N=300
dt=1.0
t=arange(0,N)*dt
x=exp(-(t-150)**2/75.0**2)*cos(2*pi*t/45.0)
plot(t,x)
show()
N=300;
dt=1;
t=(0:N-1)*dt;
x=exp(-(t-150).^2/75^2).*cos(2*pi*t/45);
plot(t,x)
Hilbert-Transformation

mit der Funktion hilbert in scipy.signal

hx=hilbert(x)
#Das ist bereits das analytische Signal mit dem Originalsignal
#im Realteil und der Hilbert-Transformierten im Imaginaerteil.
plot(t,real(hx),t,imag(hx))
show()
h=real(-1j*(hx-x))
plot(t,x,t,h,'--')
show()

… oder mittels der DFT, Phasendrehung und IDFT

X=fft(x)
X[0]=0.0
for i in range(1,int(floor((N-1)/2.0))+1):
  X[i]*=-1j

for i in range(int(ceil((N+1)/2.0)),N):
  X[i]*=+1j

h=real(ifft(X)) # Das ist die Hilbert-Transformierte.
plot(t,x,t,h,'--')
show()
hx=x+1j*h       # Das ist das analytische Signal.
plot(t,real(hx),t,imag(hx))
show()
X=fft(x);
X(1)=0;
for i=2:fix((length(x)+1)/2)
X(i)=-1j*X(i);
end
for i=fix((length(x)+3)/2):fix((length(x)+2)/2)
X(i)=0;
end
for i=fix((length(x)+4)/2):length(x)
X(i)=+1j*X(i);
end
h=ifft(X); % Das ist die Hilbert-Transformierte.
hx=x+1j*h; % Das ist das analytische Signal.
plot(t,real(hx),t,imag(hx))
Hüllkurve
plot(t,real(hx),t,imag(hx),t,abs(hx))
show()
plot(t,real(hx),t,imag(hx),t,abs(hx))
Frequenzgemisch
N=300
dt=1.0
t=arange(0,N)*dt
x=exp(-(t-150)**2/75.0**2)*(cos(2*pi*t/45.0)+0.4*cos(2*pi*t/35.0)+0.8*cos(2*pi*t/30.0))
plot(t,x)
show()

mit der Funktion hilbert in scipy.signal

hx=hilbert(x) # Das ist bereits das analytische Signal.
plot(t,real(hx),t,imag(hx),t,abs(hx))
show()

… oder mittels der DFT, Phasendrehung und IDFT

X=fft(x)
X[0]=0.0
for i in range(1,int(floor((N-1)/2.0))+1):
  X[i]*=-1j

for i in range(int(ceil((N+1)/2.0)),N):
  X[i]*=+1j

h=ifft(X) # Das ist die Hilbert-Transformierte.
hx=x+1j*h # Das ist das analytische Signal.
plot(t,real(hx),t,imag(hx),t,abs(hx))
show()
N=300;
dt=1;
t=(0:N-1)*dt;
x=exp(-(t-150).^2/75^2).*(cos(2*pi*t/45)+0.4*cos(2*pi*t/35)+0.8*cos(2*pi*t/30));
plot(t,x)
waitforbuttonpress
X=fft(x);
X(1)=0;
for i=2:fix((length(x)+1)/2)
X(i)=-1j*X(i);
end
for i=fix((length(x)+3)/2):fix((length(x)+2)/2)
X(i)=0;
end
for i=fix((length(x)+4)/2):length(x)
X(i)=+1j*X(i);
end
h=ifft(X); % Das ist die Hilbert-Transformierte.
hx=x+1j*h; % Das ist das analytische Signal.
plot(t,real(hx),t,imag(hx),t,abs(hx))
komplexe Signale
N=300
dt=1.0
t=arange(0,N)*dt
x=exp(-(t-150)**2/75.0**2)*(cos(2*pi*t/45.0)+1j*cos(2*pi*t/45.0+0.5))
plot(t,real(x),t,imag(x))
show()

Leider ist die HT bei SciPy nur für reelle Signale implementiert. Für komplexe Signale muss man die Signalteile getrennt berechnen …

hx=hilbert(real(x))+1j*hilbert(imag(x))
h=-1j*(hx-x)
plot(t,real(x),t,imag(x),t,real(h),'--',t,imag(h),'--')
show()

… oder mittels der DFT, Phasendrehung und IDFT.

X=fft(x)
X[0]=0.0
for i in range(1,int(floor((N-1)/2.0))+1):
  X[i]*=-1j

for i in range(int(ceil((N+1)/2.0)),N):
  X[i]*=+1j

h=ifft(X) # Das ist die Hilbert-Transformierte.
plot(t,real(x),t,imag(x),t,real(h),'--',t,imag(h),'--')
show()
N=300;
dt=1;
t=(0:N-1)*dt;
x=exp(-(t-150).^2/75^2).*(cos(2*pi*t/45)+1j*cos(2*pi*t/45+0.5));
plot(t,real(x),t,imag(x))
waitforbuttonpress
X=fft(x);
X(1)=0;
for i=2:fix((length(x)+1)/2)
X(i)=-1j*X(i);
end
for i=fix((length(x)+3)/2):fix((length(x)+2)/2)
X(i)=0;
end
for i=fix((length(x)+4)/2):length(x)
X(i)=+1j*X(i);
end
h=ifft(X);
plot(t,real(x),t,imag(x),t,real(h),'--',t,imag(h),'--')