# Path: thrifty/toads_data.py
# class DetectionResult(object):
#     def __init__(self, timestamp, block, soa, carrier_info, corr_info,
#                  rxid=None, txid=None):
#     def serialize(self):
#     def deserialize(cls, string, with_rxid=False, with_txid=False):
# def _load_toads(stream, with_rxid=True, with_txid=True):
# def load_toad(stream):
# def load_toads(stream):
# def toads_array(detections, with_ids=True):
# 
# Path: thrifty/carrier_detect.py
# def fft_range_index(start, stop, length):
# def detect(fft_mag, thresh_coeffs, window=None, peak_filter=None):
# def _estimate_noise(fft_mag, peak_mag):
# def _calculate_threshold(fft_mag, thresh_coeffs, noise_rms):
# def _get_window(array, window):
# def _filter(fft_mag, weights):
# def _window_peak(fft_mag, window, peak_filter):


import numpy as np
from scipy.optimize import curve_fit
from thrifty import toads_data
from thrifty import carrier_detect
    def _interpolator(fft_mag, peak_idx):
        """Curve fitting of Dirichlet kernel to FFT."""
        xdata = np.array(np.arange(-(width//2), width//2+1))
        ydata = fft_mag[peak_idx + xdata]
        initial_guess = (fft_mag[peak_idx], 0)
        popt, _ = curve_fit(_fit_model, xdata, ydata, p0=initial_guess)
        amplitude, fit_offset = popt
        if return_amplitude:
            return amplitude, fit_offset
        else:
            return fit_offset

    return _interpolator


def parabolic_interpolator(fft_mag, peak_idx):
    """Estimate sub-bin carrier frequency by fitting a parabola."""
    # pylint: disable=invalid-name
    a, b, c = fft_mag[peak_idx-1], fft_mag[peak_idx], fft_mag[peak_idx+1]
    offset = (c - a) / (4*b - 2*a - 2*c)
    return offset


def make_polyfit_interpolator(width):
    """Estimate sub-bin carrier frequency by fitting a quadratic function with
    polyfit."""

    def _interpolator(fft_mag, peak_idx):
        xdata = np.array(np.arange(-(width//2), width//2+1))
        ydata = fft_mag[peak_idx + xdata]

# Path: thrifty/toads_data.py
# class DetectionResult(object):
#     def __init__(self, timestamp, block, soa, carrier_info, corr_info,
#                  rxid=None, txid=None):
#     def serialize(self):
#     def deserialize(cls, string, with_rxid=False, with_txid=False):
# def _load_toads(stream, with_rxid=True, with_txid=True):
# def load_toad(stream):
# def load_toads(stream):
# def toads_array(detections, with_ids=True):
# 
# Path: thrifty/carrier_detect.py
# def fft_range_index(start, stop, length):
# def detect(fft_mag, thresh_coeffs, window=None, peak_filter=None):
# def _estimate_noise(fft_mag, peak_mag):
# def _calculate_threshold(fft_mag, thresh_coeffs, noise_rms):
# def _get_window(array, window):
# def _filter(fft_mag, weights):
# def _window_peak(fft_mag, window, peak_filter):


# Path: thrifty/carrier_sync.py
import numpy as np
from scipy.optimize import curve_fit
from thrifty import toads_data
from thrifty import carrier_detect

    def _interpolator(fft_mag, peak_idx):
        """Curve fitting of Dirichlet kernel to FFT."""
        xdata = np.array(np.arange(-(width//2), width//2+1))
        ydata = fft_mag[peak_idx + xdata]
        initial_guess = (fft_mag[peak_idx], 0)
        popt, _ = curve_fit(_fit_model, xdata, ydata, p0=initial_guess)
        amplitude, fit_offset = popt
        if return_amplitude:
            return amplitude, fit_offset
        else:
            return fit_offset

    return _interpolator


def parabolic_interpolator(fft_mag, peak_idx):
    """Estimate sub-bin carrier frequency by fitting a parabola."""
    # pylint: disable=invalid-name
    a, b, c = fft_mag[peak_idx-1], fft_mag[peak_idx], fft_mag[peak_idx+1]
    offset = (c - a) / (4*b - 2*a - 2*c)
    return offset


def make_polyfit_interpolator(width):
    """Estimate sub-bin carrier frequency by fitting a quadratic function with
    polyfit."""

    def _interpolator(fft_mag, peak_idx):
        xdata = np.array(np.arange(-(width//2), width//2+1))
        ydata = fft_mag[peak_idx + xdata]

        coeffs = np.polyfit(xdata, ydata, 2)