Commit c361ab42 authored by Nicolas Nunez Barreto's avatar Nicolas Nunez Barreto

agrego meds de modos

parent 023ded66
# -*- coding: utf-8 -*-
"""
CYthon Impel,entation
"""
# https://cython.readthedocs.io/en/latest/src/tutorial/cython_tutorial.html
def optimal_param(float frequency_obj):
cdef long frequency_sample = 250_000_000
cdef float frequency = 0
cdef float delta = frequency_obj
cdef int n, N, k
cdef int n_o, N_o, k_o
# cdef int n_2, N_2, k_2
cdef bint next_break
n_o, N_o, k_o = 0,0,0
# n_2, N_2, k_2 = 0,0,0
for n in range(10,1024):
next_break = False
for k in range(1,1000):
if next_break:
break
if frequency_sample*1024/(n*k) < frequency_obj:
next_break = True
N = int(round(n*k*frequency_obj/frequency_sample))
if N<1 or n/N<10:
continue
frequency = frequency_sample*N/(n*k)
if abs(frequency-frequency_obj) < delta :
delta = abs(frequency-frequency_obj)
n_o, N_o, k_o = n, N, k
frequency = frequency_sample*N_o/(n_o*k_o)
return frequency, n_o, N_o, k_o
def optimal_param3(float frequency_obj):
cdef long frequency_sample = 250_000_000
cdef float frequency = 0
cdef float delta = frequency_obj
cdef int n, N, k
cdef int n_o, N_o, k_o
# cdef int n_2, N_2, k_2
cdef bint next_break
n_o, N_o, k_o = 0,0,0
# n_2, N_2, k_2 = 0,0,0
for n in range(10,1005):
next_break = False
for k in range(1,1000):
if next_break:
break
if frequency_sample*1024/(n*k) < frequency_obj:
next_break = True
N = int(round(n*k*frequency_obj/frequency_sample))
if N<1 or n/N<10:
continue
frequency = frequency_sample*N/(n*k)
if abs(frequency-frequency_obj) < delta :
delta = abs(frequency-frequency_obj)
n_o, N_o, k_o = n, N, k
frequency = frequency_sample*N_o/(n_o*k_o)
return frequency, n_o, N_o, k_o
def optimal_param2(float frequency_obj):
cdef long frequency_sample = 250_000_000
cdef float frequency = 0
cdef float delta = frequency_obj
cdef int n, N, k
cdef int n_o, N_o, k_o
cdef int n_2, N_2, k_2
cdef int n_3, N_3, k_3
cdef float frequency1 = 0
cdef float frequency2 = 0
cdef float frequency3 = 0
cdef bint next_break
n_o, N_o, k_o = 0,0,0
n_2, N_2, k_2 = 0,0,0
n_3, N_3, k_3 = 0,0,0
for n in range(10,1024):
next_break = False
for k in range(1,1000):
if next_break:
break
if frequency_sample*1024/(n*k) < frequency_obj:
next_break = True
# print(n,k, int(round(n*k*frequency_obj/frequency_sample)) )
N = int(round(n*k*frequency_obj/frequency_sample))
if N<1 or n/N<10:
continue
frequency = frequency_sample*N/(n*k)
if abs(frequency-frequency_obj) <= delta :
delta = abs(frequency-frequency_obj)
n_3, N_3, k_3, frequency3 = n_2, N_2, k_2, frequency2
n_2, N_2, k_2, frequency2 = n_o, N_o, k_o, frequency1
n_o, N_o, k_o, frequency1 = n, N, k, frequency
#frequency = frequency_sample*N_o/(n_o*k_o)
return frequency1, n_o, N_o, k_o, frequency2, n_2, N_2, k_2, frequency3, n_3, N_3, k_3
#%% Version con tolerancia que devuelve vectores largos
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
def optimal_param_tol(float frequency_obj, float tolerance):
cdef long frequency_sample = 250_000_000
cdef double frequency = 0
cdef int n, N, k
cdef bint next_break
# cdef int i
# # allocate number * sizeof(double) bytes of memory
# cdef double *my_array = <double *> malloc(
# number * sizeof(double))
# if not my_array:
# raise MemoryError()
# try:
# ran = random.normalvariate
# for i in range(number):
# my_array[i] = ran(0, 1)
# # ... let's just assume we do some more heavy C calculations here to make up
# # for the work that it takes to pack the C double values into Python float
# # objects below, right after throwing away the existing objects above.
# return [x for x in my_array[:number]]
# finally:
# # return the previously allocated memory to the system
# free(my_array)
cdef int* n_o
cdef int* N_o
cdef int* k_o
cdef double* frequencies
cdef int* mem_tmp
cdef double* mem_tmp2
n_o = <int*> PyMem_Malloc( 1000 * sizeof(int) )
if not n_o:
raise MemoryError("mem for n_o could not be allocated")
N_o = <int*> PyMem_Malloc( 1000 * sizeof(int) )
if not N_o:
raise MemoryError("mem for N_o could not be allocated")
k_o = <int*> PyMem_Malloc( 1000 * sizeof(int) )
if not k_o:
raise MemoryError("mem for k_o could not be allocated")
frequencies = <double*> PyMem_Malloc( 1000 * sizeof(double) )
if not frequencies:
raise MemoryError("mem for frequencies could not be allocated")
# cdef int[1000] n_o
# cdef int[1000] N_o
# cdef int[1000] k_o
# cdef float[1000] frequencies
cdef int i = 0
cdef int max_i = 1000
try:
# finally:
# # return the previously allocated memory to the system
# free(my_array)
for n in range(10,1024):
next_break = False
for k in range(1,1000):
if next_break:
break
if frequency_sample*1024/(n*k) < frequency_obj:
next_break = True
N = int(round(n*k*frequency_obj/frequency_sample))
if N<1 or n/N<10:
continue
frequency = frequency_sample*N/(n*k)
if abs(frequency-frequency_obj) < tolerance :
n_o[i] = n
k_o[i] = k
N_o[i] = N
frequencies[i] = frequency
i += 1
if i >- max_i:
max_i += 1000
mem_tmp = <int*> PyMem_Realloc( n_o, max_i * sizeof(int))
if not mem_tmp:
raise MemoryError()
n_o = mem_tmp
mem_tmp = <int*> PyMem_Realloc( k_o, max_i * sizeof(int))
if not mem_tmp:
raise MemoryError()
k_o = mem_tmp
mem_tmp = <int*> PyMem_Realloc( N_o, max_i * sizeof(int))
if not mem_tmp:
raise MemoryError()
N_o = mem_tmp
mem_tmp2 = <double*> PyMem_Realloc( frequencies, max_i * sizeof(double))
if not mem_tmp2:
raise MemoryError()
frequencies = mem_tmp2
#return frequencies, n_o, N_o, k_o
return [x for x in frequencies[:i]]
finally:
# return the previously allocated memory to the system
PyMem_Free(n_o)
PyMem_Free(N_o)
PyMem_Free(k_o)
PyMem_Free(frequencies)
#
#def optimal_param(float frequency_obj):
#
# cdef long frequency_sample = 250_000_000
# cdef float frequency = 0
#
# cdef float delta = frequency_obj
#
# cdef int n, N, k
# cdef int n_o, N_o, k_o
#
# cdef bint next_break
#
# n_o, N_o, k_o = 0,0,0
#
#
# for n in range(10,1024):
# next_break = False
# for k in range(1,1000):
# if next_break:
# break
#
# if frequency_sample*1024/(n*k) < frequency_obj:
# next_break = True
#
# N = int(round(n*k*frequency_obj/frequency_sample))
#
# if N<1:
# continue
#
# frequency = frequency_sample*N/(n*k)
#
# if abs(frequency-frequency_obj) < delta :
# delta = abs(frequency-frequency_obj)
# n_o, N_o, k_o = n, N, k
#
#
# return frequency, n_o, N_o, k_o
#
from setuptools import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("freq_syn.pyx")
)
......@@ -160,11 +160,12 @@ class AD9910RAM(EnvExperiment):
self.create_applets()
for ind,am_freq in enumerate(sequence):
# t0 = time() ; jj=0
t0 = time() #; jj=0
# print(f"{jj}: {time()-t0}") ; jj += 1
# print(f"4step time: {round(time()-t0,2)}")
parameters = get_urukul_params( am_freq )
# print(f"5step time: {round(time()-t0,2)}")
frec, num_samples, clock_step, n_harmonic = parameters
modulation = get_urukul_array(frec, num_samples, n_harmonic)
......@@ -183,6 +184,7 @@ class AD9910RAM(EnvExperiment):
# data = data + [0]*10
# self.fix = True
for ii in range(len(data)):
#print(len(data))
self.data[ii] = data[ii]
# print(self.data)
......@@ -194,20 +196,24 @@ class AD9910RAM(EnvExperiment):
try:
# print(f"6step time: {round(time()-t0,2)}")
self.run_kernel()
# print(f"7step time: {round(time()-t0,2)}")
except:
print("HUBO UNA FALLA DEL run_kernel()")
self.fix = True
data = data + [0]*10
if len(data)<1024-10:
self.fix = True
data = data + [0]*10
for ii in range(len(data)):
self.data[ii] = data[ii]
self.run_kernel()
for ii in range(len(data)):
self.data[ii] = data[ii]
self.run_kernel()
self.mutate_dataset("error_freq", ind, 1 )
sleep(self.scan_time_step)
print(f"step time: {round(time()-t0,2)}")
print("FFIIINNN")
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment