-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateWindowCoefs.py
executable file
·95 lines (77 loc) · 3.34 KB
/
generateWindowCoefs.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python
# generate coefficients from window function
# specify type of window
# write coefficients to file as unsigned integers
# command line Arguments:
# ----------------------------------------------------------------------------------------- #
# #
# -p file_path = destination directory of coef_file #
# -l window_length = length of window #
# -w window = window function type (string) #
# -n numPoints = number of data points in input #
# #
# ----------------------------------------------------------------------------------------- #
# --------------------------------Example Commandline-------------------------------------- #
# #
# ./generateWindowCoefs.py -l 35 -w 'boxcar' -n 4096 -p ~/project_dir #
# #
# --------------------------------Example Commandline-------------------------------------- #
import sys
import itertools
import argparse
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.mlab as mylab
import csv
import Verilog_VCD
from scipy import signal
from scipy.optimize import curve_fit
import FixedPoint as fp
import re as regex
def generate_window_coefs(window,window_length,file_path,numPoints):
print '\nGenerating {} window of length {}...'.format(window,window_length)
# specify total bits and fractional bits for fixed point input:
n_bits = 16
n_frac_bits = 15
if (window=='boxcar'):
coefs=signal.boxcar(window_length)
elif (window=='hamming'):
coefs=signal.hamming(window_length)
elif (window=='hann'):
coefs=signal.hann(window_length)
elif (window=='blackman'):
coefs=signal.blackman(window_length)
else:
coefs=signal.boxcar(window_length)
#Write out hex file for VHDL
intData=np.zeros(numPoints)
nintData=np.uint16(coefs*(2**n_bits-1))
paddingFrac=4
if (window_length==numPoints):
intData=coefs
else:
for i in range(len(coefs)):
intData[int(numPoints/paddingFrac)+i]=coefs[i]
intData = [ID*(2**n_bits-1) for ID in intData]
intData=np.hstack([intData,intData])
with open(str(file_path)+'/fpgaCoefData'+str(numPoints)+'_'+str(window)+'.txt','w') as FID:
FID.write('\n'.join(['{}'.format(int(x)) for x in intData]))
with open(str(file_path)+'/macCoefData'+str(numPoints)+'_'+str(window)+'.txt','w') as FID:
FID.write('signal myCoef : input_array32 :=(')
FID.write(','.join(['x"{0:08X}"'.format(int(x)) for x in intData])+');')
def main(argv):
parser=argparse.ArgumentParser(description='Manage VHDL Verification and Plotting')
# commandline args, for usage type '-h'
parser.add_argument('-w','--window',dest='window',type=str,help='Type of window function (boxcar,hamming,hann,blackman)\ndefault=boxcar')
parser.add_argument('-l','--win_length',dest='winLen',type=int,help='Length of window function')
parser.add_argument('-n','--numPoints',dest='numPoints',type=int,help='Number of points')
parser.add_argument('-p','--file_path',dest='fpath',type=str,help='Project file path')
args = parser.parse_args()
# function call in main:
generate_window_coefs(args.window,args.winLen,args.fpath,args.numPoints)
# Display inputs:
print 'Window chosen for signal of {} points'.format(args.numPoints)
print 'Writing results to {} directory...\n'.format(str(args.fpath))
if __name__ == "__main__":
main(sys.argv[1:])