Skip to content
gen_wf.py 2.08 KiB
Newer Older
Nicolas Nunez Barreto's avatar
Nicolas Nunez Barreto committed
# -*- coding: utf-8 -*-
"""
Uso la info de parametros_AM.npz para armar el array que se debe cargar
en el Uryukul
"""


import os
import argparse
from numpy import *
import matplotlib.pyplot as plt

FILENAME = "parametros_AM.npz"


#%% Auxiliares

def first(iterable, condition = lambda x: True , index=True):
    """
    Returns the first item in the `iterable` that
    satisfies the `condition`.

    If the condition is not given, returns the first item of
    the iterable.

    Raises `StopIteration` if no item satysfing the condition is found.

    >>> first( (1,2,3), condition=lambda x: x % 2 == 0)
    2
    >>> first(range(3, 100))
    3
    >>> first( () )
    Traceback (most recent call last):
    ...
    StopIteration
    """

    if index:
        return next( ii for ii,x in enumerate(iterable) if condition(x))
    else:
        return next(x for x in iterable if condition(x))



#%%

dd = load(FILENAME)['dd']


# Formato:
#        [frec en HZ, largo vector, step en clks , nro de armonico]



def get_urukul_array(f0, method='next'):
    """
    get the array values for AM modulation in urukul for the frequency f0

    params:
        f0 (float): frequency to set on AM
        method    : next (first value greater than f0), close (closest value to f0)
    """

    if  method=='next':
        param = first(dd , lambda x: x[0]>=f0 , index=False)
    else:
        ValueError('close still not implemented')

    frec, num_samples, clock_step, n_harmonic = param

    xx         = linspace(0, 2*pi*n_harmonic, num=num_samples, endpoint=False)
    modulation = sin(xx)

    return modulation, frec, clock_step


def convert_amp_to_data(amp):
    """
    takes amp values (from 0 to 1) and returns data values (suitable for DDS load)
    """
    MAX = 2**10-1
    N   = 10

    if not iterable(amp):
        amp = [amp]

    return list([ int(round(val*MAX)) << (32 - (N - 1)) for val in amp ])




f0    = 564001 # Hz
depth = 0.1

modulation, frec, clock_step = get_urukul_array(f0)

modulation                   = (modulation/2 * depth)+0.9

data                         = convert_amp_to_data( modulation )