flux.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. self.terminate_at_index = True
  13. def __str__(self):
  14. s = "Sample Frequency: %f MHz\n" % (self.sample_freq/1000000)
  15. s += "Total Flux: %u\n" % len(self.list)
  16. rev = 0
  17. for t in self.index_list:
  18. s += "Revolution %u: %.2fms\n" % (rev, t*1000/self.sample_freq)
  19. rev += 1
  20. return s[:-1]
  21. def flux_for_writeout(self):
  22. return self
  23. @classmethod
  24. def from_bitarray(cls, bitarray, bitrate, timing=None):
  25. if not timing:
  26. timing = [1000] * len(bitarray)
  27. timing_i = iter(timing)
  28. flux_list = []
  29. count = 0
  30. for bit in bitarray:
  31. count += next(timing_i)
  32. if bit:
  33. flux_list.append(count)
  34. count = 0
  35. flux_list[0] += count
  36. return Flux([sum(flux_list)], flux_list, bitrate * 1000)
  37. # Local variables:
  38. # python-indent: 4
  39. # End: