In [15]:
from numpy import random, zeros, arange, cos
from scipy import pi
from scipy.linalg import toeplitz, inv, pinv
from pylab import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show, subplot
N = 5
h = [0.2,1,-1,0.6,1]
# x = random.normal(0, 0.01, N)
x = cos(2*pi*0.01234*arange(N) + 2*pi*random.uniform(-1,1))
X = toeplitz(x, zeros(N)) # Need to in fill with zeros.
H = toeplitz(h, zeros(N)) # Need to in fill with zeros.
y = H @ x
y2 = X @ h
h_hat = pinv(X.transpose() @ X) @ X.transpose() @ y
h_hat2 = pinv(X.transpose() @ X) @ X.transpose() @ y2
h_hat3 = pinv(X) @ y
figure(1, figsize=(20, 6))
subplot(1, 3, 1)
plot(h)
title("True FIR filter")
subplot(1, 3, 2)
plot(y)
plot(y2,'r.')
title("$\mathbf{Xh}$ (red) and $\mathbf{Hx}$ (blue) of filter")
subplot(1, 3, 3)
plot(h)
plot(h_hat,'ro')
plot(h_hat2,'g.')
plot(h_hat3,'k+',markersize=10)
title("True (blue) and estimated (red) filter just pseduo +")
Out[15]:
In [ ]:
In [ ]: