flux.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # greaseweazle/flux.py
  2. #
  3. # Written & released by Keir Fraser <keir.xen@gmail.com>
  4. #
  5. # This is free and unencumbered software released into the public domain.
  6. # See the file COPYING for more details, or visit <http://unlicense.org>.
  7. class Flux:
  8. def __init__(self, index_list, flux_list, sample_freq):
  9. self.index_list = index_list
  10. self.list = flux_list
  11. self.sample_freq = sample_freq
  12. def __str__(self):
  13. s = "Sample Frequency: %f MHz\n" % (self.sample_freq/1000000)
  14. s += "Total Flux: %u\n" % len(self.list)
  15. rev = 0
  16. for t in self.index_list:
  17. s += "Revolution %u: %.2fms\n" % (rev, t*1000/self.sample_freq)
  18. rev += 1
  19. return s[:-1]
  20. @classmethod
  21. def from_bitarray(cls, bitarray, bitrate, timing=None):
  22. if not timing:
  23. timing = [1000] * len(bitarray)
  24. timing_i = iter(timing)
  25. flux_list = []
  26. count = 0
  27. for bit in bitarray:
  28. count += next(timing_i)
  29. if bit:
  30. flux_list.append(count)
  31. count = 0
  32. flux_list[0] += count
  33. return Flux([sum(flux_list)], flux_list, bitrate * 1000)
  34. # Local variables:
  35. # python-indent: 4
  36. # End: