Subsections
Python has all the standard data types such as int, float, string,
...and a few more. You can check which type a variable has with:
“type(variable)”. Important for DSP are these types:
Numbers are represented in a standard way, including complex numbers:
- 6E23
- 6+2j is a complex number which is just generated
by adding a “j” to the imaginary part.
- complex(0,1) creates 1j and is useful if you have
variables which you want to combine into a complex number.
Note that python has implicit types which might make you trip
over. If you write “a=1” then “a” is an integer whereas
“a=1.0” is a floating point number. You can force a certain type by using
the typecast operator, for example “ a = int(b)” to create an integer.
There are numerous data types in Python which allow storing
collections of data. For DSP we need: lists, tupels and arrays. To clear
up the confusion from the start we are going through them one
by one. First up: lists!
- p = [1,9,77] is a list with the components 1,2 and 77.
- p[2] returns the third element of the list (the index
counts from 0). Note the index needs to be an integer so you might
need to cast a floating point variables first into an integer, for example
“p[int(a)]”.
- p[1:5] extracts from the list all elements from
index number 1 to index number 4 (!). The index 5 is excluded!
This is called splicing. Check out the online docs
for more examples.
- p = range(1,4) generates which is the same as
a “for loop” in C: for(int i=1;i<4;i++)
- p.append(10) adds a new element to the end of the
list. You see that lists are essentially classes with methods (see below)!
Check out the documentation for more options to manipulate lists.
- len(p2) provides the number of elements.
Tupels are similar to lists but they are rather used as
“containers” to package up different variables into one variable. For
example, you might want to put the sampling rate and the resolution of
an ADC into a tuple as they belong together.
- p = (1,9,'kk') is a tupel with the two integer numbers 1,2 and 'kk'.
- The round brackets are often omitted:
p = 1,9,'kk' which implicitly defines a tupel.
- p[1] returns the 2nd element of the tupel (the index
counts from 0).
- Extracting a tupel into separate variables also works:
a,b,c = p assigns 1 to a, 9 to b and 'kk' to c. This
is very convenient for functions if you want to return more than
one result. Just package them up into a tuple and return it.
Numpy arrays or matrices are used for number crunching and are ideal for
DSP. Use these for all of you DSP data manipulation.
They are essentially C arrays with a thin wrapper around
them. In order to create and manipulate these arrays you need to use the numpy functions.
- a = np.array([1, 2, 5]) which creates a numpy
array from a standard Python list containing 1,2,5.
- b = np.array([[6, 2], [9, 7]]) creates a two dimensional
array.
- There are a lot of convenience functions in numpy to create
arrays just containing zeros, ones or ascending numbers etc. Check out
the numpy docs.
Since numpy arrays are close relatives to C arrays they have an additional
field which is called “dtype” which determines the type of the
array elements, for example “int16” (a 16 bit wide integer).
>>> data
array([-12288, -12545, -12798, ..., 511, 513, 513], dtype=int16)
>>> data.dtype
dtype('int16')
>>> data.astype(float)
array([-12288., -12545., -12798., ..., 511., 513., 513.])
where the member function “astype” can convert from one array type to another, here
from int16 to floating point.
github / contact