Repository navigation
Expand file tree
/
Copy pathdsp.py
More file actions
141 lines (108 loc) · 3.02 KB
/
Copy pathdsp.py
File metadata and controls
141 lines (108 loc) · 3.02 KB
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
#!/usr/bin/env python
"""
Digital signal processing for python-csp.
Copyright (C) Sarah Mount, 2009.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have rceeived a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from csp.cspprocess import *
import math
# TODO: Use numpy for more sophisticated processes.
ACCEL_DUE_TO_GRAVITY = 9.80665
@forever
def Zip(outchan, inchannels, _process=None):
"""Take data from a number of input channels, and write that
data as a single list to L{outchan}.
"""
while True:
outchan.write([chan.read() for chan in inchannels])
yield
return
@forever
def Unzip(inchan, outchans, _process=None):
"""Continuously read tuples of data from a single input channel and send
each datum out down its own output channel.
"""
while True:
data = inchan.read()
for i in range(data):
outchans[i].write(data[i])
yield
return
@forever
def Sin(inchan, outchan, _process=None):
while True:
outchan.write(math.sin(inchan.read()))
yield
return
@forever
def Cos(inchan, outchan, _process=None):
while True:
outchan.write(math.cos(inchan.read()))
yield
return
@forever
def Tan(inchan, outchan, _process=None):
while True:
outchan.write(math.tan(inchan.read()))
yield
return
@forever
def GenerateFloats(outchan, _process=None):
x = 0.0
while True:
outchan.write(x)
x += 0.1
yield
return
@forever
def Magnitude(inchan, outchan, _process=None):
while True:
acceldata = inchan.read()
mag = 0.0
for axis in acceldata: mag += axis ** 2
outchan.write(math.sqrt(mag))
yield
return
@forever
def Difference(inchan, outchan, window=1, _process=None):
cache = 0.0
while True:
acceldata = inchan.read()
try:
outchan.write(acceldata - cache)
cache = acceldata
except IndexError:
pass
yield
return
@forever
def Square(inchan, outchan, _process=None):
while True:
data = inchan.read()
outchan.write(data ** 2)
yield
return
@forever
def Normalise(inchan, outchan, _process=None, start=0.0, end=100.0):
scale = end - start
while True:
outchan.write(inchan.read() / scale)
yield
return
@forever
def Threshold(thresh, inchan, outchan, _process=None):
while True:
mag = inchan.read()
if mag >= thresh:
outchan.write(mag)
yield
return