-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot_SD_data.py
142 lines (98 loc) · 3.93 KB
/
plot_SD_data.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
Mutatio SD-daten Auswertung by Damian Schneider
"""
import matplotlib.pyplot as plt
import matplotlib.dates as md
import numpy as np
import datetime as dt
import time
import matplotlib.patches as patches
import matplotlib.path as path
#simply plot a file, no adjustments:
#plt.plotfile('/Users/daedae/PycharmProjects/test/netzsinus/2016067.txt', delimiter='\t', cols=(0, 1),
# names=('Frequenz', 'Zeit'), marker='o')
#plt.show()
#plot a file with possibility to reformat the data
#enter path to your file here
with open('/Users/daedae/PycharmProjects/test/netzsinus/2016059_eu.txt') as f:
data = f.read().splitlines()
timestamps = [row.split('\t')[0] for row in data]
timestamps = np.array(timestamps).astype(np.float) #make floats from strings
frequency = [row.split('\t')[1] for row in data]
frequency = np.array(frequency).astype(np.float) #make floats from strings
quality = [row.split('\t')[2] for row in data]
quality = np.array(quality).astype(np.int) #make integers from strings
datetimestamps = [dt.datetime.fromtimestamp(idx) for idx in timestamps]
#Mutatio code bug (is fixed now): overflows erkennen und bereinigen
for i in range(1, len(frequency)):
if(frequency[i]>50.2 and frequency[i-1] < 49.8): #underflow value
while(frequency[i] > 50.0):
frequency[i] = frequency[i]-0.65536
i += 1
if(frequency[i]<49.8 and frequency[i-1] > 50.2): #overflow value
while(frequency[i] < 50.0):
frequency[i] = frequency[i]+0.65536
i += 1
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.set_title("Grid Frequency")
#ax1.set_xlabel('Zeit')
ax.set_ylabel('Frequency')
plt.subplots_adjust(bottom=0.3)
plt.xticks( rotation=25 )
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
ax.plot(datetimestamps,frequency, c='b', label='Frequency')
#leg = ax1.legend() #show the legend label
plt.show()
#histogramm und fourier analyse:
fig = plt.figure(2)
plt.subplots_adjust(hspace=0.4)
ax = fig.add_subplot(211) #plt.subplots()
# histogram our data with numpy
n, bins = np.histogram(frequency, 1000)
# get the corners of the rectangles for the histogram
left = np.array(bins[:-1])
right = np.array(bins[1:])
bottom = np.zeros(len(left))
top = bottom + n
ax.axes.get_yaxis().set_visible(False) #disable drawing the y axis
# we need a (numrects x numsides x 2) numpy array for the path helper
# function to build a compound path
XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T
# get the Path object
barpath = path.Path.make_compound_path_from_polys(XY)
# make a patch out of it
patch = patches.PathPatch(
barpath, facecolor='gold', edgecolor='gold', alpha=0.8)
ax.add_patch(patch)
# update the view limits
ax.set_xlim(left[0], right[-1])
ax.set_ylim(bottom.min(), top.max()+top.max()*0.05)
ax.set_title("Frequency Distribution")
#plt.show()
#fourier analysis
#the problem is uneven spaced time data, lets make it one value per second data using cubic interpolation
from scipy.interpolate import interp1d
from scipy.fftpack import fft
frequency_interp = interp1d(timestamps, frequency) #, kind='cubic'
timestamps_interp = np.linspace(timestamps[0], timestamps[-1], int(timestamps[-1] - timestamps[0]), endpoint=True)
#now we have interpolated data, do the FFT
# Number of samplepoints
N = int(timestamps[-1] - timestamps[0])
# sample spacing
T = 1.0 #1Hz or 1 sample per second
x = np.linspace(0.0, N*T, N)
yf = fft(frequency_interp(timestamps_interp))
xf = np.linspace(0.0, T,N,endpoint=True)
#xf = np.linspace(timestamps[0], timestamps[-1], int(timestamps[-1] - timestamps[0]), endpoint=True)
ax = fig.add_subplot(212)
ax.set_title("Fourier analysis of Frequency")
ax.set_xlabel('Oscillations in the Frequency in [Hz]')
#ax.set_yscale('log')
ax.axes.get_yaxis().set_visible(False) #disable drawing the y axis
ax.axes.get_yaxis().set_visible(True) #disable drawing the y axis
plt.plot(xf[N/15:N/2],np.abs(yf[N/15:N/2]), c='r')
plt.grid()
plt.show()