Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19083 > unrolled thread

Plot square wave

Started byYigit Turgut <y.turgut@gmail.com>
First post2012-01-18 06:41 -0800
Last post2012-01-18 17:09 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Plot square wave Yigit Turgut <y.turgut@gmail.com> - 2012-01-18 06:41 -0800
    Re: Plot square wave Peter Otten <__peter__@web.de> - 2012-01-18 17:09 +0100

#19083 — Plot square wave

FromYigit Turgut <y.turgut@gmail.com>
Date2012-01-18 06:41 -0800
SubjectPlot square wave
Message-ID<03386c82-34e2-4ecd-a0fe-d109d1956fc0@m2g2000vbc.googlegroups.com>
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?

[toc] | [next] | [standalone]


#19090

FromPeter Otten <__peter__@web.de>
Date2012-01-18 17:09 +0100
Message-ID<mailman.4836.1326902986.27778.python-list@python.org>
In reply to#19083
Yigit Turgut wrote:

> 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?

There is no "Python trick", pylab is showing the data you provide. To get a 
square wave you need two y values per x value, for example

import pylab

time = [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
data = [0, 1, 1, 0, 0, 1, 1, 0, 0, 1]
pylab.plot(time, data)
pylab.ylim(-.1, 1.1) # so you see the horizontal lines of the graph
pylab.xlim(-.1, 4.1)
pylab.show()

A general advice: try the individual aspects of your script (plotting, 
pygame flicker, data i/o) separately to make sure you understand them well 
enough before putting them together in the final version of your code.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web