-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gen_Fig2b.m
152 lines (119 loc) · 3.14 KB
/
Gen_Fig2b.m
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
% This script generates Figure 3b of the paper "SPARSE MULTI-REFERENCE
% ALIGNMENT: SAMPLE COMPLEXITY AND COMPUTATIONAL HARDNESS" by Tamir Bendory, Oscar Mickelin, and Amit Singer
%
% Last update: September 20, 2021
% Oscar Mickelin
clc; clf; clear; close all
Ls = 2:2:26; %signal length
Ms = 1:1:max(Ls); %sparsity level
repeats = 10; %number of trials
inner_rep = 10; %number of random R to try
errs = Inf*ones(length(Ls), length(Ms)); %average residual
%main loop
for Lind = 1:length(Ls)
L = Ls(Lind);
for Mind = 1:length(Ms)
M = Ms(Mind);
if M < L/2
for iter = 1:repeats
disp([L,M,iter])
if iter == 1
errs(Lind,Mind) = solve_binary_sos_phase_inner(L,M,inner_rep)/repeats;
else
errs(Lind,Mind) = errs(Lind,Mind) + solve_binary_sos_phase_inner(L,M,inner_rep)/repeats;
end
end
end
end
%plot during loop
imagesc(errs)
caxis([0,1])
colorbar
pause(0.01)
end
%save data
fname = sprintf('./binary_sosN%drepeats%dinner_rep%d.mat', L, repeats,inner_rep);
save(fname)
%plot after loop
imagesc(errs)
caxis([0,1])
colorbar
function err = solve_binary_sos_phase_inner(L,M,inner_rep)
%%% Generates one instance of the binary MRA
%generate a binary signal
ind_true = randperm(L);
ind_true = ind_true(1:M);
if sum(ind_true==1)==0
ind_true(1) = 1;
end
x_true = zeros(L,1);
x_true (ind_true) = 1;
% generate R
R = randn(L);
R = R + R';
R = R/norm(R);
%run SoS
[err,x_est] = solve_binary_sos_phase(L,M,x_true,R);
for i = 1:inner_rep-1
if norm(x_est) <= 1e-3
%if SoS failed, rerun a maximum of inner_rep times
[err,x_est] = solve_binary_sos_phase(L,M,x_true,R);
end
end
end
function [err,x_est,x_true,y] = solve_binary_sos_phase(L,M,x_true,R)
%%% Solves one instance of the binary MRA
yalmip('clear')
mset clear
mset('yalmip',true)
mset(sdpsettings('solver', 'mosek','cachesolvers',1,'verbose',0))
%variable
mpol('x',L,1)
% Generate ground-truth, if not provided
if nargin <= 2
ind_true = randperm(L);
ind_true = ind_true(1:M);
if sum(ind_true==1)==0
ind_true(1) = 1;
end
x_true = zeros(L,1);
x_true (ind_true) = 1;
end
% measurements
y = abs(fft(x_true)).^2;
% SoS
% preparation
F = dftmtx(L);
if nargin <= 3
R = randn(L);
R = R+R';
R = R/norm(R);
end
obj = x'*R*x;
constr = [sum(x.^2) == M, x'.^2 == x', x(1) == 1];
% measurement constraints
for i = 1:L
f = F(i, :);
a = real(f);
b = imag(f);
constr = [constr, (a*x)^2 + (b*x)^2 == y(i)];
end
%%
degree = 2; %second-order sum-of-squares relaxation
disp('Solving the problem')
try
P = msdp(min(obj), constr, degree);
[status,~] = msol(P);
if status == 0
%if not solved, use x = 0 as result
x_est = zeros(L,1);
err = compute_error(x_est, x_true);
return
end
x_est = double(x);
err = compute_error(x_est, x_true);
catch
x_est = zeros(L,1);
err = compute_error(x_est, x_true);
end
end