Last active
December 10, 2018 10:31
-
-
Save ThomasA/8eac456c1f27e6dc9ef03f8bcef3fcc6 to your computer and use it in GitHub Desktop.
A small Python script to test a claim from the StackExchange answer https://dsp.stackexchange.com/a/53947/1464 about the coherence of a matrix
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
""" | |
This is a small script investigating the coherence discussed in the | |
Stackexchange Signal Processing answer here: | |
https://dsp.stackexchange.com/a/53947/1464 | |
""" | |
import numpy as np | |
from scipy.fftpack import dct | |
from scipy.linalg import circulant | |
def coherence(mat): | |
""" | |
Calculate the coherence of a matrix. | |
""" | |
gram_mat = mat.conj().T @ mat | |
gram_mat[np.diag_indices_from(gram_mat)] = 0 | |
return np.max(np.abs(gram_mat)) | |
# Construct the 2D DCT matrix | |
dct1_mat = dct(np.eye(32)) | |
B = np.kron(dct1_mat, dct1_mat) | |
B /= np.linalg.norm(B, axis=0) | |
# Construct a circulant 200 x 1024 matrix (from random Gaussian | |
# samples) | |
circ_vec = np.random.randn(1, 1024) | |
A = circulant(circ_vec)[:200,:] | |
A /= np.linalg.norm(A, axis=0) | |
# Construct and normalise the product of A and B | |
prod_mat = A @ B | |
prod_mat /= np.linalg.norm(prod_mat, axis=0) | |
# Just out of curiosity, try a completely random Gaussian matrix in | |
# place of A | |
C = np.random.randn(200,1024) | |
C /= np.linalg.norm(C) | |
prod_mat2 = C @ B | |
prod_mat2 /= np.linalg.norm(prod_mat2) | |
print(f"Coherence of A: {coherence(A)}") | |
print(f"Coherence of B: {coherence(B)}") | |
print(f"Coherence of A @ B: {coherence(prod_mat)}") | |
print(f"Coherence of C: {coherence(C)}") | |
print(f"Coherence of C @ B: {coherence(prod_mat2)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment