Ich beschäftige mich manchmal mit Zeitreihen und Wellenformdaten, deshalb habe ich versucht, sie zu glätten. Ich werde die drei Methoden zusammen als Memorandum aufschreiben.
Generieren Sie die Wellenformdaten, die dieses Mal verwendet werden sollen
import numpy as np
import matplotlib.pyplot as plt
N = 1024 #Anzahl von Beispielen
dt = 0.001 #Probenahmezyklus[s]
f1, f2 = 30, 90 #Frequenz[Hz]
a1, a2 = 1.5, 0.8 #Amplitude
t = np.arange(0, N*dt, dt) #Zeit[s]
wave = a1*np.sin(2*np.pi*f1*t) + a2*np.sin(2*np.pi*f2*t) + 0.3 * np.random.randn(N) #Signal
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, wave)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.title("wave")
plt.show()
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, wave)
ax.set_xlim(0, 0.1)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.title("enlarged view")
plt.show()
window = 5 #Bereich des gleitenden Durchschnitts
w = np.ones(window)/window
x = np.convolve(wave, w, mode='same')
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x, label='moving average')
ax.plot(t, wave, alpha=0.3, label='wave')
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.legend()
plt.title("moving average window=5")
plt.show()
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x, label='moving average')
ax.plot(t, wave, alpha=0.3, label='wave')
ax.set_xlim(0, 0.1)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.legend()
plt.title("enlarged view")
plt.show()
import scipy.signal
#Die Reihenfolge der polymorphen: 2
#Rahmenlänge: 5
x = scipy.signal.savgol_filter(wave, 5, 2, deriv=0)
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x, label='Savitzky-Golay')
ax.plot(t, wave, alpha=0.3, label='wave')
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.legend()
plt.title("Savitzky-Golay deriv=0")
plt.show()
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x, label='Savitzky-Golay')
ax.plot(t, wave, alpha=0.3, label='wave')
ax.set_xlim(0, 0.1)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.legend()
plt.title("enlarged view")
plt.show()
x = scipy.signal.savgol_filter(wave, 5, 2, deriv=1)
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.title("Savitzky-Golay deriv=1")
plt.show()
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x)
ax.set_xlim(0, 0.1)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.title("enlarged view")
plt.show()
x = scipy.signal.savgol_filter(wave, 5, 2, deriv=2)
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.title("Savitzky-Golay deriv=2")
plt.show()
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x)
ax.set_xlim(0, 0.1)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.title("enlarged view")
plt.show()
3.1 Fast Fourier Transform
x = np.fft.fft(wave)
x = np.abs(x) #Konvertieren Sie komplexe Zahlen in absolute Werte
x = x / N * 2 #Einstellen der Amplitude
fq = np.linspace(0, 1.0/dt, N) #Frequenzanpassung
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(fq[: int(N / 2)], x[: int(N / 2)])
ax.set_xlabel("Frequency [Hz]")
ax.set_ylabel("Swing")
ax.grid()
plt.title("Fast Fourier Transform")
plt.show()
#Frequenz 30,90, Amplitude 1.5, 0.Peak ist um 8 zu sehen
3.2 Inverse Fast Fourier Transform without Noise
threshold = 0.6 #Größenschwelle
x = np.fft.fft(wave)
x_abs = np.abs(x)
x_abs = x_abs / N * 2
x[x_abs < threshold] = 0
x = np.fft.ifft(x)
x = x.real #Extrahieren Sie nur den Realteil aus einer komplexen Zahl
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x, label='IFFT')
ax.plot(t, wave, alpha=0.3, label='wave')
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.legend()
plt.title("Inverse Fast Fourier Transform")
plt.show()
fig, ax = plt.subplots(figsize=(14.0, 6.0))
ax.plot(t, x, label='IFFT')
ax.plot(t, wave, alpha=0.3, label='wave')
ax.set_xlim(0, 0.1)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Signal")
ax.grid()
plt.legend()
plt.title("enlarged view")
plt.show()
Wenn ich Zeit habe, möchte ich den theoretischen Teil hinzufügen und andere Methoden beschreiben.
Recommended Posts