-
Notifications
You must be signed in to change notification settings - Fork 4
/
SLMS.py
48 lines (39 loc) · 1.25 KB
/
SLMS.py
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
"""
Sliding-analytical least-mean-squares (SLMS) algorithm for signal recovery.
This is the ALMS with a sliding window carried out iteratively.
"""
from __future__ import division
import numpy as np
import numpy.linalg as npl
from scipy.linalg import toeplitz
from scipy.io import loadmat
from scipy.io.wavfile import write as wavwrite
# Function for forming filter regression matrix given an array (data) and order (m)
regmat = lambda data, m: toeplitz(data, np.concatenate(([data[0]], np.zeros(m-1))))[m-1:]
# Unpack data
data = loadmat('audio_data.mat')
noisy_speech = data['reference'].flatten()
noise = data['primary'].flatten()
fs = data['fs'].flatten()[0] # Hz
# Filter order
m = 100
# Window size must be larger than filter order
s = 10*m
assert s > m
# Prep
X = regmat(noise, m)
w_list = []
speech = []
# Slide window by window through the input data
# and find the analytically optimal weights for
# the output data, keeping the speech signal
# as the error for each sample in that window
for i in np.arange(0, len(noise)-s, s):
x = X[i:(i+s)]
y = noisy_speech[(i+m-1):(i+m-1+s)]
w = npl.pinv(x).dot(y)
e = y - x.dot(w)
w_list.append(w)
speech.extend(e)
# Save results
wavwrite('recovered_SLMS.wav'.format(m, s), fs, np.array(speech))