Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/bin/env python
  2. # Written by Jonathan Foote February 2011
  3. # http://creativecommons.org/licenses/by-sa/3.0/
  4.  
  5. def bit(n, val):
  6.    ''' return True if bit n of val is set) '''
  7.    val &= (1 << n)
  8.    return(val > 0)
  9.  
  10. # generate a high modulation rate pwm stream
  11. def fastmodulationPWM(val):
  12.    ''' Generate a binary fast modulation PWM array for the given duty cycle '''
  13.  
  14.    N = 4                        # four bits of PWM
  15.    n = 16                       # length of PWM signal (2^N)
  16.  
  17.    # make a list of n zeros
  18.    r = list(0 for i in range(n))
  19.  
  20.    for i in range(n):
  21.       flag = 0
  22.       if bit (0,i) == 1:
  23.          flag =  bit(N-1,val)
  24.       elif bit(1,i):
  25.          flag |= bit(N-2,val)
  26.       elif bit(2,i):
  27.          flag |= bit(N-3,val)
  28.       elif bit(3,i):
  29.          flag |= bit(N-4,val)
  30.       if(flag):
  31.          r[i] = 1
  32.  
  33.    return(r)
  34.      
  35.  
  36. # test the fast modulation routine for all values of duty cycle
  37. n = 16
  38. for i in range(n): # for a range of duty cycles
  39.    r = fastmodulationPWM(i) # compute the fast modulation array
  40.    print "%2d: %s" % (i,str(r)) # and print it