Browse Source

More work on sinefit

tags/nilmtools-1.0
Jim Paris 11 years ago
parent
commit
698cb6ef26
1 changed files with 34 additions and 2 deletions
  1. +34
    -2
      src/sinefit.py

+ 34
- 2
src/sinefit.py View File

@@ -55,8 +55,38 @@ def process(data, interval, args, insert_function, final):
start = 0
while start < (rows - N):
this = data[start:start+N, column]
t_min = data[start, 0]/1e6
t_max = data[start+N-1, 0]/1e6

# Do 4-parameter sine wave fit
(A, f0, phi, C) = sfit4(this, fs)
start += N

# Check bounds. If frequency is too crazy, ignore it
if f0 < (f_expected/2) or f0 > (f_expected*2):
continue

p.plot(arange(N), this)

# Period starts when the argument of cosine is 3*pi/2 degrees,
# so we're looking for sample number:
# f0/fs * 2 * pi * n + phi = 3 * pi / 2
# f0/fs * 2 * pi * n = (3 * pi / 2 - phi)
# n = (3 * pi / 2 - phi) / (f0/fs * 2 * pi)
zc_n = (3 * pi / 2 - phi) / (f0 / fs * 2 * pi)
period_n = fs/f0
# Add periods to make N positive
while zc_n < 0:
zc_n += period_n
# Mark the zero crossings until we're a half period away
# from the end of the window.
while zc_n < (N - period_n/2):
p.plot(zc_n, C, 'ro')
zc_n += period_n
p.plot(zc_n, C, 'bo')
p.plot(min(N,zc_n + period_n/2), C, 'go')

p.show()
start += min(N, round(zc_n + period_n/2))

print "processed",start,"rows"
return start
@@ -73,7 +103,9 @@ def sfit4(data, fs):

Output:
Parameters [A, f0, phi, C] to fit the equation
x[n] = A * cos(f0/fs * 2 * pi * n + phi) + C
x[n] = A * cos(f0/fs * 2 * pi * n + phi) + C
where n is sample number. Or, as a function of time:
x(t) = A * cos(f0 * 2 * pi * t + phi) + C

by Jim Paris
(Verified to match sfit4.m)


Loading…
Cancel
Save