| 1 | #!/usr/bin/env/python |
|---|
| 2 | |
|---|
| 3 | import math |
|---|
| 4 | import random |
|---|
| 5 | |
|---|
| 6 | class sources: |
|---|
| 7 | def __init__(self): |
|---|
| 8 | pass |
|---|
| 9 | |
|---|
| 10 | def get_sources_list(self): |
|---|
| 11 | return ['sine', 'cosine', 'random', 'file'] |
|---|
| 12 | |
|---|
| 13 | def gen_sine(self, freq, len, type): |
|---|
| 14 | '''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")''' |
|---|
| 15 | |
|---|
| 16 | count = 0 |
|---|
| 17 | sine = [] |
|---|
| 18 | while count < len: |
|---|
| 19 | sine.append(math.sin(freq*2*math.pi*count/len)) |
|---|
| 20 | count = count + 1 |
|---|
| 21 | |
|---|
| 22 | sine = self.convert_data_type(sine, type) |
|---|
| 23 | |
|---|
| 24 | return sine |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | def gen_cosine(self, freq, len, type): |
|---|
| 28 | '''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")''' |
|---|
| 29 | |
|---|
| 30 | count = 0 |
|---|
| 31 | cosine = [] |
|---|
| 32 | while count < len: |
|---|
| 33 | cosine.append(math.cos(freq*2*math.pi*count/len)) |
|---|
| 34 | count = count + 1 |
|---|
| 35 | |
|---|
| 36 | cosine = self.convert_data_type(cosine, type) |
|---|
| 37 | |
|---|
| 38 | return cosine |
|---|
| 39 | |
|---|
| 40 | def read_file(self, file_name, delimiter, type): |
|---|
| 41 | #TODO: test this method |
|---|
| 42 | |
|---|
| 43 | data_from_file = open(file_name, 'r') |
|---|
| 44 | data_from_file = data_from_file.parse(delimiter) |
|---|
| 45 | |
|---|
| 46 | data_from_file = convert_data_type(data_from_file, type) |
|---|
| 47 | |
|---|
| 48 | return data_from_file |
|---|
| 49 | |
|---|
| 50 | def gen_random_data(self, len, rand_type, type): |
|---|
| 51 | '''the rand_type variable defines which function call to use in the random module''' |
|---|
| 52 | |
|---|
| 53 | data = [] |
|---|
| 54 | rand_type = rand_type.lower() #makes comparison case insensitive |
|---|
| 55 | if rand_type == 'random': |
|---|
| 56 | count = 0 |
|---|
| 57 | while count < len: |
|---|
| 58 | data.append(random.random()) |
|---|
| 59 | count = count + 1 |
|---|
| 60 | elif rand_type == 'uniform': |
|---|
| 61 | count = 0 |
|---|
| 62 | while count < len: |
|---|
| 63 | data.append(random.uniform()) |
|---|
| 64 | count = count + 1 |
|---|
| 65 | else: |
|---|
| 66 | print "random methods other than random and uniform are not supported in sources.random_data" |
|---|
| 67 | |
|---|
| 68 | data = self.convert_data_type(data, type) |
|---|
| 69 | |
|---|
| 70 | return data |
|---|
| 71 | |
|---|
| 72 | def convert_data_type(self, data, type): |
|---|
| 73 | type = type.lower() #makes comparisons case-insensitive |
|---|
| 74 | |
|---|
| 75 | if type == "short" or type == "int": #short and int are the same in python |
|---|
| 76 | #TODO: test the robustness of the max() module |
|---|
| 77 | #data_max = max(data) |
|---|
| 78 | data_max = 1 |
|---|
| 79 | data = [int(round(x*(32767/data_max))) for x in data] #conver to integer format |
|---|
| 80 | elif type == "float": |
|---|
| 81 | data = [float(x) for x in data] |
|---|
| 82 | else: |
|---|
| 83 | print "types other than float, short, and int are not yet supported in sources.convert" |
|---|
| 84 | |
|---|
| 85 | return data |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|