Subsections

Data types

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

Numbers are represented in a standard way, including complex numbers: 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.

Lists

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!

Tupels

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.

numpy Arrays/Matrices

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.

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

Creative Commons License