comparison pyCSalgos/GAP/GAP.py @ 52:768b57e446ab

Created Analysis.py and working
author nikcleju
date Thu, 08 Dec 2011 09:05:04 +0000
parents 4f3bc35195ce
children a115c982a0fd
comparison
equal deleted inserted replaced
51:eb4c66488ddf 52:768b57e446ab
3 Created on Thu Oct 13 14:05:22 2011 3 Created on Thu Oct 13 14:05:22 2011
4 4
5 @author: ncleju 5 @author: ncleju
6 """ 6 """
7 7
8 #from numpy import * 8
9 #from scipy import *
10 import numpy 9 import numpy
11 import numpy.linalg 10 import numpy.linalg
12 import scipy as sp 11 import scipy as sp
13 #import scipy.stats #from scipy import stats 12
14 #import scipy.linalg #from scipy import lnalg
15 import math 13 import math
16 14
17 from numpy.random import RandomState
18 rng = RandomState()
19
20 def Generate_Analysis_Operator(d, p):
21 # generate random tight frame with equal column norms
22 if p == d:
23 T = rng.randn(d,d);
24 [Omega, discard] = numpy.qr(T);
25 else:
26 Omega = rng.randn(p, d);
27 T = numpy.zeros((p, d));
28 tol = 1e-8;
29 max_j = 200;
30 j = 1;
31 while (sum(sum(abs(T-Omega))) > numpy.dot(tol,numpy.dot(p,d)) and j < max_j):
32 j = j + 1;
33 T = Omega;
34 [U, S, Vh] = numpy.linalg.svd(Omega);
35 V = Vh.T
36 #Omega = U * [eye(d); zeros(p-d,d)] * V';
37 Omega2 = numpy.dot(numpy.dot(U, numpy.concatenate((numpy.eye(d), numpy.zeros((p-d,d))))), V.transpose())
38 #Omega = diag(1./sqrt(diag(Omega*Omega')))*Omega;
39 Omega = numpy.dot(numpy.diag(1.0 / numpy.sqrt(numpy.diag(numpy.dot(Omega2,Omega2.transpose())))), Omega2)
40 #end
41 ##disp(j);
42 #end
43 return Omega
44
45
46 def Generate_Data_Known_Omega(Omega, d,p,m,k,noiselevel, numvectors, normstr):
47 #function [x0,y,M,LambdaMat] = Generate_Data_Known_Omega(Omega, d,p,m,k,noiselevel, numvectors, normstr)
48
49 # Building an analysis problem, which includes the ingredients:
50 # - Omega - the analysis operator of size p*d
51 # - M is anunderdetermined measurement matrix of size m*d (m<d)
52 # - x0 is a vector of length d that satisfies ||Omega*x0||=p-k
53 # - Lambda is the true location of these k zeros in Omega*x0
54 # - a measurement vector y0=Mx0 is computed
55 # - noise contaminated measurement vector y is obtained by
56 # y = y0 + n where n is an additive gaussian noise with norm(n,2)/norm(y0,2) = noiselevel
57 # Added by Nic:
58 # - Omega = analysis operator
59 # - normstr: if 'l0', generate l0 sparse vector (unchanged). If 'l1',
60 # generate a vector of Laplacian random variables (gamma) and
61 # pseudoinvert to find x
62
63 # Omega is known as input parameter
64 #Omega=Generate_Analysis_Operator(d, p);
65 # Omega = randn(p,d);
66 # for i = 1:size(Omega,1)
67 # Omega(i,:) = Omega(i,:) / norm(Omega(i,:));
68 # end
69
70 #Init
71 LambdaMat = numpy.zeros((k,numvectors))
72 x0 = numpy.zeros((d,numvectors))
73 y = numpy.zeros((m,numvectors))
74 realnoise = numpy.zeros((m,numvectors))
75
76 M = rng.randn(m,d);
77
78 #for i=1:numvectors
79 for i in range(0,numvectors):
80
81 # Generate signals
82 #if strcmp(normstr,'l0')
83 if normstr == 'l0':
84 # Unchanged
85
86 #Lambda=randperm(p);
87 Lambda = rng.permutation(int(p));
88 Lambda = numpy.sort(Lambda[0:k]);
89 LambdaMat[:,i] = Lambda; # store for output
90
91 # The signal is drawn at random from the null-space defined by the rows
92 # of the matreix Omega(Lambda,:)
93 [U,D,Vh] = numpy.linalg.svd(Omega[Lambda,:]);
94 V = Vh.T
95 NullSpace = V[:,k:];
96 #print numpy.dot(NullSpace, rng.randn(d-k,1)).shape
97 #print x0[:,i].shape
98 x0[:,i] = numpy.squeeze(numpy.dot(NullSpace, rng.randn(d-k,1)));
99 # Nic: add orthogonality noise
100 # orthonoiseSNRdb = 6;
101 # n = randn(p,1);
102 # #x0(:,i) = x0(:,i) + n / norm(n)^2 * norm(x0(:,i))^2 / 10^(orthonoiseSNRdb/10);
103 # n = n / norm(n)^2 * norm(Omega * x0(:,i))^2 / 10^(orthonoiseSNRdb/10);
104 # x0(:,i) = pinv(Omega) * (Omega * x0(:,i) + n);
105
106 #elseif strcmp(normstr, 'l1')
107 elif normstr == 'l1':
108 print('Nic says: not implemented yet')
109 raise Exception('Nic says: not implemented yet')
110 #gamma = laprnd(p,1,0,1);
111 #x0(:,i) = Omega \ gamma;
112 else:
113 #error('normstr must be l0 or l1!');
114 print('Nic says: not implemented yet')
115 raise Exception('Nic says: not implemented yet')
116 #end
117
118 # Acquire measurements
119 y[:,i] = numpy.dot(M, x0[:,i])
120
121 # Add noise
122 t_norm = numpy.linalg.norm(y[:,i],2)
123 n = numpy.squeeze(rng.randn(m, 1))
124 # In case n i just a number, nuit an array, norm() fails
125 if n.ndim == 0:
126 nnorm = abs(n)
127 else:
128 nnorm = numpy.linalg.norm(n, 2);
129 y[:,i] = y[:,i] + noiselevel * t_norm * n / nnorm
130 realnoise[:,i] = noiselevel * t_norm * n / nnorm
131 #end
132
133 return x0,y,M,LambdaMat,realnoise
134
135 #####################
136 15
137 #function [xhat, arepr, lagmult] = ArgminOperL2Constrained(y, M, MH, Omega, OmegaH, Lambdahat, xinit, ilagmult, params) 16 #function [xhat, arepr, lagmult] = ArgminOperL2Constrained(y, M, MH, Omega, OmegaH, Lambdahat, xinit, ilagmult, params)
138 def ArgminOperL2Constrained(y, M, MH, Omega, OmegaH, Lambdahat, xinit, ilagmult, params): 17 def ArgminOperL2Constrained(y, M, MH, Omega, OmegaH, Lambdahat, xinit, ilagmult, params):
139 18
140 # 19 #