import scipy.io as io
from scipy import signal
import numpy as np
import matplotlib
import matplotlib.image as im
import matplotlib.pyplot as plt
import time
import struct
import math
from sklearn.metrics.pairwise import cosine_similarity
from mpl_toolkits.mplot3d import Axes3D
import binascii
# Load X matrix
output = io.loadmat("X.mat")['X']
rank = np.linalg.matrix_rank(output)
unitary, singular, vunitary = np.linalg.svd(output, full_matrices=True)
# print(rank)
# print(u.shape)
# print(s.shape)
# print(vh.shape)
# print(s)
# original image
plt.imshow(output, interpolation='nearest', cmap='gray')
plt.title('Original Image')
plt.show()
# SVD reconstruction function
def reconstruct(u, s, vh):
smat = np.zeros((u.shape[0], vh.shape[0]))
smat[:vh.shape[0], :vh.shape[0]] = np.diag(s)
return np.dot(u, np.dot(smat, vh))
# SVD Truncate Function (cuts down to k SVs)
def truncK(s, k):
thresholdMat = np.zeros(s.shape)
thresholdMat = s > s[k]
return s * thresholdMat
# SVD truncate test
thresh = truncK(singular, 100)
plt.imshow(reconstruct(unitary, thresh, vunitary), interpolation='nearest', cmap='gray')
plt.title('Original Image')
plt.show()
# SVDs - Image starts to become extremely blurry at 10 SVs
values = [100, 50, 30, 20, 10, 1]
for value in values:
sing = truncK(singular, value)
reconstructed = reconstruct(unitary, sing, vunitary)
plt.imshow(reconstructed, interpolation='nearest', cmap='gray')
plt.title(f'{value} Singular Values Image')
plt.show()
plt.imsave(f'{value}_Singluar_Values_Image.png', reconstructed, cmap = 'Greys_r')
# Center Matrix
def center(matrix):
output = np.zeros(matrix.shape)
mean = np.mean(matrix, axis=0)
for row in range(matrix.shape[0]):
for col in range(matrix.shape[1]):
output[row][col] = matrix[row][col] - mean[col]
return output
def print1Matrix(matrix, ptitle=''):
fig = plt.figure()
x = np.arange(0, matrix.shape[0], 1)
plt.scatter(x, matrix)
plt.savefig(f'singular_values_{ptitle}.png')
plt.show()
# print matrix helper function
def printMatrix(matrix, ptitle=''):
fig = plt.figure()
x = np.zeros(matrix.shape[1])
y = np.zeros(matrix.shape[1])
for row in matrix:
for col in range(matrix.shape[1]):
x[col] = row[col]
y[col] = col
plt.scatter(y, x)
x = np.zeros(matrix.shape[1])
y = np.zeros(matrix.shape[1])
plt.show()
# Load Image
eavesdropping = io.loadmat("eavesdropping.mat")["Y"]
print(eavesdropping.shape)
print(eavesdropping)
(189, 250)
[[-0.66513832 0.25104256 0.99232413 ... -0.20359839 -0.15619818
-0.29911372]
[-1.50520773 0.99242977 0.16507988 ... -0.4049977 -1.06757605
-0.80849093]
[-0.61040474 0.12677165 0.26199726 ... -0.02475327 -0.8503764
-0.5156413 ]
...
[-0.79238094 -0.46170266 -0.50858065 ... -0.89669862 -0.0810798
0.58548784]
[-1.82573704 1.44140333 0.16248024 ... 0.26534426 -1.09686154
1.0722296 ]
[-0.11787526 0.39097268 -1.1713306 ... 1.04220242 0.63938637
2.17226697]]
eavU, eavS, eavVh = np.linalg.svd(eavesdropping)
eaveCenter = center(eavesdropping)
ceavU, ceavS, ceavVh = np.linalg.svd(eaveCenter)
print1Matrix(eavS, ptitle='not_centered')
print1Matrix(ceavS, ptitle='centered')
principleMatrix = np.matmul(ceavU, np.diag(ceavS))
printMatrix(principleMatrix)
vMat = np.conjugate(np.transpose(ceavVh))
printMatrix(ceavVh[0:2])
principle = np.zeros(eavesdropping.shape)
principle[0] = ceavVh[0]
principle[1] = ceavVh[1]
principle = np.conjugate(np.transpose(principle))
principle2 = np.matmul(eaveCenter, principle)
plt.title('principle directions')
plt.scatter(principle2.transpose()[0], principle2.transpose()[1])
plt.savefig('principle directions.png')
plt.show()
# Decode
decode = (principle2.transpose()[0] < 0).astype(int)
for b in range(27):
currentByte = ''.join(str(n) for n in decode[(7 * b) : (7 * b) + 7])
currentInt = int(currentByte, 2)
print(chr(currentInt), end='')
richb is love richb is life