| 1 | #!/usr/bin/env/python |
|---|
| 2 | |
|---|
| 3 | import math |
|---|
| 4 | import random |
|---|
| 5 | |
|---|
| 6 | class sources: |
|---|
| 7 | def __init__(self,parent): |
|---|
| 8 | self.parent = parent |
|---|
| 9 | self.available_sources = ['file', 'sine', 'cosine', 'random', 'zeros'] |
|---|
| 10 | #IMPORTANT: corresponding_methods must be in the same order as the available_sources |
|---|
| 11 | self.corresponding_methods = ['read_file','gen_sine', 'gen_cosine', 'gen_random_data', 'gen_zeros'] |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | def get_sources_list(self): |
|---|
| 15 | return self.available_sources |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | def gen_signal(self): |
|---|
| 19 | eval_string = "self." + self.corresponding_methods[parent.signal_index] + "()" |
|---|
| 20 | return eval(eval_string) #return the signal generated by the corresponding method |
|---|
| 21 | |
|---|
| 22 | def read_file(self): |
|---|
| 23 | |
|---|
| 24 | try: |
|---|
| 25 | my_file = open(self.parent.file_name, 'r') |
|---|
| 26 | except IOError: |
|---|
| 27 | print "no file named " + self.parent.file_name |
|---|
| 28 | return [] #return an empty packet |
|---|
| 29 | |
|---|
| 30 | data_from_file = my_file.read() #read the file as a big string |
|---|
| 31 | my_file.close() #done reading the file. process the string from now on |
|---|
| 32 | |
|---|
| 33 | data_from_file.strip('\n') #\n's will not be tollerated |
|---|
| 34 | data_from_file.strip('[') #['s will not be tollerated |
|---|
| 35 | data_from_file.strip(']') #]'s will not be tollerated |
|---|
| 36 | |
|---|
| 37 | data_from_file = data_from_file.split(self.parent.delimiter) #break string into a list |
|---|
| 38 | |
|---|
| 39 | return data_from_file |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | def gen_sine(self): |
|---|
| 43 | '''freq is frequency in cycles per packet. len is the length of the packet. type is the data type to return (e.g., "float", "short", "int")''' |
|---|
| 44 | |
|---|
| 45 | count = 0 |
|---|
| 46 | sine = [] |
|---|
| 47 | while count < self.parent.len: |
|---|
| 48 | sine.append(math.sin(self.parent.freq*2*math.pi*count/self.parent.len)) |
|---|
| 49 | count = count + 1 |
|---|
| 50 | |
|---|
| 51 | return sine |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | def gen_cosine(self): |
|---|
| 55 | '''freq is frequency in cycles per packet. len is the length of the packet. type is the data type to return (e.g., "float", "short", "int")''' |
|---|
| 56 | |
|---|
| 57 | count = 0 |
|---|
| 58 | cosine = [] |
|---|
| 59 | while count < self.parent.len: |
|---|
| 60 | cosine.append(math.cos(self.parent.freq*2*math.pi*count/self.parent.len)) |
|---|
| 61 | count = count + 1 |
|---|
| 62 | |
|---|
| 63 | return cosine |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | def gen_random_data(self): |
|---|
| 68 | '''the rand_type variable defines which function call to use in the random module''' |
|---|
| 69 | |
|---|
| 70 | data = [] |
|---|
| 71 | tmp_rand_type = self.parent.rand_type.lower() #makes comparison case insensitive |
|---|
| 72 | if tmp_rand_type == 'random': |
|---|
| 73 | count = 0 |
|---|
| 74 | while count < self.parent.len: |
|---|
| 75 | data.append(random.random()) |
|---|
| 76 | count = count + 1 |
|---|
| 77 | elif tmp_rand_type == 'uniform': |
|---|
| 78 | count = 0 |
|---|
| 79 | while count < self.parent.len: |
|---|
| 80 | data.append(random.uniform()) |
|---|
| 81 | count = count + 1 |
|---|
| 82 | else: |
|---|
| 83 | print "random methods other than random and uniform are not supported in sources.random_data" |
|---|
| 84 | |
|---|
| 85 | return data |
|---|
| 86 | |
|---|
| 87 | def gen_zeros(self): |
|---|
| 88 | '''returns a list of zeros''' |
|---|
| 89 | data = [] |
|---|
| 90 | for n in range(self.parent.len): |
|---|
| 91 | data.append(0) |
|---|
| 92 | return data |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|