Plot square wave

Yigit Turgut y.turgut at gmail.com
Wed Jan 18 09:41:48 EST 2012


Hi all,

I am trying to generate a pseudo pwm signal, low-high transition will
take place when screen goes from black to white and high-low
transition when white to black. As a result I am trying to plot the
signal. Here is my code;

import time, pylab, numpy, scipy, pygame

def _func1():
     global end
     global white
     global k
     global t
     global i
     k = numpy.arange(4)
     t = numpy.arange(4)
     i = 0
     f = open("test.txt", "w")
     white = True
     start = time.time()
     end = time.time() - start
     screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
     timer = pygame.time.Clock()
     test = repr(time.time())
     while(end<8.00):
      end = time.time() - start
      if white:
	screen.fill((255, 255, 255))
	time.sleep(1)
	k[i] = 0
	t[i] = end
	f.write(str(t[i]) + "\t" + str(k[i]) + "\n")
	i = i + 1
	print repr(end)

      else:
	screen.fill((0, 0, 0))
	time.sleep(1)
	k[i] = 1
	t[i] = end
	f.write(str(t[i]) + "\t" + str(k[i]) + "\n")
	i =  i+ 1
	print repr(end)

      white = not white
      pygame.display.update()
     pygame.quit()

if __name__ == "__main__":
  _func1()
  time,data = numpy.loadtxt('test.txt', unpack=True)
  print k
  print t
  print i
  pylab.plot(time,data)
  pylab.show()


Problem is I get a sawtooth instead of a square wave. I know that I
need to define points between 0,1,2 time integer values to achieve
this. But I hope there is a python trick that will yield this
time,data plot to a square wave?




More information about the Python-list mailing list