Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- 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 )