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.
 
 
 
 

199 lines
7.1 KiB

  1. #!/usr/bin/env python3
  2. # Sine wave fitting.
  3. from nilmdb.utils.printf import printf, sprintf
  4. import nilmtools.filter
  5. import nilmtools.math
  6. from nilmdb.utils.time import (timestamp_to_human,
  7. timestamp_to_seconds,
  8. seconds_to_timestamp)
  9. import numpy
  10. import sys
  11. # import pylab as p
  12. def main(argv=None):
  13. f = nilmtools.filter.Filter()
  14. parser = f.setup_parser("Sine wave fitting")
  15. group = parser.add_argument_group("Sine fit options")
  16. group.add_argument('-c', '--column', action='store', type=int,
  17. help='Column number (first data column is 1)')
  18. group.add_argument('-f', '--frequency', action='store', type=float,
  19. default=60.0,
  20. help='Approximate frequency (default: %(default)s)')
  21. group.add_argument('-m', '--min-freq', action='store', type=float,
  22. help='Minimum valid frequency '
  23. '(default: approximate frequency / 2))')
  24. group.add_argument('-M', '--max-freq', action='store', type=float,
  25. help='Maximum valid frequency '
  26. '(default: approximate frequency * 2))')
  27. group.add_argument('-a', '--min-amp', action='store', type=float,
  28. default=20.0,
  29. help='Minimum signal amplitude (default: %(default)s)')
  30. # Parse arguments
  31. try:
  32. args = f.parse_args(argv)
  33. except nilmtools.filter.MissingDestination as e:
  34. rec = "float32_3"
  35. print("Source is %s (%s)" % (e.src.path, e.src.layout))
  36. print("Destination %s doesn't exist" % (e.dest.path))
  37. print("You could make it with a command like:")
  38. print(" nilmtool -u %s create %s %s" % (e.dest.url, e.dest.path, rec))
  39. raise SystemExit(1)
  40. if args.column is None or args.column < 1:
  41. parser.error("need a column number >= 1")
  42. if args.frequency < 0.1:
  43. parser.error("frequency must be >= 0.1")
  44. if args.min_freq is None:
  45. args.min_freq = args.frequency / 2
  46. if args.max_freq is None:
  47. args.max_freq = args.frequency * 2
  48. if (args.min_freq > args.max_freq or
  49. args.min_freq > args.frequency or
  50. args.max_freq < args.frequency):
  51. parser.error("invalid min or max frequency")
  52. if args.min_amp < 0:
  53. parser.error("min amplitude must be >= 0")
  54. f.check_dest_metadata({"sinefit_source": f.src.path,
  55. "sinefit_column": args.column})
  56. f.process_numpy(process, args=(args.column, args.frequency, args.min_amp,
  57. args.min_freq, args.max_freq))
  58. class SuppressibleWarning(object):
  59. def __init__(self, maxcount=10, maxsuppress=100):
  60. self.maxcount = maxcount
  61. self.maxsuppress = maxsuppress
  62. self.count = 0
  63. self.last_msg = ""
  64. def _write(self, sec, msg):
  65. if sec:
  66. now = timestamp_to_human(seconds_to_timestamp(sec)) + ": "
  67. else:
  68. now = ""
  69. sys.stderr.write(now + msg)
  70. def warn(self, msg, seconds=None):
  71. self.count += 1
  72. if self.count <= self.maxcount:
  73. self._write(seconds, msg)
  74. if (self.count - self.maxcount) >= self.maxsuppress:
  75. self.reset()
  76. def reset(self, seconds=None):
  77. if self.count > self.maxcount:
  78. self._write(seconds, sprintf("(%d warnings suppressed)\n",
  79. self.count - self.maxcount))
  80. self.count = 0
  81. def process(data, interval, args, insert_function, final):
  82. (column, f_expected, a_min, f_min, f_max) = args
  83. rows = data.shape[0]
  84. # Estimate sampling frequency from timestamps
  85. ts_min = timestamp_to_seconds(data[0][0])
  86. ts_max = timestamp_to_seconds(data[-1][0])
  87. if ts_min >= ts_max: # pragma: no cover; process_numpy shouldn't send this
  88. return 0
  89. fs = (rows-1) / (ts_max - ts_min)
  90. # Pull out about 3.5 periods of data at once;
  91. # we'll expect to match 3 zero crossings in each window
  92. N = max(int(3.5 * fs / f_expected), 10)
  93. # If we don't have enough data, don't bother processing it
  94. if rows < N:
  95. return 0
  96. warn = SuppressibleWarning(3, 1000)
  97. # Process overlapping windows
  98. start = 0
  99. num_zc = 0
  100. last_inserted_timestamp = None
  101. while start < (rows - N):
  102. this = data[start:start+N, column]
  103. t_min = timestamp_to_seconds(data[start, 0])
  104. # t_max = timestamp_to_seconds(data[start+N-1, 0])
  105. # Do 4-parameter sine wave fit
  106. (A, f0, phi, C) = nilmtools.math.sfit4(this, fs)
  107. # Check bounds. If frequency is too crazy, ignore this window
  108. if f0 < f_min or f0 > f_max:
  109. warn.warn(sprintf("frequency %s outside valid range %s - %s\n",
  110. str(f0), str(f_min), str(f_max)), t_min)
  111. start += N
  112. continue
  113. # If amplitude is too low, results are probably just noise
  114. if A < a_min:
  115. warn.warn(sprintf("amplitude %s below minimum threshold %s\n",
  116. str(A), str(a_min)), t_min)
  117. start += N
  118. continue
  119. # p.plot(arange(N), this)
  120. # p.plot(arange(N), A * sin(f0/fs * 2 * pi * arange(N) + phi) + C, 'g')
  121. # Period starts when the argument of sine is 0 degrees,
  122. # so we're looking for sample number:
  123. # n = (0 - phi) / (f0/fs * 2 * pi)
  124. zc_n = (0 - phi) / (f0 / fs * 2 * numpy.pi)
  125. period_n = fs/f0
  126. # Add periods to make N positive
  127. while zc_n < 0:
  128. zc_n += period_n
  129. last_zc = None
  130. # Mark the zero crossings until we're a half period away
  131. # from the end of the window
  132. while zc_n < (N - period_n/2):
  133. # p.plot(zc_n, C, 'ro')
  134. t = t_min + zc_n / fs
  135. if (last_inserted_timestamp is None or
  136. t > last_inserted_timestamp):
  137. insert_function([[seconds_to_timestamp(t), f0, A, C]])
  138. last_inserted_timestamp = t
  139. warn.reset(t)
  140. else: # pragma: no cover -- this is hard to trigger,
  141. # if it's even possible at all; I think it would require
  142. # some jitter in how the waves fit, across a window boundary.
  143. warn.warn("timestamp overlap\n", t)
  144. num_zc += 1
  145. last_zc = zc_n
  146. zc_n += period_n
  147. # Advance the window one quarter period past the last marked
  148. # zero crossing, or advance the window by half its size if we
  149. # didn't mark any.
  150. if last_zc is not None:
  151. advance = min(last_zc + period_n/4, N)
  152. else:
  153. advance = N/2
  154. # p.plot(advance, C, 'go')
  155. # p.show()
  156. start = int(round(start + advance))
  157. # Return the number of rows we've processed
  158. warn.reset(last_inserted_timestamp)
  159. if last_inserted_timestamp:
  160. now = timestamp_to_human(seconds_to_timestamp(
  161. last_inserted_timestamp)) + ": "
  162. else:
  163. now = ""
  164. printf("%sMarked %d zero-crossings in %d rows\n", now, num_zc, start)
  165. return start
  166. if __name__ == "__main__":
  167. main()