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