System Controlling: Signal generation

Sunday, April 22, 2012

Signal generation

Introduction

If you want to test your system, you always need an input signal. In the tools.signalgenerator you can find some basic signals like sine wave, cosine wave or square wave etc.

Sine wave

The sine wave or sinusoid is a mathematical function that describes a smooth repetitive oscillation. $$y(t) = A \cdot \sin(\omega t + \phi) + D$$
Where: 
  • A, the amplitude, is the peak deviation of the function from its center position.
  • \(\omega\), the angular frequency
  • \(\phi\),the phase, specifies where in its cycle the oscillation begins at t = 0.
  • D, a non-zero center amplitude
The sinwave class contains all these parameters. Also there are two extra parameters: the sampling time(\(T_{s}\)) and the end time(mtime)
class sinwave:
    """
    Generating a sinus vawe.
    x(t)= amp * sin(2 * pi * F * t + phase) + bias
    """
    def __init__(self, amp = 220, freq = 50, Ts = 0.01, mtime = 0.01, bias = 0, phase = 0):
        self.amp = amp
        self.freq = freq
        self.Ts = Ts
        self.mtime = mtime
        self.bias = bias
        self.phase = phase
        self.stime = arange(0, self.mtime, self.Ts)

    def signal(self, stime = None, phase = None, freq = None, bias = None, amp = None):
        if stime == None:
             stime = self.stime
        if phase == None:
             phase = self.phase;
        if freq == None:
             freq = self.freq;
        if bias == None:
             bias = self.bias;
        if amp == None:
             amp = self.amp;
        x = 2 * pi * freq * stime
        return amp * sin(x + phase) + bias;
If you want to use this class, first you have to create a new class, also there are three options to define the parameters, while you are creating the class,
sinW = sinwave( Ts = Ts, mtime = 1, freq = 5, amp = 6000 )

when the signal function is called,
pw = sinW.signal( freq = 5, amp = 3000 )

or directly you can reach the parameters
sinW.app = 400
sinW = sinwave( Ts = Ts, mtime = 1, freq = 5, amp = 6000 )
sinW.amp = 400
pw = sinW.signal()
The result of the following code above is:


Square wave

A square wave is a kind of non-sinusoidal waveform, an ideal square wave alternates regularly and instantaneously between two levels. Using Fourier expansion with cycle frequency f over time t, we can write an ideal square wave as an infinite series of the form: $$ x_{square}(t) = \frac{4}{\pi} \sum_{k=1}^\infty {\sin{\left (2\pi (2k-1) ft \right )}\over(2k-1)}$$$$= \frac{4}{\pi}\left (\sin(2\pi ft) + {1\over3}\sin(6\pi ft) + {1\over5}\sin(10\pi ft) + \cdots\right )$$ Here is the square wave implementation in python.
class squarewave: 
    """
    Generating a square signal using the signum of the sinus function. 
    x(t) = amp * sign( sin( 2 * pi * F * t + phase ) + bias )
    where 2*pi*f is the angular frequency
    """
    def __init__(self, amp = 220, freq = 50, Ts = 0.01, mtime = 0.5, bias = 0, phase = 0):  
        self.amp   =  amp
        self.freq  = freq 
        self.Ts   = Ts
        self.mtime  = mtime
        self.bias  = bias
        self.phase  = phase
        self.stime  = arange(0, self.mtime , self.Ts)  
  
    def signal(self, stime = None, phase = None, freq = None, bias = None, amp = None):  
        from numpy import sign  
        if stime == None:
            stime  =  self.stime
        if phase == None:
            phase  =  self.phase;
        if freq == None:
            freq  =  self.freq;
        if bias == None:
            bias  =  self.bias;
        if amp == None:
           amp = self.amp;   
        x  = 2 * pi * freq * stime 
        return amp * sign( sin( x + phase ) ) + bias;
So, here is a full example how does it possible to create a square wave:
if __name__ == "__main__":
    Ts = 0.001
    sinW = sinwave( Ts = Ts, mtime = 1, freq = 5, amp = 60 )   
    pw = sinW.signal()    
    
    squareW =   squarewave( Ts = Ts, mtime = 1, freq = 5, amp = 60 ) 
    sw  = squareW.signal()   
    
    pylab.figure( 1 )
    pylab.plot( pw )
    pylab.plot( sw )
    
    pylab.show()


