SineWaveform.py 2.02 KB
Newer Older
1 2 3 4 5 6 7 8 9
__author__ = "Federico Asara"
__copyright__ = "Copyright 2007, The Cogent Project"
__credits__ = ["Federico Asara", "Juan David Gonzalez Cobas"]
__license__ = "GPL2"
__version__ = "1.0.0"
__maintainer__ = "Federico Asara"
__email__ = "federico.asara@gmail.com"
__status__ = "Production"

Federico Asara's avatar
Federico Asara committed
10 11 12
import Waveform
from Utilities import *
from numpy import *
13 14 15
import Pyro4
import Pyro4.util
import sys
Federico Asara's avatar
Federico Asara committed
16 17

class SineWaveform(Waveform.Waveform):
18 19 20 21 22 23 24 25 26
    def get(self, what):
        return self.__getattribute__(what)
    
    def set(self, what, how):
        self.__setattr__(what, how)
    
    _parameters = {'frequency':['Frequency', 'Frequency of the sinewave, in HZ', 1000, float],
                  'amplitude':['Amplitude', 'Amplitude of the sinewave, in Vpp', 1, float],
                  'dc':['DC Compoment', 'DC component of the sinewave, in Vpp', 0, float]}
Federico Asara's avatar
Federico Asara committed
27 28
                  
    def __init__(self, *args, **kwargs):
29
        Waveform.Waveform.__init__(self, *args, **kwargs)
Federico Asara's avatar
Federico Asara committed
30 31 32 33 34 35 36 37 38 39 40 41
    
    def generate(self, sampleRate, samples, nbits, fsr):
        f = self.frequency
        A = self.amplitude
        C = self.dc
        
        t = arange(samples, dtype=float)/sampleRate
        s = A*sin(2*pi*f*t) +C
        
        lsb = fsr/(2**nbits)
        
        return (s/lsb).astype(int)
42
    
Federico Asara's avatar
Federico Asara committed
43 44 45 46 47 48 49 50 51 52 53 54 55
    
    def generatePeriod(self, nbits, samples, fsr):
        f = self.frequency
        A = self.amplitude
        C = self.dc
        
        t = arange(samples, dtype=float)/samples
        s = A*sin(2*pi*t) +C
        
        lsb = fsr/(2**nbits)
        
        return (s/lsb).astype(int), f
        
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    def scale(self, factor):
        """Multiply the frequency by factor."""
        self.frequency *= factor
        
        return self

name = 'Sine Waveform'
target = SineWaveform

import commands

def launch():
    g = target()
    hn = commands.getoutput('hostname')
    
    daemon=Pyro4.Daemon(host = hn)
    
    myUri = daemon.register(g)
    
    ns=Pyro4.locateNS()
    ns.register("Sine", myUri)
    daemon.requestLoop()

if __name__ == '__main__':
    launch()