1import cmsisdsp.fixedpoint as f
2import numpy as np
3
4#: F64 format
5F64 = 64
6#: F32 format
7F32 = 32
8#: F16 format
9F16 = 16
10#: Q31 fixed point format
11Q31 = 31
12#: Q15 fixed point format
13Q15 = 15
14#: Q7 fixed point format
15Q7 = 7
16
17class UnknownCMSISDSPDataType(Exception):
18    pass
19
20def convert(samples,format):
21    """
22     Return an array of scalars in a given format converted from an array of doubles.
23     It is typically used to convert a reference table in double to a table with a lower
24     accuracy used for a specific implementation of an algorithm.
25
26     :param samples: array of double.
27     :type samples: array
28     :param format: Format identification (F64,F32,F16,Q31,Q15,Q7).
29     :type format: int
30     :return: array of scalars in chosen format.
31     :rtype: array
32
33     """
34    if format==Q31:
35       return(f.toQ31(np.array(samples)))
36    if format==Q15:
37       return(f.toQ15(np.array(samples)))
38    if format==Q7:
39       return(f.toQ7(np.array(samples)))
40    if format==F64:
41        return(np.array(samples).astype(dtype=np.float64))
42    if format==F32:
43        return(np.array(samples).astype(dtype=np.float32))
44    if format==F16:
45        return(np.array(samples).astype(dtype=np.float16))
46    raise UnknownCMSISDSPDataType