# root/experimental/AWG/trunk/AWG/sources.py@3552

Revision 3552, 2.6 KB (checked in by DrewCormier, 6 years ago)

initial import

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