-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautokorrelation.py
More file actions
26 lines (20 loc) · 870 Bytes
/
autokorrelation.py
File metadata and controls
26 lines (20 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding: utf-8 -*-
"""Berechnen der Autokorrelationsfunktion einer Datenreihe. """
import matplotlib.pyplot as plt
import numpy as np
# Erstellen eines Cosinus mit Rauschen
x = np.linspace(0, 25, 200)
y = 2 * np.cos(2 * x) + np.random.normal(size=x.size)
# Die Autkorrelation wird mit Hilfe der Funktion correlate() gebildet.
# Das Ergebnis wird direkt skaliert.
ac = np.correlate(y, y, mode='full') / np.sum(y**2)
# Durch den Parameter mode='full' wird die Korrelationsfunktion
# beidseitig bestimmt. Im folgenden Schritt wird die erste Hälfte des
# Vektors abgeschnitten. (Sprich: "Von der Hälfte der Länge des Vektors
# bis zu seinem Ende")
ac = ac[int(ac.size / 2):]
# Plotten der Zeitreihe und der Autkokorrelation
fig, ax = plt.subplots()
ax.plot(x, y, 'r', x, ac, 'b')
ax.legend(['Messreihe', 'Autokorrelation'])
plt.show() # Anzeigen des Plots