CSV files are common for data manipulation in Python, in cases where you extract the data from an excel sheet. Here is a short tutorial on how to extract some data out of a csv file, along with other nifty tricks along the way.
1) Easy Binary Conversion
You can use this to convert to binary.
your_binary_string = "0100000010001"
int_value = int(your_binary_string, 2)
Of course, you can extend this to octal, etc.
2) File reading and list comprehension
Suppose you have a whole csv of binary numbers. You need to read it out to python as a list.
Read the csv as string, and convert it to int easily.
Your csv data looks like this :
and you just want to get the values in bold. Meaning putting each of the the fourth and third column values in a tuple, converted to integer value.
import matplotlib.pyplot as plt
import numpy as np
filename = 'yourfile.csv'
with open(filename, 'rt') as csvfile:
reader = csv.reader(csvfile)
# the following line uses list comprehension to iterate through every row in the csv
# and creates row number of tuples in a list
# You can call next(reader) to skip a row, for example if the 1st row is just labels
iq = [(int(row, 2), int(row, 2)) for row in reader]
You get something like this:
[(4079, 63914), (4353, 61824), (3683, 62088), (3813, 61592) .... ]
At this point, if for some reason you want to get all the 1st values in each tuple in one list, and the 2nd values in each tuple in another list, you can do:
i, q = zip(*iq)
i = list(i)
q = list(q)
You will get for i :
[4079, 4353, 3683, 3813, 3231, 3201, 2750, 2550, 2201, 1874, 1559, .... ]
[63914, 61824, 62088, 61592, 61459, 61228, 60994, 60880, 60655, 60587... ]
3) Writing CSV files
With the previous i and q lists you extracted, you can now write it out as a csv.
with open('complex_out.csv', 'w') as csvfile:
fieldnames = ['i', 'q']
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
for row_num in range(len(i)):
Your csv file looks like this :
3) Converting data types
From the above data, I know that my number is really a 16bit binary representation.
Now, I am told that this is a 2’s complement representation.
So each tuple should be really (int16, int16), with +ve and -ve values possible. Fortunately Python allows us to do this easily.
iq = np.array(iq, np.int16) # creates an array with iq existing values, converted to int16
array([[ 4079, -1622],
[ 4353, -3712],
[ 3683, -3448],
[ -567, 5301],
[ -216, 5329],
[ 146, 5332]], dtype=int16)
Now we want to convert it to complex64, for further processing down the line.
sig = iq.astype(np.float32).view(np.complex64) # convert the values to float32, before viewing it as a complex64 (2 float32s)
[ 146.+5332.j]], dtype=complex64)
sig = sig.ravel() # flatten it to 1D
array([ 4079.-1622.j, 4353.-3712.j, 3683.-3448.j, ..., -567.+5301.j,
-216.+5329.j, 146.+5332.j], dtype=complex64)
Hooray, now we’re ready to do further processing on this data!