whoops, no ability to add comments in between functions in pseudocode
[openpower-isa.git] / src / openpower / decoder / isa / naivedct.py
1 #
2 # Fast discrete cosine transform algorithms (Python)
3 #
4 # Copyright (c) 2020 Project Nayuki. (MIT License)
5 # https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy of
8 # this software and associated documentation files (the "Software"), to deal in
9 # the Software without restriction, including without limitation the rights to
10 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 # the Software, and to permit persons to whom the Software is furnished to do so,
12 # subject to the following conditions:
13 # - The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 # - The Software is provided "as is", without warranty of any kind, express or
16 # implied, including but not limited to the warranties of merchantability,
17 # fitness for a particular purpose and noninfringement. In no event shall the
18 # authors or copyright holders be liable for any claim, damages or other
19 # liability, whether in an action of contract, tort or otherwise, arising from,
20 # out of or in connection with the Software or the use or other dealings in the
21 # Software.
22 #
23
24 import math
25
26
27 # DCT type II, unscaled.
28 # See: https://en.wikipedia.org/wiki/Discrete_cosine_transform#DCT-II
29 def transform(vector):
30 result = []
31 factor = math.pi / len(vector)
32 for i in range(len(vector)):
33 sum = 0.0
34 for (j, val) in enumerate(vector):
35 sum += val * math.cos((j + 0.5) * i * factor)
36 result.append(sum)
37 return result
38
39
40 # DCT type III, unscaled.
41 # See: https://en.wikipedia.org/wiki/Discrete_cosine_transform#DCT-III
42 def inverse_transform(vector):
43 result = []
44 factor = math.pi / len(vector)
45 for i in range(len(vector)):
46 sum = vector[0] / 2
47 for j in range(1, len(vector)):
48 sum += vector[j] * math.cos(j * (i + 0.5) * factor)
49 result.append(sum)
50 return result