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.
 
 
 
 

47 lines
1.6 KiB

  1. #!/usr/bin/env python3
  2. import nilmtools.filter
  3. import scipy.signal
  4. def main(argv=None):
  5. f = nilmtools.filter.Filter()
  6. parser = f.setup_parser("Median Filter")
  7. group = parser.add_argument_group("Median filter options")
  8. group.add_argument("-z", "--size", action="store", type=int, default=25,
  9. help="median filter size (default %(default)s)")
  10. group.add_argument("-d", "--difference", action="store_true",
  11. help="store difference rather than filtered values")
  12. try:
  13. args = f.parse_args(argv)
  14. except nilmtools.filter.MissingDestination as e:
  15. print("Source is %s (%s)" % (e.src.path, e.src.layout))
  16. print("Destination %s doesn't exist" % (e.dest.path))
  17. print("You could make it with a command like:")
  18. print(" nilmtool -u %s create %s %s" % (e.dest.url,
  19. e.dest.path, e.src.layout))
  20. raise SystemExit(1)
  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()