I'd like to implement the Log(arithmic) AGC
from
But with
import numpy as np
import matplotlib.pyplot as plt
def signal():
cycles = 15
resolution = cycles * 20
length = 2 * np.pi * cycles
x = np.cos(np.arange(0, length, length / resolution))
cycles = 10
resolution = cycles * 20
length = 2 * np.pi * cycles
x = np.append(x, [2.5 * np.cos(np.arange(0, length, length / resolution))])
cycles = 15
resolution = cycles * 20
length = 2 * np.pi * cycles
x = np.append(x, [np.cos(np.arange(0, length, length / resolution))])
cycles = 20
resolution = cycles * 20
length = 2 * np.pi * cycles
x = np.append(x, [0.25 * np.cos(np.arange(0, length, length / resolution))])
return x
def logarithmic_agc_book(x, alpha, R):
g1 = 0
y = np.zeros((len(x),))
for n, xn in enumerate(x):
y[n] = xn * np.exp2(g1)
tmp = y[n] * y[n]
# tmp = LPF(tmp)
tmp = 2 * np.log2(R) - np.log2(tmp)
tmp = tmp * alpha
g = tmp + g1
g1 = g
return y
def logarithmic_agc_web(x, alpha, R):
g1 = 0
y = np.zeros((len(x),))
for n, xn in enumerate(x):
y[n] = xn * np.exp(g1)
tmp = np.abs(y[n])
tmp = np.log(R) - np.log(tmp)
tmp = tmp * alpha
g = tmp + g1
g1 = g
return y
x = signal()
fig1, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
fig1.tight_layout()
ax1.plot(x, label="Signal")
ax1.set_xlim([0, 1199])
ax1.set_ylim([-5, 5])
ax1.set_xlabel(f"Time (samples)")
ax1.legend()
ax2.plot(logarithmic_agc_book(x, 0.01, 1), label="Logarithmic AGC (book)")
ax2.plot(logarithmic_agc_web(x, 0.01, 1), label="Logarithmic AGC (web)")
ax2.set_xlim([0, 1199])
# ax2.set_ylim([-5, 5])
ax2.set_xlabel(f"Time (samples)")
ax2.legend()
plt.show()
I only get for both variants
which seems that both are not correct (e.g., peaks at ~50). Does somebody spot the problem in the implementations?