root/experimental/AWG/trunk/AWG/sources.py @ 3588

Revision 3588, 3.5 KB (checked in by DrewCormier, 6 years ago)

added labels. GUI format changes

Line 
1#!/usr/bin/env/python
2
3import math
4import random
5
6class sources:
7    def __init__(self):
8        pass
9
10    def get_sources_list(self):
11        #TODO: setupt __init__ in AWG Frame so that 'file' can be first
12        return ['file','sine', 'cosine', 'random', 'zeros']
13
14    def gen_sine(self, freq, len, type):
15        '''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")'''
16
17        count = 0
18        sine = []
19        while count < len:
20            sine.append(math.sin(freq*2*math.pi*count/len))
21            count = count + 1
22
23        sine = self.convert_data_type(sine, type)
24
25        return sine
26
27
28    def gen_cosine(self, freq, len, type):
29        '''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")'''
30
31        count = 0
32        cosine = []
33        while count < len:
34            cosine.append(math.cos(freq*2*math.pi*count/len))
35            count = count + 1
36
37        cosine = self.convert_data_type(cosine, type)
38
39        return cosine
40
41    def read_file(self, file_name, delimiter, type):
42
43        try:
44            my_file = open(file_name, 'r')
45        except IOError:
46            print "no file named " + file_name
47            return []  #return an empty packet
48
49        data_from_file = my_file.read()   #read the file as a big string
50        my_file.close()   #done reading the file.  process the string from now on
51
52        data_from_file.strip('\n')  #\n's will not be tollerated
53        data_from_file.strip('[')   #['s will not be tollerated
54        data_from_file.strip(']')   #]'s will not be tollerated
55
56        data_from_file = data_from_file.split(delimiter)  #break string into a list
57        data_from_file = self.convert_data_type(data_from_file, type)
58
59        return data_from_file
60
61    def gen_random_data(self, len, rand_type, 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        data = self.convert_data_type(data, type)
80
81        return data
82               
83    def gen_zeros(self, len):
84        '''returns a list of zeros'''
85        data = []
86        for n in range(len):
87            data.append(0)
88        return data
89
90    def convert_data_type(self, data, type):
91        type = type.lower()  #makes comparisons case-insensitive
92       
93        if type == "short" or type == "int":   #short and int are the same in python
94            #TODO: test the robustness of the max() module
95            #WARNING: if the maximum value of the data is greater than 1, this will probably crash
96            #data_max = max(data)
97            data_max = 1
98            data = [int(round(float(x)*(32767/data_max))) for x in data]  #conver to integer format
99        elif type == "float":
100            data = [float(x) for x in data]
101        else:
102            print "types other than float, short, and int are not yet supported in sources.convert"
103       
104        return data
105
106
107
108
109
110
111
112
Note: See TracBrowser for help on using the browser.