1 | #!/usr/bin/python -tt
|
---|
2 | #
|
---|
3 | # Dominik Neise, Werner Lustermann
|
---|
4 | # TU Dortmund, ETH Zurich
|
---|
5 | #
|
---|
6 | import numpy as np
|
---|
7 |
|
---|
8 | class SignalGenerator(object):
|
---|
9 | """ Signal Generator
|
---|
10 | generates signals for testing several helper classes like:
|
---|
11 | * fir filters
|
---|
12 | * signal extractors
|
---|
13 | """
|
---|
14 |
|
---|
15 | def __init__(self, option_str = 'len 100 noise 3', name = 'SignalGenerator'):
|
---|
16 | """ initialize the generator
|
---|
17 | sets default signal to generate
|
---|
18 | """
|
---|
19 | self.option_str = option_str.lower()
|
---|
20 | self.options = make_options_from_str(option_str)
|
---|
21 | self.parse_options()
|
---|
22 | self.name = name
|
---|
23 |
|
---|
24 | def parse_options(self):
|
---|
25 | o = self.options #shortcut
|
---|
26 | if 'len' in o:
|
---|
27 | self.npoints = int(o['len'][0])
|
---|
28 | else:
|
---|
29 | self.npoints = 100
|
---|
30 | if 'noise' in o:
|
---|
31 | self.sigma = float(o['noise'][0])
|
---|
32 | else:
|
---|
33 | self.sigma = 1
|
---|
34 | if 'bsl' in o:
|
---|
35 | self.bsl = float(o['bsl'][0])
|
---|
36 | else:
|
---|
37 | self.bsl = -0.5
|
---|
38 |
|
---|
39 | if 'step' in o:
|
---|
40 | self.step_height = float(o['step'][0])
|
---|
41 | self.step_start = int(o['step'][1])
|
---|
42 | self.step_stop = int(o['step'][2])
|
---|
43 |
|
---|
44 | if 'triangle' in o:
|
---|
45 | self.pulses = []
|
---|
46 | # append 1st pulse to list of pulses
|
---|
47 | self.pulses.append( ( float(o['triangle'][0]) , float(o['triangle'][1]), int(o['triangle'][2]), int(o['triangle'][3]) ) )
|
---|
48 | number_of_pulses_after_1st = (len(o['triangle'])-4)/2
|
---|
49 | for i in range(number_of_pulses_after_1st):
|
---|
50 | self.pulses.append( ( float(o['triangle'][2*i+4]) , float(o['triangle'][2*i+5]), int(o['triangle'][2]), int(o['triangle'][3]) ) )
|
---|
51 |
|
---|
52 | if 'spike' in o:
|
---|
53 | self.spikes = []
|
---|
54 | for i in range(len(o['spike'])/2):
|
---|
55 | self.spikes.append( ( int(o['spike'][2*i]), float(o['spike'][2*i+1]) ) )
|
---|
56 |
|
---|
57 | def __call__(self, option_str = ''):
|
---|
58 | if option_str:
|
---|
59 | self.option_str = option_str.lower()
|
---|
60 | self.options = make_options_from_str(self.option_str)
|
---|
61 | self.parse_options()
|
---|
62 |
|
---|
63 | signal = np.zeros(self.npoints)
|
---|
64 | signal += self.bsl
|
---|
65 | signal += np.random.randn(self.npoints) * self.sigma
|
---|
66 | if 'step' in self.options:
|
---|
67 | signal[self.step_start:self.step_stop] += self.step_height
|
---|
68 | if 'triangle' in self.options:
|
---|
69 | for pulse in self.pulses:
|
---|
70 | pos = pulse[0]
|
---|
71 | height = pulse[1]
|
---|
72 | rise = pulse[2]
|
---|
73 | fall = pulse[3]
|
---|
74 | start = pos - rise
|
---|
75 | stop = pos + fall
|
---|
76 | signal[start:pos] += np.linspace(0., height, rise)
|
---|
77 | signal[pos:stop] += np.linspace(height, 0. , fall)
|
---|
78 | if 'spike' in self.options:
|
---|
79 | for spike in self.spikes:
|
---|
80 | signal[spike[0]] += spike[1]
|
---|
81 | return signal
|
---|
82 |
|
---|
83 | def __str__(self):
|
---|
84 | s = self.name + '\n'
|
---|
85 | s += 'possible options and parameters\n'
|
---|
86 | s += ' * len: number of samples (100)\n'
|
---|
87 | s += ' * noise: sigma (1)\n'
|
---|
88 | s += ' * bsl: level (-0.5)\n'
|
---|
89 | s += ' * step: height, start, end\n'
|
---|
90 | s += ' * triangle: pos height risingedge, fallingedge [pos height ...]\n'
|
---|
91 | s += ' * spike: pos height [pos height ...]\n'
|
---|
92 |
|
---|
93 | s += 'current options are:\n'
|
---|
94 | for key in self.options.keys():
|
---|
95 | s += key + ':' + str(self.options[key]) + '\n'
|
---|
96 | return s
|
---|
97 |
|
---|
98 |
|
---|
99 | # Helper function to parse signalname and create a dictionary
|
---|
100 | # dictionary layout :
|
---|
101 | # key : string
|
---|
102 | # value : [list of parameters]
|
---|
103 | def make_options_from_str(signalname):
|
---|
104 | options = {}
|
---|
105 | for word in (signalname.lower()).split():
|
---|
106 | if word.isalpha():
|
---|
107 | current_key = word
|
---|
108 | options[current_key] = []
|
---|
109 | # if word.isdigit():
|
---|
110 | else:
|
---|
111 | options[current_key].append(word)
|
---|
112 | # else:
|
---|
113 | # print '-nothing'
|
---|
114 | return options
|
---|
115 |
|
---|
116 | def _plotter(signal, text):
|
---|
117 | x=range(len(signal))
|
---|
118 | ax = plt.plot(x, signal, 'b.', label='signal')
|
---|
119 | plt.title('test of SignalGenerator with option string:\n' + text)
|
---|
120 | plt.xlabel('sample')
|
---|
121 | plt.legend()
|
---|
122 | plt.grid(True)
|
---|
123 | plt.show()
|
---|
124 |
|
---|
125 | if __name__ == '__main__':
|
---|
126 | import matplotlib.pyplot as plt
|
---|
127 | """ test the class """
|
---|
128 | myGenerator = SignalGenerator('len 400 noise 0.3 bsl -2.5 triangle 50 10.2 10 100 65 10 150 20 180 10 250 10 spike 100 50. 20 50 21 49')
|
---|
129 | sig = myGenerator()
|
---|
130 | print myGenerator
|
---|
131 | _plotter(sig, myGenerator.option_str)
|
---|