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

Revision 4194, 2.7 KB (checked in by DrewCormier, 7 years ago)

starting to restructure code so that each source-type method has the same API

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