| 1 | #!/usr/bin/env/python |
|---|
| 2 | |
|---|
| 3 | ''' |
|---|
| 4 | This module contains signal sources that can be called by AWG.py. |
|---|
| 5 | |
|---|
| 6 | To add a custom source, you must make changes in two places: |
|---|
| 7 | |
|---|
| 8 | 1. Add the name that you want to appear in the AWG graphical user |
|---|
| 9 | interface to the self.available_sources dictionary as dictionary keys. |
|---|
| 10 | The corresponding values in the dictionay indicate which function is |
|---|
| 11 | called when each source is selected. |
|---|
| 12 | |
|---|
| 13 | 2. Add your function that generates your custom signal anywhere below the |
|---|
| 14 | comment line "#source functions are defined below:". Your function |
|---|
| 15 | should fit the same general format as the existing functions: |
|---|
| 16 | |
|---|
| 17 | def my_function(self): |
|---|
| 18 | #define your signal here |
|---|
| 19 | return your_signal |
|---|
| 20 | ''' |
|---|
| 21 | |
|---|
| 22 | import math |
|---|
| 23 | import random |
|---|
| 24 | |
|---|
| 25 | class sources: |
|---|
| 26 | def __init__(self,parent): |
|---|
| 27 | self.parent = parent |
|---|
| 28 | self.available_sources = {'file': 'read_file()', |
|---|
| 29 | 'sine': 'gen_sine()', |
|---|
| 30 | 'cosine': 'gen_cosine()', |
|---|
| 31 | 'random': 'gen_random_data()', |
|---|
| 32 | 'zeros': 'gen_zeros()', |
|---|
| 33 | 'ones': 'gen_ones()'} |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | def get_sources_list(self): |
|---|
| 37 | return self.available_sources |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | def gen_signal(self): |
|---|
| 41 | eval_string = "self." + self.available_sources[self.parent.signal_type] |
|---|
| 42 | return eval(eval_string) # return the signal generated by |
|---|
| 43 | # the corresponding method |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | #------------------------------------------------------------------------------ |
|---|
| 47 | # source functions are defined below: |
|---|
| 48 | |
|---|
| 49 | def read_file(self): |
|---|
| 50 | '''Reads data from a file and returns the items in the file as a list. |
|---|
| 51 | Parent class must have a variable "file_name" and a variable |
|---|
| 52 | "delimiter".''' |
|---|
| 53 | try: |
|---|
| 54 | my_file = open(self.parent.file_name, 'r') |
|---|
| 55 | except IOError: |
|---|
| 56 | print "no file named " + self.parent.file_name |
|---|
| 57 | return [] # return an empty packet |
|---|
| 58 | |
|---|
| 59 | data_from_file = my_file.read() # read the file as a big string |
|---|
| 60 | my_file.close() # done reading the file. |
|---|
| 61 | # process the string from now on |
|---|
| 62 | |
|---|
| 63 | data_from_file.strip('\n') # \n's will not be tollerated |
|---|
| 64 | data_from_file.strip('[') # ['s will not be tollerated |
|---|
| 65 | data_from_file.strip(']') # ]'s will not be tollerated |
|---|
| 66 | |
|---|
| 67 | # break the string into a list |
|---|
| 68 | data_from_file = data_from_file.split(self.parent.delimiter) |
|---|
| 69 | |
|---|
| 70 | return data_from_file |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | def gen_sine(self): |
|---|
| 74 | '''Generates a sinusoid and returns it as a list. Parent must have |
|---|
| 75 | a variable "signal_len" that corresponds to the number of elements |
|---|
| 76 | in the list. Parent must have a variable "frequency" that |
|---|
| 77 | corresponds to frequency in cycles per packet.''' |
|---|
| 78 | count = 0 |
|---|
| 79 | sine = [] |
|---|
| 80 | while count < self.parent.signal_len: |
|---|
| 81 | sine.append(math.sin(self.parent.frequency*2*math.pi*count/self.parent.signal_len)) |
|---|
| 82 | count = count + 1 |
|---|
| 83 | |
|---|
| 84 | return sine |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | def gen_cosine(self): |
|---|
| 88 | '''Generates a cosine and returns it as a list. Parent must have |
|---|
| 89 | a variable "signal_len" that corresponds to the number of elements |
|---|
| 90 | in the list. Parent must have a variable "frequency" |
|---|
| 91 | that corresponds to frequency in cycles per packet.''' |
|---|
| 92 | |
|---|
| 93 | count = 0 |
|---|
| 94 | cosine = [] |
|---|
| 95 | while count < self.parent.signal_len: |
|---|
| 96 | cosine.append(math.cos(self.parent.frequency*2*math.pi*count/self.parent.signal_len)) |
|---|
| 97 | count = count + 1 |
|---|
| 98 | |
|---|
| 99 | return cosine |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | def gen_random_data(self): |
|---|
| 103 | '''the rand_type variable defines which function call to use |
|---|
| 104 | in the random module''' |
|---|
| 105 | data = [] |
|---|
| 106 | |
|---|
| 107 | # lower() makes the comparison case insensitive |
|---|
| 108 | tmp_rand_type = self.parent.rand_type.lower() |
|---|
| 109 | if tmp_rand_type == 'random': |
|---|
| 110 | count = 0 |
|---|
| 111 | while count < self.parent.signal_len: |
|---|
| 112 | data.append(random.random()) |
|---|
| 113 | count = count + 1 |
|---|
| 114 | elif tmp_rand_type == 'uniform': |
|---|
| 115 | count = 0 |
|---|
| 116 | while count < self.parent.signal_len: |
|---|
| 117 | data.append(random.uniform()) |
|---|
| 118 | count = count + 1 |
|---|
| 119 | else: |
|---|
| 120 | print "random methods other than random and uniform are not supported in sources.random_data" |
|---|
| 121 | |
|---|
| 122 | return data |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | def gen_zeros(self): |
|---|
| 126 | '''returns a list of zeros''' |
|---|
| 127 | data = [] |
|---|
| 128 | for n in range(self.parent.signal_len): |
|---|
| 129 | data.append(0) |
|---|
| 130 | return data |
|---|
| 131 | |
|---|
| 132 | def gen_ones(self): |
|---|
| 133 | '''returns a list of ones''' |
|---|
| 134 | data = [] |
|---|
| 135 | for n in range(self.parent.signal_len): |
|---|
| 136 | data.append(1) |
|---|
| 137 | return data |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|