Example #1
0
def plot(x, y, db=True):
    """Plot feature and targets"""
    from signals.utils import Figure, Subplot
    fig = Figure('System Input & Output')
    fig.add(Subplot.AmplitudeSpectrum(x, prefix='Input Signal', db=db))
    fig.add(Subplot.AmplitudeSpectrum(y, prefix='Output Signal', db=db))
    fig.plot()
Example #2
0
def define_and_plot(*args, **kwargs):
    from signals.generator import multi_tone
    from signals.utils import Figure, Subplot

    # Initiate model
    model = Volterra()
    model.set_kernel((0, ), 1.0)
    model.set_kernel((2, ), 0.0)
    model.set_kernel((0, 0), 0.0)
    model.set_kernel((2, 2), 0.0)
    model.set_kernel((1, 0, 0), 0.2)
    model.set_kernel((2, 2, 2), 0.0)

    # Generate multi tone signal
    freqs = [160, 220]
    signal = multi_tone(freqs, 1000, 2, noise_power=1e-3)
    response = model(signal)

    # Plot
    title = 'Volterra Response, Input freqs = {}'.format(freqs)
    fig = Figure(title)
    fig.add(Subplot.PowerSpectrum(signal, prefix='Input Signal'))
    prefix = 'System Response'
    fig.add(Subplot.PowerSpectrum(response, prefix=prefix))
    fig.plot()
Example #3
0
 def plot(self, db=True):
     from signals.utils import Figure, Subplot
     x = self.signls[0]
     y = self.responses[0]
     fig = Figure('[{}] System Input & Output'.format(self.name))
     fig.add(Subplot.AmplitudeSpectrum(x, prefix='Input Signal', db=db))
     fig.add(Subplot.AmplitudeSpectrum(y, prefix='Output Signal', db=db))
     fig.plot()
Example #4
0
def separate_test(*args, **kwargs):
    from signals.generator import multi_tone
    from signals.utils import Figure, Subplot

    # Initiate model
    model = Volterra(degree=3, memory_depth=3)
    model.set_kernel((0, ), 1.0)
    model.set_kernel((1, ), 0.2)
    model.set_kernel((2, ), 0.0)
    model.set_kernel((0, 0), 0.0)
    model.set_kernel((1, 0), 0.0)
    model.set_kernel((1, 1), 0.0)
    model.set_kernel((2, 0), 0.1)
    model.set_kernel((2, 1), 0.0)
    model.set_kernel((2, 2), 0.1)
    model.set_kernel((0, 0, 0), 0.0)
    model.set_kernel((1, 0, 0), 0.0)
    model.set_kernel((1, 1, 0), 0.0)
    model.set_kernel((1, 1, 1), 0.001)
    model.set_kernel((2, 0, 0), 0.0)
    model.set_kernel((2, 1, 0), 0.0)
    model.set_kernel((2, 2, 0), 0.002)
    model.set_kernel((2, 2, 1), 0.0)
    model.set_kernel((2, 2, 2), 0.0)

    # Generate multi tone signal
    freqs = [160, 220]
    signal = multi_tone(freqs, 1000, 2, noise_power=1e-3)
    response = model(signal)

    max_order = 2
    yn = model.separate_interp(signal, max_order=max_order, verbose=True)
    truth = model.separate(signal, max_order=max_order)

    print('>> rms(response) = {:.4f}'.format(
        np.linalg.norm(response) / signal.size))
    for n in range(len(yn)):
        delta = np.linalg.norm(yn[n] - truth[n]) / signal.size
        print(':: Delta_{} = {:.4f}'.format(n + 1, delta))

    # Separate Kernel

    # Plot
    bshow = True
    if bshow:
        for n in range(len(yn)):
            fig = Figure('Volterra Separation order - {}'.format(n + 1))
            fig.add(Subplot.PowerSpectrum(response, prefix='response'))
            fig.add(
                Subplot.PowerSpectrum(truth[n],
                                      prefix='truth order - {}'.format(n + 1)))
            fig.add(
                Subplot.PowerSpectrum(yn[n],
                                      prefix='pred order - {}'.format(n + 1)))
            fig.plot()
