-
Notifications
You must be signed in to change notification settings - Fork 2
/
Data_smooth.py
136 lines (96 loc) · 3.42 KB
/
Data_smooth.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
import numpy
import Data_load as dl
def smooth(x, window_len=100, window='hanning'):
"""smooth the data using a window with requested size.
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
input:
x: the input signal
window_len: the dimension of the smoothing window; should be an odd integer
window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
flat window will produce a moving average smoothing.
output:
the smoothed signal
example:
t=linspace(-2,2,0.1)
x=sin(t)+randn(len(t))*0.1
y=smooth(x)
see also:
numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
scipy.signal.lfilter
TODO: the window parameter could be the window itself if an array instead of a string
NOTE: length(output) != length(input), to correct this: return y[(window_len/2-1):-(window_len/2)] instead of just y.
"""
s = numpy.r_[x[window_len - 1:0:-1], x, x[-2:-window_len - 1:-1]]
# print(len(s))
if window == 'flat': # moving average
w = numpy.ones(window_len, 'd')
else:
w = eval('numpy.' + window + '(window_len)')
y = numpy.convolve(w / w.sum(), s, mode='valid')
return y
from numpy import *
from pylab import *
import os
import random
current_path = os.getcwd()
DataX = list()
DataY = list()
Data_Animal_X = list()
Data_Animal_Y = list()
Data_Both_X = list()
Data_Both_Y = list()
Data_sun_human = list()
Data_sun_human_Y = list()
Data_rain_human = list()
Data_rain_human_Y = list()
Data_sun_none = list()
Data_sun_none_Y = list()
Data_rain_none = list()
Data_rain_none_Y = list()
dl.Data_load("Sunny", "Animal", 3, Data_Animal_X, Data_Animal_Y)
dl.Data_load("Sunny", "Human", 3, DataX, DataY)
#dl.Data_load("Sunny", "Both", 3, Data_Both_X, Data_Both_Y)
dl.Data_load("Sunny", "Human", 3, Data_sun_human, Data_sun_human_Y)
dl.Data_load("Rain", "Human", 3, Data_rain_human, Data_rain_human_Y)
dl.Data_load("Sunny", "None", 3, Data_sun_none, Data_sun_none_Y)
dl.Data_load("Rain", "None", 3, Data_rain_none, Data_rain_none_Y)
sun = list()
rain = list()
sun_ani = list()
sun_none = list()
rain_none = list()
for i in range(0,10):
j = random.randrange(0, Data_sun_human.__len__())
sun.append(Data_sun_human[j])
j = random.randrange(0, Data_rain_human.__len__())
rain.append(Data_rain_human[j])
j = random.randrange(0, Data_Animal_X.__len__())
sun_ani.append(Data_Animal_X[j])
j = random.randrange(0, Data_sun_none.__len__())
sun_none.append(Data_sun_none[j])
j = random.randrange(0, Data_rain_none.__len__())
rain_none.append(Data_rain_none[j])
print(j)
def smooth_data(datalist, plot_title):
for i in range(0,10):
t = linspace(-4, 4, 100)
x = datalist[i]
xn = x + randn(len(t)) * 0.1
y = smooth(x)
ws = 31
subplot(211)
plot(x)
windows = ['flat']
title(plot_title)
subplot(212)
plot(smooth(xn, 10, 'flat'))
title("Smoothing")
show()
smooth_data(sun,"Sunny & Human")
smooth_data(rain, "Rain & Human")
smooth_data(sun_none, "Sunny & None")
smooth_data(rain_none, "Rain & None")
smooth_data(sun_ani, "Sunny & Animal")