Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- import numpy as np
- # This is the inverse of covariance matrix of hann windowed gaussian noise (times 4.0) */
- R_inv= [
- [ 336, 504, 540, 480, 360, 216, 84 ],
- [ 504, 1071, 1260, 1170, 900, 549, 216 ],
- [ 540, 1260, 1800, 1800, 1440, 900, 360 ],
- [ 480, 1170, 1800, 2100, 1800, 1170, 480 ],
- [ 360, 900, 1440, 1800, 1800, 1260, 540 ],
- [ 216, 549, 900, 1170, 1260, 1071, 504 ],
- [ 84, 216, 360, 480, 540, 504, 336 ]
- ]
- N = 255
- filter7_table = np.empty((2*N + 1, 7))
- def hann_response(delta):
- if (abs(delta - 1.0) < 0.5):
- return np.sinc(delta - 1.0) / (delta*(1 + delta))
- if (abs(delta + 1.0) < 0.5):
- return np.sinc(delta + 1.0) / (delta*(delta - 1))
- if abs(delta) < 0.5:
- return np.sinc(delta) / (1 - delta*delta)
- return np.sin(np.pi*delta) / (np.pi * delta * (1 - delta*delta))
- def tabulate_hann_filter7():
- for m in range(2*N + 1):
- # We're in a particular row of a N x 7 matrix.
- # So we're populating the columns of the m-th row of the matrix filter7_table.
- # mismatch is basically dividing -1 to 1 into N (256) pieces.
- mismatch = (m - N) / (1.0*N)
- c = np.empty(7)
- # For mismatch = -1.0, argument of hann_response is { +2.0, +1.0, 0.0, -1.0, -2.0, -3.0, -4.0 }.
- # For mismatch = -0.5, argument of hann_response is { +2.5, +1.5, +0.5, -0.5, -1.5, -2.5, -3.5 }.
- # For mismatch = 0.0, argument of hann_response is { +3.0, +2.0, +1.0, 0.0, -1.0, -2.0, -3.0 }.
- # For mismatch = +0.5, argument of hann_response is { +3.5, +2.5, +1.5, +0.5, -0.5, -1.5, -2.5 }.
- # For mismatch = +1.0, argument of hann_response is { +4.0, +3.0, +2.0, +1.0, 0.0, -1.0, -2.0 }.
- # So for any mismatch, the arguments of hann response are 7 numbers, uniformly distributed between mismatch + 3 and mismatch - 3.
- # Which is probably just a way of sampling 7 bins around the mismatch + 0 (= mismatch) bin.
- for i in range(7):
- c[i] = hann_response(mismatch - (i - 3))
- # This basically means that it's -ve for all even i (and i=0) and +ve for all odd i.
- if((i+3) & 1):
- c[i] = -c[i]
- norm = 0
- row_norm = 0
- for i in range(7):
- filter7_table[m][i] = 0
- for j in range(7):
- # This is just a matched filter.
- filter7_table[m][i] += R_inv[i][j]*c[j]
- norm += filter7_table[m][i] * c[i]
- # row_norm += filter7_table[m][i]
- # Not sure if the following should be there.
- norm = 1.0/norm
- # norm = np.sqrt(norm)
- # row_norm = 1.0 / row_norm
- for i in range(7):
- filter7_table[m][i] *= norm
- # filter7_table[m][i] *= row_norm
- def tabulated_fill_hann_filter7(mismatch):
- # Divide the bins into N numbers and find a k between 0 and N (255).
- # When mismatch = -1, k = 0.
- # When mismatch = 1, k = 2N.
- k = int(round((mismatch + 1) * N))
- return filter7_table[k]
- def get_filter(bin_shift):
- mismatch = bin_shift - round(bin_shift)
- return tabulated_fill_hann_filter7(mismatch)
- if __name__ == "__main__":
- # gaussian_noise()
- # window()
- tabulate_hann_filter7()
- # print filter7_table[0]
- # print filter7_table[1]
- # print filter7_table[2]
- # print filter7_table[3]
- print
- print
- print "Filter is: "
- print get_filter(bin_shift = -65.25), np.sum(get_filter(bin_shift = -65.25))
- print get_filter(bin_shift = -65.05), np.sum(get_filter(bin_shift = -65.05))
- print get_filter(bin_shift = -17.05), np.sum(get_filter(bin_shift = -17.05))
- print get_filter(bin_shift = 2.05), np.sum(get_filter(bin_shift = 2.05))
- print get_filter(bin_shift = 2.15), np.sum(get_filter(bin_shift = 2.15))
- print get_filter(bin_shift = 2.07), np.sum(get_filter(bin_shift = 2.07))
- # for i in range(255):
- # print i, " => ",
- # for j in range(7):
- # print filter7_table[i][j],
- # print "\n"
- x = get_filter(bin_shift=-15.1775)
- print sum([y**2 for y in x])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement