This repository was archived by the owner on Sep 27, 2024. It is now read-only.
forked from hadizand/DL_CS_ECG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurementMatrix.py
100 lines (77 loc) · 2.64 KB
/
measurementMatrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import numpy as np
def generate_DBBD_matrix(M, N):
"""
Generates a deterministic Diagonally Blocked Block Diagonal (DBBD) matrix.
A DBBD matrix is a type of block diagonal matrix where each block is a square diagonal matrix.
Parameters
----------
M : int
Number of rows in the matrix.
N : int
Number of columns in the matrix. Should be a multiple of M.
Returns
-------
A : numpy.ndarray
The generated DBBD matrix of shape (M, N).
Raises
------
ValueError
If `N` is not a multiple of `M`.
Example
-------
>>> generate_DBDD_matrix(3, 9)
array([[1., 1., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 1., 1.]])
"""
if N % M != 0:
raise ValueError("N should be a multiple of M.")
Phi = np.zeros((M, N))
m = N // M
for i in range(M):
Phi[i, i*m:(i+1)*m] = 1
return Phi
def generate_random_matrix(M, N, matrix_type='gaussian'):
"""
Generates a random matrix based on the specified type.
Parameters
----------
M : int
Number of rows in the matrix.
N : int
Number of columns in the matrix.
matrix_type : str, optional (default='gaussian')
The type of random matrix to generate. Options are:
- 'gaussian': A matrix with entries drawn from a normal distribution scaled by 1/M.
- 'scaled_binary': A matrix with binary entries (±0.5), scaled by 1/sqrt(M).
- 'unscaled_binary': A matrix with binary entries (±1), with no scaling.
Returns
-------
A : numpy.ndarray
The generated random matrix of shape (M, N).
Raises
------
ValueError
If `matrix_type` is not one of the supported types.
Example
-------
>>> generate_random_matrix(2, 3, matrix_type='gaussian')
array([[ 0.01, -0.02, 0.03],
[-0.04, 0.05, -0.06]])
>>> generate_random_matrix(2, 3, matrix_type='scaled_binary')
array([[-0.5, 0. , -0.5],
[ 0.5, -0.5, 0. ]])
>>> generate_random_matrix(2, 3, matrix_type='unscaled_binary')
array([[ 1., -1., 1.],
[-1., 1., -1.]])
"""
if matrix_type == 'gaussian':
A = ((1/M)**2) * np.random.randn(M, N)
elif matrix_type == 'scaled_binary':
A = np.random.binomial(1, 0.5, size=(M, N)) - 0.5
A = (1/np.sqrt(M)) * A
elif matrix_type == 'unscaled_binary':
A = np.random.binomial(1, 0.5, size=(M, N)) * 2 - 1
else:
raise ValueError("Unsupported matrix type. Choose either 'gaussian', 'scaled_binary', or 'unscaled_binary'.")
return A