Sawtooth wave
class sawtoothwave:
    """
    Generating a triangle vawe.
    x(t)= amp * 2 / pi *sum(( -1 )^i * (sin( 2 * i * pi * F * t + phase) + bias) / i )
    where 2*pi*f is the angular frequency
    """
    def __init__(self, amp = 220, freq = 50, Ts = 0.01, mtime = 5, bias = 0, phase = 0, harmonic = 50):
        self.amp   =  amp
        self.freq  = freq 
        self.Ts   = Ts
        self.mtime  = mtime
        self.bias  = bias
        self.phase  = phase
        self.harmonic = harmonic
        self.stime   =  arange(0, self.mtime, self.Ts)   
  
    def signal(self, stime = None, phase = None, freq = None, bias = None, amp = None, harmonic = None):    
        if stime == None:
            stime  =  self.stime
        if phase == None:
            phase  =  self.phase;
        if freq == None:
            freq  =  self.freq;
        if bias == None:
            bias  =  self.bias;
        if amp == None:
            amp  =  self.amp; 
        if harmonic == None:
            harmonic=  self.harmonic;
     
        x  = 2 * pi * freq * stime 
        res = zeros(len(x))
        for i in range( 1, harmonic + 1):   
            res +=  pow( -1, i+1 ) * sin( i * (x + phase) ) / i;   
   
        return amp * (2. / pi) * res + bias
if __name__ == "__main__":
    Ts = 0.001
    sinW = sinwave( Ts = Ts, mtime = 1, freq = 5, amp = 60 )   
    pw = sinW.signal()    
    
    squareW =   squarewave( Ts = Ts, mtime = 1, freq = 5, amp = 60 ) 
    sw  = squareW.signal()
    
    sawtoothW =   sawtoothwave( Ts = Ts, mtime = 1, freq = 5, amp = 60 ) 
    stw  = sawtoothW.signal()
    
    
    pylab.figure( 1 )
    pylab.plot( pw )
    pylab.plot( sw )
    pylab.plot( stw )
    
    
    pylab.show()

Triangle wave
class trianglewave:
    """
    Generating a triangle vawe.
    x(t)= amp * 8 / pi^2 *sum(( -1 )^i * (sin((2 * i + 1) * 2 * pi * F * t + phase) + bias) / ( 2 * i + 1 )^2 )
    """
    def __init__(self, amp = 220, freq = 50, Ts = 0.01, mtime = 5, bias = 0, phase = 0, harmonic = 50):
        self.amp   =  amp
        self.freq  = freq 
        self.Ts   = Ts
        self.mtime  = mtime
        self.bias  = bias
        self.phase  = phase
        self.harmonic = harmonic
        self.stime   =  arange(0, self.mtime, self.Ts)   
  
    def signal(self, stime = None, phase = None, freq = None, bias = None, amp = None, harmonic = None):    
        if stime == None:
             stime  =  self.stime
        if phase == None:
             phase  =  self.phase;
        if freq == None:
             freq  =  self.freq;
        if bias == None: 
             bias  =  self.bias;
        if amp == None:
             amp  =  self.amp; 
        if harmonic == None:
             harmonic=  self.harmonic;
     
        x  = 2 * pi * freq * stime 
        res = zeros(len(x))
        for i in range( 0, harmonic ):   
            res +=  pow( -1, i ) * sin(( 2 * i + 1 ) * (x + phase) ) / pow( ( 2. * i + 1.), 2 );   
   
       return amp * (8. / pow(pi,2)) * res + bias
if __name__ == "__main__":
    Ts = 0.001
    sinW = sinwave( Ts = Ts, mtime = 1, freq = 5, amp = 60 )   
    pw = sinW.signal()    
    
    squareW =   squarewave( Ts = Ts, mtime = 1, freq = 5, amp = 60 ) 
    sw  = squareW.signal()
    
    sawtoothW =   sawtoothwave( Ts = Ts, mtime = 1, freq = 5, amp = 60 ) 
    stw  = sawtoothW.signal()
    
    triangleW =   trianglewave( Ts = Ts, mtime = 1, freq = 5, amp = 60 ) 
    tw  = triangleW.signal()   
    
    pylab.figure( 1 )
    pylab.plot( pw )
    pylab.plot( sw )
    pylab.plot( stw )
    pylab.plot( tw )
    
    pylab.show()

No comments:

Post a Comment