Example #5
0
def plot(system_output, wiener_output, wiener_delta, vn_output, vn_delta,
          homo_str):
  form_title = 'Input Frequencies = {}'.format(TEST_FREQS)
  fig = Figure(form_title)
  # Add ground truth
  prefix = 'System Output, $||y|| = {:.4f}$'.format(system_output.norm)
  fig.add(Subplot.PowerSpectrum(system_output, prefix=prefix))
  # Add
  prefix = 'Wiener Output, $||\Delta|| = {:.4f}$'.format(wiener_delta.norm)
  fig.add(Subplot.PowerSpectrum(wiener_output, prefix=prefix,
                                Delta=wiener_delta))
  # Add
  prefix = 'VN_{:.2f} Output, $||\Delta|| = {:.4f}$'.format(
    homo_str, vn_delta.norm)
  fig.add(Subplot.PowerSpectrum(vn_output, prefix=prefix, Delta=vn_delta))
  fig.plot(ylim=True)
Example #6
0
def define_and_plot(*args, **kwargs):
    from signals.generator import multi_tone
    from signals.utils import Figure, Subplot

    # Initiate model
    model = Volterra(degree=3, memory_depth=3)
    model.set_kernel((0, ), 1.0)
    model.set_kernel((1, ), 0.3)
    model.set_kernel((2, ), 0.0)
    model.set_kernel((0, 0), 0.0)
    model.set_kernel((1, 0), 0.0)
    model.set_kernel((1, 1), 0.0)
    model.set_kernel((2, 0), 0.1)
    model.set_kernel((2, 1), 0.0)
    model.set_kernel((2, 2), 0.0)
    model.set_kernel((0, 0, 0), 0.0)
    model.set_kernel((1, 0, 0), 0.0)
    model.set_kernel((1, 1, 0), 0.0)
    model.set_kernel((1, 1, 1), 0.0)
    model.set_kernel((2, 0, 0), 0.0)
    model.set_kernel((2, 1, 0), 0.0)
    model.set_kernel((2, 2, 0), 0.0)
    model.set_kernel((2, 2, 1), 0.0)
    model.set_kernel((2, 2, 2), 0.0)

    # Generate multi tone signal
    freqs = [160, 220]
    signal = multi_tone(freqs, 1000, 2, noise_power=1e-3)
    order = 2
    response = model.inference(signal, order)

    delta = np.linalg.norm(signal - response) / signal.size
    print('>> Delta = {:.4f}'.format(delta))

    # Plot
    title = 'Volterra Response, Input freqs = {}'.format(freqs)
    fig = Figure(title)
    fig.add(Subplot.PowerSpectrum(signal, prefix='Input Signal'))
    prefix = 'System Response{}'.format(
        ' Order-{}'.format(order) if not order is None else '')

    response = model(signal) - model.inference(signal, 2)
    fig.add(Subplot.PowerSpectrum(response, prefix=prefix))
    fig.plot()
Example #7
0
    def plot_laguerre_3D(alpha=0.2, L=50, contour=True):
        from signals.utils import Figure, Subplot
        import matplotlib.pyplot as plt

        t = np.arange(L + 1)
        # x, y = np.meshgrid(t, t)
        z = np.zeros(shape=(L + 1, L + 1))
        for j in range(L + 1):
            z[j] = Laguerre.phi_j(alpha, j, t)

        def plot_function(*args):
            if contour:
                plt.contour(z)
                plt.xlabel('Time Units')
                plt.ylabel('Order of Laguerre function')
            else:
                raise NotImplementedError('!! 3D not implemented')

        fig = Figure('Discrete Laguerre Functions 3D')
        fig.add(Subplot.Custom(plot_function))
        fig.plot()
