-
Notifications
You must be signed in to change notification settings - Fork 10
/
iqgen.py
202 lines (157 loc) · 7.24 KB
/
iqgen.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
#Copyright (c) 2017 James Gibbard
#Generates an IQ data file based on provided parameters
#Tested with python 2.7 and 3.6
#Requires numpy and matplotlib
import argparse
from sys import byteorder
import numpy as np
import matplotlib.pyplot as plt
def generateTone(fs, toneFreq, numSamples, amplitude):
#Generates a sinusoidal signal with the specified
#frequency and amplitude
step = (float(toneFreq) / float(fs)) * 2.0 * np.pi
phaseArray = np.array(range(0,numSamples)) * step
#Euler's Formular: e^(j*theta) = cos(theta) + j * sin(theta)
#For a complex sinusoidal theta = 2*pi*f*t where each time step is 1/fs
wave = np.exp(1.0j * phaseArray) * amplitude
return wave
def generateRandom(numSamples, amplitude):
#Generate random data from both I and Q
scale = int(amplitude)
realArray = np.random.randint(-scale, scale + 1, numSamples)
imagArray = np.random.randint(-scale, scale + 1, numSamples)
return realArray + 1j * imagArray
def generateImpulse(numSamples, amplitude):
#Generates an impulse signal
#Impulse is place in middle of array
wave = np.zeros(numSamples, 'complex')
wave[int(numSamples/2)] = amplitude + 1.0j * amplitude
return wave
def complexToSingleArray(array, orderIQ):
#Convert compex array to real array when
#I and Q are stored one after each other
#This is done because numpy can only write
#complex numbers as to a file in certain data types
realArray = np.real(array)
imagArray = np.imag(array)
#Create array double length of input to hold both real and imag values
output = np.zeros(realArray.size + imagArray.size)
#Pack real (I) and imag (Q) values in the correct order
#By default numpy writes index 0 to an output file first
if orderIQ:
output[0::2] = realArray
output[1::2] = imagArray
else:
output[1::2] = realArray
output[0::2] = imagArray
return output
if __name__ == '__main__':
#Generate command line parser to parse inputs
cliParser = argparse.ArgumentParser(description='Generates quadrature IQ samples')
#The number of samples is the only compulsory option
cliParser.add_argument('samples', type=int, help='number of output samples')
#These options are mutually exclusive, i.e. only one may be picked
outTypeGroup = cliParser.add_mutually_exclusive_group()
outTypeGroup.add_argument('-t', '--tone', type=float,
help='output sinusoidal tone [Hz]')
outTypeGroup.add_argument('-i', '--impulse', action='store_true',
help='generate impulse')
outTypeGroup.add_argument('-r', '--random', action='store_true',
help='generate random data')
#All these options are optional
cliParser.add_argument('-fs', '--sampleRate', type=float,
help='sets the sample rate [sps] (default=1e6)', default=1000000.0)
cliParser.add_argument('-o', '--filename', type=str, help='output filename')
cliParser.add_argument('-f', '--format', type=str,
help='Output format [int8 | int16 | int32 | float16 | float32 | float64] (default=int16)',
default='int16')
cliParser.add_argument('-a', '--amplitude', type=float,
help='amplitude the output')
cliParser.add_argument('-be', '--bigendian', action='store_true',
help='output data in big endian format (default=False)')
cliParser.add_argument('-qi', '--orderQI', action='store_true',
help='store output data as Q then I (Default = I then Q)')
cliParser.add_argument('-p', '--plot', action='store_true',
help='display a plot of the output')
args = cliParser.parse_args()
if args.tone == None and args.impulse == False and args.random == False:
print("Output type not specified. Assuming impulse output")
#Set the scale value
if args.amplitude != None:
amplitude = args.amplitude
else:
if args.format == 'int16':
amplitude = ((2.0**15) - 1)
elif args.format == 'int32':
amplitude = ((2.0**31) - 1)
elif args.format == 'int8':
amplitude = ((2.0**7) - 1)
elif args.format == 'float16' or args.format == 'float32' or args.format == 'float64':
amplitude = 1.0
else:
cliParser.error('Output format must be [int8 | int16 | int32 | float16 | float32 | float64]')
#Set the output type
if args.tone != None:
#If the tone frequency is set then output a tone
output = generateTone(args.sampleRate, args.tone, args.samples, amplitude)
filename = "tone_" + str(args.tone) + "_fs_" + str(args.sampleRate)
elif args.random:
#If random is set output random data in both I and Q
output = generateRandom(args.samples, amplitude)
filename = "random"
else:
#Otherwise output an impulse signal
output = generateImpulse(args.samples, amplitude)
filename = "impulse"
#Append whether I or Q comes first in the the filename
if args.orderQI:
filename += "_QI"
else:
filename += "_IQ"
#Set output data type format
if args.format == 'int16':
output = complexToSingleArray(output, not args.orderQI).astype(np.int16)
elif args.format == 'int32':
output = complexToSingleArray(output, not args.orderQI).astype(np.int32)
elif args.format == 'int8':
output = complexToSingleArray(output, not args.orderQI).astype(np.int8)
elif args.format == 'float16':
output = complexToSingleArray(output, not args.orderQI).astype(np.float16)
elif args.format == 'float32':
output = complexToSingleArray(output, not args.orderQI).astype(np.float32)
elif args.format == 'float64':
output = complexToSingleArray(output, not args.orderQI).astype(np.float64)
#Add the data format to the filename
filename += '_' + str(args.format)
#If system byteorder is different to desired output byte order
#Then swich the endianness
if byteorder == 'little':
if args.bigendian == True:
output = output.byteswap()
elif byteorder == 'big':
if args.bigendian == False:
output = output.byteswap()
#Add the endianness to the filename
if args.bigendian == False:
filename += "_LE"
else:
filename += "_BE"
filename += ".dat"
#If a filename is set in the arguments then use it
if args.filename != None:
filename = args.filename
#Open the output file and write the data to it
with open(filename, 'wb') as f:
output.tofile(f)
#If plotting is enabled then generate a plot
if args.plot:
if args.orderQI:
plt.plot(output[1::2], label='I')
plt.plot(output[0::2], label='Q')
else:
plt.plot(output[0::2], label='I')
plt.plot(output[1::2], label='Q')
plt.grid(True)
plt.legend(loc='upper right', frameon=True)
plt.show()