You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

median.py 1.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/python
  2. import nilmtools.filter, scipy.signal
  3. def main(argv = None):
  4. f = nilmtools.filter.Filter()
  5. parser = f.setup_parser("Median Filter")
  6. group = parser.add_argument_group("Median filter options")
  7. group.add_argument("-z", "--size", action="store", type=int, default=25,
  8. help = "median filter size (default %(default)s)")
  9. group.add_argument("-d", "--difference", action="store_true",
  10. help = "store difference rather than filtered values")
  11. try:
  12. args = f.parse_args(argv)
  13. except nilmtools.filter.MissingDestination as e:
  14. print("Source is %s (%s)" % (e.src.path, e.src.layout))
  15. print("Destination %s doesn't exist" % (e.dest.path))
  16. print("You could make it with a command like:")
  17. print(" nilmtool -u %s create %s %s" % (e.dest.url,
  18. e.dest.path, e.src.layout))
  19. raise SystemExit(1)
  20. meta = f.client_src.stream_get_metadata(f.src.path)
  21. f.check_dest_metadata({ "median_filter_source": f.src.path,
  22. "median_filter_size": args.size,
  23. "median_filter_difference": repr(args.difference) })
  24. f.process_numpy(median_filter, args = (args.size, args.difference))
  25. def median_filter(data, interval, args, insert, final):
  26. (size, diff) = args
  27. (rows, cols) = data.shape
  28. for i in range(cols - 1):
  29. filtered = scipy.signal.medfilt(data[:, i+1], size)
  30. if diff:
  31. data[:, i+1] -= filtered
  32. else:
  33. data[:, i+1] = filtered
  34. insert(data)
  35. return rows
  36. if __name__ == "__main__":
  37. main()