|
|
@@ -3,8 +3,18 @@ |
|
|
|
# Miscellaenous useful mathematical functions |
|
|
|
from nilmdb.utils.printf import * |
|
|
|
from numpy import * |
|
|
|
from scipy import * |
|
|
|
import scipy |
|
|
|
|
|
|
|
def numpy_raise_errors(func): |
|
|
|
def wrap(*args, **kwargs): |
|
|
|
old = seterr('raise') |
|
|
|
try: |
|
|
|
return func(*args, **kwargs) |
|
|
|
finally: |
|
|
|
seterr(**old) |
|
|
|
return wrap |
|
|
|
|
|
|
|
@numpy_raise_errors |
|
|
|
def sfit4(data, fs): |
|
|
|
"""(A, f0, phi, C) = sfit4(data, fs) |
|
|
|
|
|
|
@@ -25,10 +35,12 @@ def sfit4(data, fs): |
|
|
|
(Verified to match sfit4.m) |
|
|
|
""" |
|
|
|
N = len(data) |
|
|
|
if N < 2: |
|
|
|
raise ValueError("bad data") |
|
|
|
t = linspace(0, (N-1) / float(fs), N) |
|
|
|
|
|
|
|
## Estimate frequency using FFT (step b) |
|
|
|
Fc = fft(data) |
|
|
|
Fc = scipy.fft.fft(data) |
|
|
|
F = abs(Fc) |
|
|
|
F[0] = 0 # eliminate DC |
|
|
|
|
|
|
@@ -76,7 +88,7 @@ def sfit4(data, fs): |
|
|
|
phi = arctan2(s[0], s[1]) # eqn B.22 (flipped for sin instead of cos) |
|
|
|
C = s[2] |
|
|
|
return (A, f0, phi, C) |
|
|
|
except Exception as e: |
|
|
|
except Exception as e: # pragma: no cover (not sure if we can hit this?) |
|
|
|
# something broke down; just return zeros |
|
|
|
return (0, 0, 0, 0) |
|
|
|
|
|
|
|