from machine import Pin
import utime
import math
pines = [2, 3, 4, 5, 6, 7, 8, 9, 10]
def GPIO_SALP(valor, listagpio):
pines_gpio = [Pin(pin, Pin.OUT) for pin in listagpio]
ii = 1
for i in range(8):
pines_gpio[i].value(1 if (valor & ii) else 0)
ii = ii << 1
def f_ecg(t, T=1.0):
"""Modelo de una senal ECG en un periodo T"""
tp = t % T
Ap, Ar, At = 0.25, 1.5, 0.35
if 0.1 <= tp < 0.2:
return Ap * math.sin(math.pi * (tp - 0.1) / 0.1)
elif 0.3 <= tp < 0.32:
return -0.3 + 15 * (tp - 0.3)
elif 0.32 <= tp < 0.38:
return Ar * math.sin(math.pi * (tp - 0.32) / 0.06)
elif 0.38 <= tp < 0.42:
return -0.3 * math.sin(math.pi * (tp - 0.38) / 0.04)
elif 0.6 <= tp < 0.8:
return At * math.sin(math.pi * (tp - 0.6) / 0.2)
return 0.0
def calc_coeficientes(N_arm, T=1.0, M=200):
"""
Calculo numerico de coeficientes de Fourier
N_arm: numero de armonicos
M: puntos de integracion numerica
"""
dt = T / M
a = [0.0] * (N_arm + 1)
b = [0.0] * (N_arm + 1)
w0 = 2 * math.pi / T
a[0] = (2.0 / T) * sum(f_ecg(k * dt) * dt for k in range(M))
for n in range(1, N_arm + 1):
sum_a = 0.0
sum_b = 0.0
for k in range(M):
t = k * dt
ft = f_ecg(t)
sum_a += ft * math.cos(n * w0 * t) * dt
sum_b += ft * math.sin(n * w0 * t) * dt
a[n] = (2.0 / T) * sum_a
b[n] = (2.0 / T) * sum_b
return a, b
def SerieFourier_ECG(nmax, M_samples=120):
"""Genera la senal ECG reconstruida y la envia al DAC"""
T = 1.0
a, b = calc_coeficientes(nmax, T, M=200)
w0 = 2 * math.pi / T
dt = T / M_samples
for k in range(M_samples):
t = k * dt
fx = a[0] / 2.0
for n in range(1, nmax + 1):
fx += a[n] * math.cos(n * w0 * t) + b[n] * math.sin(n * w0 * t)
print(fx)
val_dac = int((fx + 0.5) * 100)
val_dac = max(0, min(255, val_dac))
GPIO_SALP(val_dac, pines)
utime.sleep_us(500)
while True:
for narmonicos in range(1, 20, 1):
SerieFourier_ECG(narmonicos)