Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. from __future__ import division
  2. import numpy as np
  3.  
  4. # This is the inverse of covariance matrix of hann windowed gaussian noise (times 4.0) */
  5. R_inv= [
  6. [ 336, 504, 540, 480, 360, 216, 84 ],
  7. [ 504, 1071, 1260, 1170, 900, 549, 216 ],
  8. [ 540, 1260, 1800, 1800, 1440, 900, 360 ],
  9. [ 480, 1170, 1800, 2100, 1800, 1170, 480 ],
  10. [ 360, 900, 1440, 1800, 1800, 1260, 540 ],
  11. [ 216, 549, 900, 1170, 1260, 1071, 504 ],
  12. [ 84, 216, 360, 480, 540, 504, 336 ]
  13. ]
  14.  
  15. N = 255
  16. filter7_table = np.empty((2*N + 1, 7))
  17.  
  18. def hann_response(delta):
  19. if (abs(delta - 1.0) < 0.5):
  20. return np.sinc(delta - 1.0) / (delta*(1 + delta))
  21. if (abs(delta + 1.0) < 0.5):
  22. return np.sinc(delta + 1.0) / (delta*(delta - 1))
  23. if abs(delta) < 0.5:
  24. return np.sinc(delta) / (1 - delta*delta)
  25.  
  26. return np.sin(np.pi*delta) / (np.pi * delta * (1 - delta*delta))
  27.  
  28. def tabulate_hann_filter7():
  29. for m in range(2*N + 1):
  30.  
  31. # We're in a particular row of a N x 7 matrix.
  32. # So we're populating the columns of the m-th row of the matrix filter7_table.
  33.  
  34. # mismatch is basically dividing -1 to 1 into N (256) pieces.
  35. mismatch = (m - N) / (1.0*N)
  36. c = np.empty(7)
  37.  
  38. # For mismatch = -1.0, argument of hann_response is { +2.0, +1.0, 0.0, -1.0, -2.0, -3.0, -4.0 }.
  39. # For mismatch = -0.5, argument of hann_response is { +2.5, +1.5, +0.5, -0.5, -1.5, -2.5, -3.5 }.
  40. # For mismatch = 0.0, argument of hann_response is { +3.0, +2.0, +1.0, 0.0, -1.0, -2.0, -3.0 }.
  41. # For mismatch = +0.5, argument of hann_response is { +3.5, +2.5, +1.5, +0.5, -0.5, -1.5, -2.5 }.
  42. # For mismatch = +1.0, argument of hann_response is { +4.0, +3.0, +2.0, +1.0, 0.0, -1.0, -2.0 }.
  43.  
  44. # So for any mismatch, the arguments of hann response are 7 numbers, uniformly distributed between mismatch + 3 and mismatch - 3.
  45. # Which is probably just a way of sampling 7 bins around the mismatch + 0 (= mismatch) bin.
  46.  
  47. for i in range(7):
  48. c[i] = hann_response(mismatch - (i - 3))
  49.  
  50. # This basically means that it's -ve for all even i (and i=0) and +ve for all odd i.
  51. if((i+3) & 1):
  52. c[i] = -c[i]
  53.  
  54. norm = 0
  55. row_norm = 0
  56. for i in range(7):
  57. filter7_table[m][i] = 0
  58. for j in range(7):
  59.  
  60. # This is just a matched filter.
  61.  
  62. filter7_table[m][i] += R_inv[i][j]*c[j]
  63.  
  64. norm += filter7_table[m][i] * c[i]
  65. # row_norm += filter7_table[m][i]
  66.  
  67.  
  68. # Not sure if the following should be there.
  69. norm = 1.0/norm
  70. # norm = np.sqrt(norm)
  71.  
  72. # row_norm = 1.0 / row_norm
  73.  
  74. for i in range(7):
  75. filter7_table[m][i] *= norm
  76. # filter7_table[m][i] *= row_norm
  77.  
  78.  
  79. def tabulated_fill_hann_filter7(mismatch):
  80.  
  81. # Divide the bins into N numbers and find a k between 0 and N (255).
  82. # When mismatch = -1, k = 0.
  83. # When mismatch = 1, k = 2N.
  84.  
  85. k = int(round((mismatch + 1) * N))
  86. return filter7_table[k]
  87.  
  88. def get_filter(bin_shift):
  89. mismatch = bin_shift - round(bin_shift)
  90. return tabulated_fill_hann_filter7(mismatch)
  91.  
  92. if __name__ == "__main__":
  93. # gaussian_noise()
  94. # window()
  95. tabulate_hann_filter7()
  96.  
  97.  
  98. # print filter7_table[0]
  99. # print filter7_table[1]
  100. # print filter7_table[2]
  101. # print filter7_table[3]
  102.  
  103. print
  104. print
  105.  
  106. print "Filter is: "
  107. print get_filter(bin_shift = -65.25), np.sum(get_filter(bin_shift = -65.25))
  108. print get_filter(bin_shift = -65.05), np.sum(get_filter(bin_shift = -65.05))
  109. print get_filter(bin_shift = -17.05), np.sum(get_filter(bin_shift = -17.05))
  110. print get_filter(bin_shift = 2.05), np.sum(get_filter(bin_shift = 2.05))
  111. print get_filter(bin_shift = 2.15), np.sum(get_filter(bin_shift = 2.15))
  112. print get_filter(bin_shift = 2.07), np.sum(get_filter(bin_shift = 2.07))
  113.  
  114. # for i in range(255):
  115. # print i, " => ",
  116. # for j in range(7):
  117. # print filter7_table[i][j],
  118. # print "\n"
  119.  
  120.  
  121. x = get_filter(bin_shift=-15.1775)
  122. print sum([y**2 for y in x])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement