From 698cb6ef26da30a6f34d6e52757d4ca7257e98ff Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Wed, 27 Mar 2013 19:30:15 -0400 Subject: [PATCH] More work on sinefit --- src/sinefit.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/sinefit.py b/src/sinefit.py index 54c45b8..b7881cd 100755 --- a/src/sinefit.py +++ b/src/sinefit.py @@ -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)