source: fact/tools/pyscripts/pyfact/generator.py@ 13355

Last change on this file since 13355 was 13328, checked in by neise, 13 years ago
added automodule support
  • Property svn:executable set to *
File size: 4.5 KB
Line 
1#!/usr/bin/python -tt
2#
3# Dominik Neise, Werner Lustermann
4# TU Dortmund, ETH Zurich
5#
6import numpy as np
7
8class 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.__module__ = 'generator'
20 self.option_str = option_str.lower()
21 self.options = make_options_from_str(option_str)
22 self.parse_options()
23 self.name = name
24
25 def parse_options(self):
26 o = self.options #shortcut
27 if 'len' in o:
28 self.npoints = int(o['len'][0])
29 else:
30 self.npoints = 100
31 if 'noise' in o:
32 self.sigma = float(o['noise'][0])
33 else:
34 self.sigma = 1
35 if 'bsl' in o:
36 self.bsl = float(o['bsl'][0])
37 else:
38 self.bsl = -0.5
39
40 if 'step' in o:
41 self.step_height = float(o['step'][0])
42 self.step_start = int(o['step'][1])
43 self.step_stop = int(o['step'][2])
44
45 if 'triangle' in o:
46 self.pulses = []
47 # append 1st pulse to list of pulses
48 self.pulses.append( ( float(o['triangle'][0]) , float(o['triangle'][1]), int(o['triangle'][2]), int(o['triangle'][3]) ) )
49 number_of_pulses_after_1st = (len(o['triangle'])-4)/2
50 for i in range(number_of_pulses_after_1st):
51 self.pulses.append( ( float(o['triangle'][2*i+4]) , float(o['triangle'][2*i+5]), int(o['triangle'][2]), int(o['triangle'][3]) ) )
52
53 if 'spike' in o:
54 self.spikes = []
55 for i in range(len(o['spike'])/2):
56 self.spikes.append( ( int(o['spike'][2*i]), float(o['spike'][2*i+1]) ) )
57
58 def __call__(self, option_str = ''):
59 if option_str:
60 self.option_str = option_str.lower()
61 self.options = make_options_from_str(self.option_str)
62 self.parse_options()
63
64 signal = np.zeros(self.npoints)
65 signal += self.bsl
66 signal += np.random.randn(self.npoints) * self.sigma
67 if 'step' in self.options:
68 signal[self.step_start:self.step_stop] += self.step_height
69 if 'triangle' in self.options:
70 for pulse in self.pulses:
71 pos = pulse[0]
72 height = pulse[1]
73 rise = pulse[2]
74 fall = pulse[3]
75 start = pos - rise
76 stop = pos + fall
77 signal[start:pos] += np.linspace(0., height, rise)
78 signal[pos:stop] += np.linspace(height, 0. , fall)
79 if 'spike' in self.options:
80 for spike in self.spikes:
81 signal[spike[0]] += spike[1]
82 return signal
83
84 def __str__(self):
85 s = self.name + '\n'
86 s += 'possible options and parameters\n'
87 s += ' * len: number of samples (100)\n'
88 s += ' * noise: sigma (1)\n'
89 s += ' * bsl: level (-0.5)\n'
90 s += ' * step: height, start, end\n'
91 s += ' * triangle: pos height risingedge, fallingedge [pos height ...]\n'
92 s += ' * spike: pos height [pos height ...]\n'
93
94 s += 'current options are:\n'
95 for key in self.options.keys():
96 s += key + ':' + str(self.options[key]) + '\n'
97 return s
98
99
100# Helper function to parse signalname and create a dictionary
101# dictionary layout :
102# key : string
103# value : [list of parameters]
104def make_options_from_str(signalname):
105 options = {}
106 for word in (signalname.lower()).split():
107 if word.isalpha():
108 current_key = word
109 options[current_key] = []
110# if word.isdigit():
111 else:
112 options[current_key].append(word)
113# else:
114# print '-nothing'
115 return options
116
117def _plotter(signal, text):
118 x=range(len(signal))
119 ax = plt.plot(x, signal, 'b.', label='signal')
120 plt.title('test of SignalGenerator with option string:\n' + text)
121 plt.xlabel('sample')
122 plt.legend()
123 plt.grid(True)
124 plt.show()
125
126if __name__ == '__main__':
127 import matplotlib.pyplot as plt
128 """ test the class """
129 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')
130 sig = myGenerator()
131 print myGenerator
132 _plotter(sig, myGenerator.option_str)
Note: See TracBrowser for help on using the repository browser.