Add basic code

This commit is contained in:
Ksyer
2024-07-16 15:54:44 +08:00
parent 088f1a82fd
commit e98983b374
5 changed files with 65 additions and 43 deletions
+82
View File
@@ -0,0 +1,82 @@
% Input: y,A,lambda,tau,Kit
% Output: x_hat_wl,x_hat_d
% Main structure of cVAMP
function [x_hat_wl, x_hat_d] = cVAMPro(y, A, lambda, tau, Kit)
% Initialization
[M, N] = size(A);
gamma = M / N;
k = 0;
p = ctranspose(A) * y;
h_1 = p;
Q_1 = gamma;
tau_d = 1;
% Iteration
while ((k < Kit) && (tau_d > tau))
% Factorized Part
x_1 = ST(h_1, lambda, Q_1);
chi_1 = F1(x_1, lambda, Q_1);
% Message Passing
h_2 = x_1 / chi_1 - h_1;
Q_2 = 1 / chi_1 - Q_1;
% Gaussian Part
t1 = (p + h_2) / Q_2;
t2 = ctranspose(A) * (A * (p + h_2)) / ((Q_2 + 1) * Q_2);
x_2 = t1 - t2;
chi_2 = gamma / (Q_2 + 1) + (1 - gamma) / Q_2;
% Message Passing
h_1_next = x_2 ./ chi_2 - h_2;
Q_1_next = 1 / chi_2 - Q_2;
tau_d = norm(h_1_next - h_1, Inf) / norm(h_1_next, Inf);
k = k + 1;
% output
x_hat_wl = x_1;
x_hat_d = h_1_next / Q_1_next;
% next
h_1 = h_1_next;
Q_1 = Q_1_next;
end
end
% SoftThreshold function
function x = ST(h_1, lambda, Q_1)
[N, M] = size(h_1);
x = zeros(N, M);
for i = 1:N
sign = h_1(i) ./ abs(h_1(i));
diff = abs(h_1(i)) - lambda(i);
x(i) = sign .* (diff ./ Q_1) .* SF(diff);
end
end
% Heaviside's step function
function v = SF(a)
if a > 0
v = 1;
elseif a == 0
v = 0; % at zero points
else
v = 0;
end
end
% Calculation of chi_1
function chi_1 = F1(x_1, lambda, Q_1)
[N, M] = size(x_1);
count = 0;
for i = 1:N
temp = Q_1 * abs(x_1(i)) + lambda(i);
count = count + (2 - lambda(i) / temp) * SF(abs(x_1(i)));
% count = count + (2-lambda(i)/temp) * (abs(x_1(i)) > 1e-4);
end
chi_1 = count / (2 * N * Q_1);
end
+31
View File
@@ -0,0 +1,31 @@
function [x_hat, threshold] = debiased_LASSO(A, y_noise, P_fa, noise_sigma_2)
sz = size(A);
N = sz(2);
LASSO_lambda = 2;
gamma = sz(1) / sz(2);
cvx_begin quiet
variable x_LASSO(N) complex
minimize(LASSO_lambda * norm(x_LASSO, 1) + norm(y_noise - A * x_LASSO, 2))
cvx_end
rho_active = sum(abs(x_LASSO) > 1e-3)/N;
Q_hat = (gamma - rho_active)/(1 - rho_active);
Rho = sum((abs(x_LASSO) > 1e-3).* (2 - LASSO_lambda./(Q_hat*abs(x_LASSO) + LASSO_lambda))) / 2 / N;
diff = 1;
while(diff > 1e-4)
Rho_pre = Rho;
Rho = sum((abs(x_LASSO) > 1e-3).* (2 - LASSO_lambda./((gamma-Rho)/(1-Rho)*abs(x_LASSO) + LASSO_lambda))) / 2 / N;
diff = abs(Rho - Rho_pre);
end
Q_hat = (gamma-Rho)/(1-Rho);
x_d_CROD = x_LASSO + A'*(y_noise - A*x_LASSO)/Q_hat;
x_hat = x_d_CROD;
RSS = 1/sz(2) * norm(y_noise - A*x_LASSO, 2) ^ 2;
sigma_w_2 = (gamma * (1 - gamma)) / ((gamma - Rho)^2) * RSS + noise_sigma_2;
threshold = -sigma_w_2 * log(P_fa);
end
+15
View File
@@ -0,0 +1,15 @@
function Psi = get_Psi(N, M, epi)
Psi = zeros(N,M*N);
for n = 0 : N-1
col = zeros(1, M*N);
Cn = floor(rand()*M);
for q = 0 : N-1
for p = 0:M-1
f1 = p/M*Cn;
f2 = q/N*n*(1+Cn*epi);
col(q*M+p+1) = exp(1j*2*pi*(f1 + f2));
end
end
Psi(n+1, :) = col;
end
end
+29
View File
@@ -0,0 +1,29 @@
function Psi = get_Psi_KR_product(N, M, epi)
d_n = floor(rand(N, 1)*M) / M;
zeta_n = 1 + d_n .* epi; % epi = B / f_c
R = zeros(N, M);
D = zeros(N, N);
for n = 0: N-1
for m = 0: M-1
R(n+1, m+1) = exp(-1j * 2 * pi * m * d_n(n+1));
end
end
for n = 0: N-1
for l = 0: N-1
D(n+1, l+1) = exp(-1j * 2 * pi * l * n * zeta_n(n+1) / N);
end
end
Psi = KR_product(R', D')';
end
function kr = KR_product(F, G)
nR_F = size(F, 1);
nR_G = size(G, 1);
mul = ones(nR_G, 1);
FF = kron(F, mul);
GG = repmat(G, nR_F, 1);
kr = FF .* GG;
end