Example #8
0
    def plot_laguerre(alphas=None, lags=25, js=None):
        # Check inputs
        if alphas is None: alphas = [0.2, 0.6]
        if js is None: js = [0, 1, 2, 3, 4]
        t = np.arange(lags + 1)

        if not isinstance(alphas, list) and not isinstance(alphas, tuple):
            alphas = [alphas]
        for alpha in alphas:
            if not np.isscalar(alpha) or not 0 < alpha < 1:
                raise ValueError(
                    '!! alpha must be a real number between 0 and 1')

        for point in t:
            if not point == int(point) >= 0:
                raise TypeError(
                    '!! Each point in t must be a non-negative integer')

        # Plot
        from signals.utils import Figure, Subplot
        fig = Figure('Discrete Orthogonal Laguerre Functions')
        title = (r"Laguerre bases $\{\phi_j[n;\alpha]\}$")
        for alpha in alphas:
            ys = []
            legends = []
            for j in js:
                ys.append(Laguerre.phi_j(alpha, j, t))
                legends.append(r'$j = {}$'.format(j))
            fig.add(
                Subplot.Default(t,
                                ys,
                                legends=legends,
                                xlabel='Time Unit',
                                title=title +
                                r', $\alpha = {}$'.format(alpha)))
        fig.plot()
Example #9
0
wiener_ratio = wiener_delta.norm / system_output.norm * 100
print('>> Wiener err ratio = {:.2f} %'.format(wiener_ratio))

# MLP
mlp_output = mlp(signal)
mlp_delta = system_output - mlp_output
mlp_ratio = mlp_delta.norm / system_output.norm * 100
print('>> MLP err ratio = {:.2f} %'.format(mlp_ratio))

# Plot
if bshow:
    form_title = 'Input Frequencies = {}'.format(freqs)

    # Input & Output
    fig = Figure(form_title)
    fig.add(Subplot.PowerSpectrum(signal, prefix='System Input'))
    fig.add(Subplot.PowerSpectrum(system_output, prefix='System Output'))
    # fig.plot()

    # Compare outputs
    fig = Figure(form_title)
    # fig.add(Subplot.PowerSpectrum(signal, prefix='System Input'))
    prefix = 'System Output, $||y|| = {:.4f}$'.format(system_output.norm)
    fig.add(Subplot.PowerSpectrum(system_output, prefix=prefix))
    prefix = 'Wiener Output, $||\Delta|| = {:.4f}$'.format(wiener_delta.norm)
    fig.add(
        Subplot.PowerSpectrum(wiener_output, prefix=prefix,
                              Delta=wiener_delta))
    prefix = 'MLP Output, $||\Delta|| = {:.4f}$'.format(mlp_delta.norm)
    fig.add(Subplot.PowerSpectrum(mlp_output, prefix=prefix, Delta=mlp_delta))
    fig.plot(ylim=(-75, 5))
Example #10
0
    print(">> Running module system_.py")

    fs = 3000
    duration = 1
    freqs = [500, 800]
    vrms = [2, 1]
    phases = [0, np.pi]
    signal = multi_tone(freqs,
                        fs,
                        duration,
                        vrms=vrms,
                        phases=phases,
                        noise_power=1e-3)

    def response(input_):
        y = np.zeros_like(input_)
        for i in range(len(input_)):
            x = input_[i]
            x_p1 = input_[i - 1] if i != 0 else 0
            y[i] = x * x_p1 if x > 0 else 0.4 * x + 0.9 * x_p1
        return y

    system = System(response)
    output = system(signal)

    fig = Figure('Input & Output')
    db = True
    fig.add(Subplot.AmplitudeSpectrum(signal, prefix='Input Signal', db=db))
    fig.add(Subplot.AmplitudeSpectrum(output, prefix='Output Signal', db=db))
    fig.plot()