index_transform_decimation_in_frequency.py
56 lines
| 1.7 KiB
| text/x-python
|
PythonLexer
|
r2 | class IndexTransformDecimationInFrequency(object): | ||
"""docstring for IndexTransformDecimationInFrequency""" | ||||
def __init__(self, N): | ||||
super(IndexTransformDecimationInFrequency, self).__init__() | ||||
self.N = N | ||||
self.new_k = range(self.N) | ||||
self.build_index_vector() | ||||
print 'new_k = ', self.new_k | ||||
def reverse_order(self, sequence): | ||||
|
r3 | old_sequence = sequence[:] | ||
for i in (range(len(sequence))): | ||||
|
r2 | a = sequence[ i ]; | ||
|
r3 | sequence[ i ] = old_sequence[ self.new_k[i] ] | ||
|
r2 | |||
def build_index_vector(self): | ||||
|
r3 | if self.N == 8: # 2^3 => 3 bits | ||
|
r2 | for indice in range(self.N): | ||
b = '{:03b}'.format(indice) | ||||
b = b[::-1] | ||||
self.new_k[indice] = int(b, 2) | ||||
|
r3 | elif self.N == 16: # 2^4 => 4 bits | ||
for indice in range(self.N): | ||||
b = '{:04b}'.format(indice) | ||||
b = b[::-1] | ||||
self.new_k[indice] = int(b, 2) | ||||
elif self.N == 32: # 2^5 => 5 bits | ||||
for indice in range(self.N): | ||||
b = '{:05b}'.format(indice) | ||||
b = b[::-1] | ||||
self.new_k[indice] = int(b, 2) | ||||
elif self.N == 256: # 2^8 => 8 bits | ||||
for indice in range(self.N): | ||||
b = '{:08b}'.format(indice) | ||||
b = b[::-1] | ||||
self.new_k[indice] = int(b, 2) | ||||
|
r2 | else: | ||
|
r3 | print'error, N = ', self.N, ' is not supported by the index_transform function' | ||
|
r2 | |||
if __name__ == "__main__": | ||||
x = range(8) | ||||
|
r3 | indexTransformDecimationInFrequency = IndexTransformDecimationInFrequency(len(x)) | ||
|
r2 | print 'x ', x | ||
indexTransformDecimationInFrequency.reverse_order(x) | ||||
print 'x rearranged ', x | ||||
y = x | ||||
indexTransformDecimationInFrequency.reverse_order(y) | ||||
print 'y ', y | ||||
|
r3 | x = range(16) | ||
indexTransformDecimationInFrequency = IndexTransformDecimationInFrequency(len(x)) | ||||
print 'x ', x | ||||
indexTransformDecimationInFrequency.reverse_order(x) | ||||
print 'x rearranged ', x | ||||
y = x | ||||
indexTransformDecimationInFrequency.reverse_order(y) | ||||
print 'y ', y | ||||