Advertisement
Guest User

Untitled

a guest
May 25th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import pdb
  6.  
  7. def lms(u, d, M, step, leak=0, initCoeffs=None, N=None, returnCoeffs=False):
  8.  
  9. if N is None:
  10. N = len(u)-M+1
  11.  
  12. initCoeffs = np.zeros(M)
  13.  
  14. # Initialization
  15. y = np.zeros(N) # Filter output
  16. e = np.zeros(N) # Error signal
  17. w = initCoeffs # Initialise equaliser
  18. leakstep = (1 - step*leak)
  19. if returnCoeffs:
  20. W = np.zeros((N, M)) # Matrix to hold coeffs for each equaliser step
  21.  
  22. # Equalise
  23. for n in xrange(N):
  24.  
  25. x = np.flipud(u[n:n+M]) #
  26.  
  27. y[n] = np.dot(x, w)
  28.  
  29. e[n] = d[n+M-1] - y[n]
  30.  
  31. w = leakstep * w + step * x * e[n]
  32.  
  33. y[n] = np.dot(x, w)
  34.  
  35. if returnCoeffs:
  36. W[n] = w
  37.  
  38. if returnCoeffs:
  39. w = W
  40.  
  41. return y, e, w
  42.  
  43.  
  44.  
  45. np.random.seed(1337)
  46. ulen = 2000
  47. coeff = np.concatenate(([1], np.zeros(10), [-0.9], np.zeros(7), [0.1]))
  48. u = np.random.randn(ulen)
  49. d = np.convolve(u, coeff)
  50. M = 20 # No. of taps
  51. step = 0.003 # Step size
  52. y, e, w = lms(u, d, M, step)
  53. print np.allclose(w, coeff)
  54.  
  55.  
  56.  
  57. plt.figure()
  58. plt.subplot(1,1,1)
  59. plt.plot(np.abs(e[0:400]))
  60.  
  61. plt.show()
  62.  
  63. #pdb.set_trace()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement