Ejemplo n.º 1
0
    def test_filt_w_output(self):
        """FastBlockLMSFilter.filt behaves like lfilter when w is taken from filt."""
        length = 16
        blocks = 16
        blocklength = length
        w = np.random.normal(size=length)
        xs = np.random.normal(size=(blocks, blocklength))
        x = np.concatenate(xs)

        filt = FastBlockLMSFilter(length, length, initial_coeff=w)

        y = []
        for xb in xs:
            y.append(filt.filt(xb))
        y = np.concatenate(y)
        yref = lfilter(filt.w, 1, x)
        npt.assert_almost_equal(y, yref)
Ejemplo n.º 2
0
    def test_w(self):
        """Finds optimal filter."""
        length = 8
        blocks = 128
        blocklength = length
        w = np.random.normal(size=length)
        xs = np.random.normal(size=blocks * blocklength + blocklength)
        y_desired = lfilter(w, 1, xs)[blocklength:]
        xs = xs[blocklength:]

        filt = FastBlockLMSFilter(length, blocklength, stepsize=1)

        for x, yd in zip(xs.reshape(blocks, blocklength),
                         y_desired.reshape(blocks, blocklength)):
            y = filt.filt(x)
            e = yd - y
            filt.adapt(x, e)

        npt.assert_almost_equal(w, filt.w)
Ejemplo n.º 3
0
    def test_single_same_as_multi_filt_normalized(self):
        length = blocks = 128
        blocklength = 32
        w = np.random.normal(size=(length, 1, 1))
        # w = np.zeros((length, 1, 1))

        filtsc = FastBlockLMSFilter(
            length=length,
            blocklength=blocklength,
            initial_coeff=w[:, 0, 0],
            stepsize=0.01,
            constrained=True,
            normalized=True,
        )
        filtmc = MultiChannelBlockLMS(
            length=length,
            blocklength=blocklength,
            initial_coeff=w,
            stepsize=0.01,
            constrained=True,
            normalized="elementwise",
        )

        xs = np.random.normal(size=(blocks, blocklength, 1))

        ysc = []
        ymc = []
        for xb in xs:
            # adapting kills this
            filtsc.adapt(xb[:, 0], xb[:, 0] * 2)
            filtmc.adapt(xb[:, None, None], xb * 2)

            ysc.append(filtsc.filt(xb[:, 0]))
            ymc.append(filtmc.filt(xb))
            npt.assert_almost_equal(ysc[-1], ymc[-1][:, 0])

        npt.assert_almost_equal(np.concatenate(ysc), np.concatenate(ymc)[:, 0])
        npt.assert_almost_equal(filtsc.w, filtmc.w[:, 0, 0])
        npt.assert_almost_equal(filtsc.W, filtmc.W[:, 0, 0])
Ejemplo n.º 4
0
from adafilt import FastBlockLMSFilter
from adafilt.io import FakeInterface

length = 512  # number of adaptive FIR filter taps
blocklength = 128  # length of I/O buffer and blocksize of filter
n_buffers = 150  # size of simulation

# plant
h = np.zeros(512)
h[255] = 1

# white noise signal
signal = np.random.normal(0, 1, size=n_buffers * blocklength)

# the adaptive filter
filt = FastBlockLMSFilter(length, blocklength, power_averaging=0.9)

# simulates an audio interface with primary and secondary paths and 40 dB SNR noise
# at the error sensor
sim = FakeInterface(blocklength, h_sec=h)

# aggregate signals during simulation
elog = []
felog = []
wslog = []
ylog = []

for i in range(n_buffers):

    # identification noise
    u = np.random.normal(0, 1, blocklength)
Ejemplo n.º 5
0
from adafilt.io import FakeInterface
from adafilt.utils import wgn

length = 8  # number of adaptive FIR filter taps
blocklength = 2  # length of I/O buffer and blocksize of filter
n_buffers = 150  # size of simulation

# primary and secondary paths
h_pri = [0, 0, 0, 0, 0, 0, 0, 0.5]
h_sec = [0, 0, 0, 1, 0, 0, 0, 0]

# white noise signal
signal = np.random.normal(0, 1, size=n_buffers * blocklength)

# the adaptive filter
filt = FastBlockLMSFilter(length, blocklength, stepsize=0.1, leakage=0.9999)

# secondary path estimate has to account for block size
plant_model = FIRFilter(np.concatenate((np.zeros(blocklength), h_sec)))

# simulates an audio interface with primary and secondary paths and 40 dB SNR noise
# at the error sensor
sim = FakeInterface(
    blocklength,
    signal,
    h_pri=h_pri,
    h_sec=h_sec,
    noise=wgn(olafilt(h_pri, signal), 40, "dB"),
)

elog = []
Ejemplo n.º 6
0
def adaptive_cancel(audio_file, noise_file, M):

    length = M  # number of adaptive FIR filter taps
    blocklength = M  # length of I/O buffer and blocksize of filter

    audio_signal, fs = read_audio(audio_file)

    noise_signal, fs = read_audio(noise_file)

    number_of_blocks = len(audio_signal) // M

    error = np.zeros(number_of_blocks * M)

    padd_audio = audio_signal[:number_of_blocks * M]
    padd_noise = noise_signal[:number_of_blocks * M]
    audio_blocks = np.array_split(padd_audio, number_of_blocks)
    noise_blocks = np.array_split(padd_noise, number_of_blocks)

    filt = FastBlockLMSFilter(length,
                              blocklength,
                              stepsize=0.003,
                              leakage=0.9999,
                              constrained=False,
                              normalized=True)

    elog = []
    felog = []
    wslog = []
    ylog = []
    dlog = []

    for i in range(number_of_blocks):

        # filter prediction
        y = filt.filt(noise_blocks[i])

        # error signal
        e = y - audio_blocks[i]
        filt.adapt(noise_blocks[i], e)

        elog.append(e)
        dlog.append(audio_blocks[i])
        ylog.append(y)
        wslog.append(filt.w)

    y_out = np.concatenate(ylog)
    d_out = np.concatenate(dlog)
    e_out = np.concatenate(elog)
    # plt.plot(padd_noise,label="Signal_with_noise")
    # plt.plot(y_out,label="Noise_Estimation")
    # plt.plot(e_out,label="Output")
    # #plt.plot(new_auxd+1.5,label="Deseada con offset")
    # plt.legend(loc='lower left')
    # plt.grid(True)
    # plt.xlabel('Sample number')
    # plt.ylabel('Amplitude')
    # plt.show()

    # plt.title("Filter Coefficient")
    # plt.plot(np.array(wslog))
    # plt.xlabel("Block Number")
    # plt.ylabel("Coefficient Magnitude")
    # plt.grid(True)
    # plt.show()

    wavio.write("salida_filtrada_LMS.wav", e_out, fs, sampwidth=3)

    return audio_signal, e_out, fs, wslog
Ejemplo n.º 7
0
blocklength = 128  # length of I/O buffer and blocksize of filter
n_buffers = 150  # size of simulation

# primary and secondary paths
h_pri = np.zeros(1024)
h_pri[-1] = 1
h_sec = np.zeros(512)
h_sec[-1] = 1

# white noise signal
signal = np.random.normal(0, 1, size=n_buffers * blocklength)

# the adaptive filter
filt = FastBlockLMSFilter(length,
                          blocklength,
                          stepsize=0.1,
                          leakage=1,
                          power_averaging=0.9)

# simulates an audio interface with primary and secondary paths and 40 dB SNR noise
# at the error sensor
sim = FakeInterface(
    blocklength,
    signal,
    h_pri=h_pri,
    h_sec=h_sec,
    noise=wgn(olafilt(h_pri, signal), 20, "dB"),
)

# secondary path estimate has to account for block size
plant_model = FIRFilter(np.concatenate((np.zeros(blocklength), h_sec)))
Ejemplo n.º 8
0
length = 512  # number of adaptive FIR filter taps
blocklength = 128  # length of I/O buffer and blocksize of filter
n_buffers = 150  # size of simulation

# primary and secondary paths
h_pri = np.zeros(1024)
h_pri[-1] = 1
h_sec = np.zeros(512)
h_sec[-1] = 1

# white noise signal
signal = np.random.normal(0, 1, size=n_buffers * blocklength)

# the adaptive filter
filt = FastBlockLMSFilter(length, blocklength)

# the dummy adaptive filter
filt_dummy = FastBlockLMSFilter(length,
                                blocklength,
                                stepsize=0.1,
                                leakage=0.99999,
                                power_averaging=0.9)

# secondary path estimate has to account for block size
plant_model_fx = SimpleFilter(np.concatenate((np.zeros(blocklength), h_sec)))
plant_model_fy = SimpleFilter(np.concatenate((np.zeros(blocklength), h_sec)))

# simulates an audio interface with primary and secondary paths and 40 dB SNR noise
# at the error sensor
sim = FakeInterface(
Ejemplo n.º 9
0
# primary and secondary paths
h_pri = np.zeros(64)
h_pri[60] = 1
h_sec = np.zeros(64)
h_sec[20] = 1

# simulates an audio interface with primary and secondary paths and 40 dB SNR noise
# at the error sensor
signal = np.random.normal(0, 1, size=n_buffers * blocklength)
sim = FakeInterface(
    blocklength, signal, h_pri=h_pri, h_sec=h_sec, noise=wgn(signal, 20, "dB")
)

# the adaptive filter
filt = FastBlockLMSFilter(
    length, blocklength, stepsize=0.01, leakage=0.99999, power_averaging=0.9
)
filt.locked = True

# secondary path estimate has to account for block size
plant_model = FIRFilter(np.zeros(blocklength + length))

# adaptive plant model
adaptive_plant_model = FastBlockLMSFilter(
    length, blocklength, stepsize=0.01, leakage=0.99999
)

# aggregate signals during simulation
elog = []
e_plog = []
wlog = []