Created
April 15, 2018 01:33
-
-
Save yonghanjung/0eb1f51eeb24804866ad09b32c5ba15f to your computer and use it in GitHub Desktop.
CS590 HW3 Prob 2-a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def binarize(num): | |
binnum = bin(num)[2:] | |
if len(binnum) < 3: | |
rem_num = 3 - len(binnum) | |
return '0'*rem_num + binnum | |
else: | |
return binnum | |
def f(binnum): | |
count_zero = 0 | |
count_one = 0 | |
for elem in binnum: | |
if elem == '0': | |
count_zero += 1 | |
else: | |
count_one += 1 | |
if count_one > count_zero: | |
return -1 | |
else: | |
return 1 | |
def Fourier_basis(bin_s, bin_x): | |
lincom = 0 | |
for elem_s, elem_x in zip(bin_s, bin_x): | |
lincom += int(elem_s) * int(elem_x) | |
return (-1)**(lincom) | |
def Fourier_Matrix(bindom): | |
FM = np.zeros((len(bindom),len(bindom))) | |
N = len(bindom) | |
for s in range(len(bindom)): | |
for x in range(len(bindom)): | |
bin_s = bindom[s] | |
bin_x = bindom[x] | |
FM[x,s] = 1/N * Fourier_basis(bin_s,bin_x) | |
return FM | |
domain_N = list(range(0,8)) | |
bindom = [binarize(x) for x in domain_N] | |
fval = [f(x) for x in bindom] | |
FM = Fourier_Matrix(bindom) | |
FC = np.matrix(fval) * np.matrix(FM) | |
print(FC) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment