##// END OF EJS Templates
Cleaned code, fixed bugs/errors, updated plotting part....
yamaro -
r23:24
parent child
Show More
@@ -4,7 +4,7
4 @author: Yolian Amaro
4 @author: Yolian Amaro
5 '''
5 '''
6
6
7 import pywt
7 #import pywt
8 import numpy as np
8 import numpy as np
9
9
10 def FSfarras():
10 def FSfarras():
@@ -11,12 +11,12
11 import time
11 import time
12 import matplotlib.pyplot as plt
12 import matplotlib.pyplot as plt
13 from scipy.optimize import root
13 from scipy.optimize import root
14
14 from scipy.stats import nanmean
15 from y_hysell96 import*
15
16 from y_hysell96 import *
16 from deb4_basis import *
17 from deb4_basis import *
17 from modelf import *
18 from modelf import *
18 from irls_dn2 import *
19 from irls_dn2 import *
19 #from scipy.optimize import fsolve
20
20
21
21
22 #-------------------------------------------------------------------------------------------------
22 #-------------------------------------------------------------------------------------------------
@@ -136,8 +136,8
136 # fact = fact + wind2 * (-(theta-(mu+sig)));
136 # fact = fact + wind2 * (-(theta-(mu+sig)));
137 # factr = factr + wind2 * (-(thetar-(mu+sig)));
137 # factr = factr + wind2 * (-(thetar-(mu+sig)));
138
138
139
140 # fact = fact/(sum(fact)[0]*2*thbound/Nt); # normalize to integral(f)==1
139 # fact = fact/(sum(fact)[0]*2*thbound/Nt); # normalize to integral(f)==1
140
141
141
142 I = sum(fact)[0];
142 I = sum(fact)[0];
143 fact = fact/I; # normalize to sum(f)==1
143 fact = fact/I; # normalize to sum(f)==1
@@ -232,10 +232,9
232 for i in range(0, thetar.size):
232 for i in range(0, thetar.size):
233 th = thetar[i];
233 th = thetar[i];
234 w = np.exp(1j*k*np.dot(r,np.sin(th)));
234 w = np.exp(1j*k*np.dot(r,np.sin(th)));
235 f_capon[i] = np.divide(1, ( np.dot( w.T.conj(), (linalg.solve(Rnz,w)) ) ).real)
235 f_capon[i] = 1/ ( np.dot( w.T.conj(), (linalg.solve(Rnz,w)) ) ).real
236
236
237 toc_capon = time.time()
237 toc_capon = time.time()
238
239 elapsed_time_capon = toc_capon - tic_capon;
238 elapsed_time_capon = toc_capon - tic_capon;
240
239
241 f_capon = f_capon.real; # get rid of numerical imaginary noise
240 f_capon = f_capon.real; # get rid of numerical imaginary noise
@@ -300,7 +299,7
300 ystar = myfun(lambda1);
299 ystar = myfun(lambda1);
301 Lambda = np.sqrt(sum(lambda1**2*sigma**2)/(4*G));
300 Lambda = np.sqrt(sum(lambda1**2*sigma**2)/(4*G));
302 ep = np.multiply(-lambda1,sigma**2)/ (2*Lambda);
301 ep = np.multiply(-lambda1,sigma**2)/ (2*Lambda);
303 es = np.dot(Hr, f_maxent) - gnz; # should be same as ep
302 es = np.dot(Hr, f_maxent) - gnz;
304 chi2 = np.sum((es/sigma)**2);
303 chi2 = np.sum((es/sigma)**2);
305
304
306
305
@@ -310,34 +309,23
310 # (Use Nr, thetar, gnz, and Hr from MaxEnt above)
309 # (Use Nr, thetar, gnz, and Hr from MaxEnt above)
311
310
312 Psi = deb4_basis(Nr);
311 Psi = deb4_basis(Nr);
313
314 # ------------Remove this-------------------------------------------
315 # wavelet1 = pywt.Wavelet('db4')
316 # Phi, Psi, x = wavelet1.wavefun(level=3)
317 #-------------------------------------------------------------------
318
312
319 # add "sum to 1" constraint
313 # add "sum to 1" constraint
320 H2 = np.concatenate( (Hr, np.ones(shape=(1,Nr))), axis=0 );
314 H2 = np.concatenate( (Hr, np.ones(shape=(1,Nr))), axis=0 );
321 g2 = np.concatenate( (gnz, np.array([[Nr/Nt]])), axis=0 );
315 g2 = np.concatenate( (gnz, np.array([[Nr/Nt]])), axis=0 );
322
316
323 tic_cs = time.time();
317 tic_cs = time.time();
324
325
326 # plt.figure(4)
327 # plt.imshow(Psi)#, interpolation='nearest')
328 # #plt.xticks([]); plt.yticks([])
329 # plt.show()
330
318
331 # Inversion
319 # Inversion
332 s = irls_dn2(np.dot(H2,Psi),g2,0.5,G);
320 s = irls_dn2(np.dot(H2,Psi),g2,0.5,G);
333
321
334 #print s
322 toc_cs = time.time()
323 elapsed_time_cs = toc_cs - tic_cs;
335
324
336 # Brightness function
325 # Brightness function
337 f_cs = np.dot(Psi,s);
326 f_cs = np.dot(Psi,s);
338
327
339 toc_cs = time.time()
328
340 elapsed_time_cs = toc_cs - tic_cs;
341
329
342 # Plot
330 # Plot
343 plt.figure(4)
331 plt.figure(4)
@@ -393,7 +381,7
393 # Plot stuff
381 # Plot stuff
394 #-------------------------------------------------------------------------------------------------
382 #-------------------------------------------------------------------------------------------------
395
383
396 #---- Capon----
384 #---- Capon----#
397 plt.figure(5)
385 plt.figure(5)
398 plt.subplot(3, 1, 1)
386 plt.subplot(3, 1, 1)
399 plt.plot(180/np.pi*thetar, f_capon, 'r', label='Capon')
387 plt.plot(180/np.pi*thetar, f_capon, 'r', label='Capon')
@@ -407,7 +395,7
407 plt.text(0.0, 1.01, '1e-4', fontsize=10, transform = plt.gca().transAxes)
395 plt.text(0.0, 1.01, '1e-4', fontsize=10, transform = plt.gca().transAxes)
408
396
409
397
410 #---- MaxEnt----
398 #---- MaxEnt----#
411 plt.subplot(3, 1, 2)
399 plt.subplot(3, 1, 2)
412 plt.plot(180/np.pi*thetar, f_maxent, 'r', label='MaxEnt')
400 plt.plot(180/np.pi*thetar, f_maxent, 'r', label='MaxEnt')
413 plt.plot(180/np.pi*thetat,fact, 'k--', label='Truth')
401 plt.plot(180/np.pi*thetat,fact, 'k--', label='Truth')
@@ -420,7 +408,7
420 plt.text(0.0, 1.01, '1e-4', fontsize=10, transform = plt.gca().transAxes)
408 plt.text(0.0, 1.01, '1e-4', fontsize=10, transform = plt.gca().transAxes)
421
409
422
410
423 #---- Compressed Sensing----
411 #---- Compressed Sensing----#
424 plt.subplot(3, 1, 3)
412 plt.subplot(3, 1, 3)
425 plt.plot(180/np.pi*thetar, f_cs, 'r', label='CS')
413 plt.plot(180/np.pi*thetar, f_cs, 'r', label='CS')
426 plt.plot(180/np.pi*thetat,fact, 'k--', label='Truth')
414 plt.plot(180/np.pi*thetat,fact, 'k--', label='Truth')
@@ -430,27 +418,7
430 # formatting y-axis
418 # formatting y-axis
431 locs,labels = plt.yticks()
419 locs,labels = plt.yticks()
432 plt.yticks(locs, map(lambda x: "%.1f" % x, locs*1e4))
420 plt.yticks(locs, map(lambda x: "%.1f" % x, locs*1e4))
433 plt.text(0.0, 1.01, '1e-4', fontsize=10, transform = plt.gca().transAxes)
421 plt.text(0.0, 1.01, '1e-4', fontsize=10, transform = plt.gca().transAxes)
434
435
436 # # PLOT PARA COMPRESSED SENSING
437 # #
438 # # subplot(3,1,3);
439 # # plot(180/pi*thetar,f_cs,'r-');
440 # # hold on;
441 # # plot(180/pi*thetat,fact,'k--');
442 # # hold off;
443 # # ylim([min(f_cs) 1.1*max(fact)]);
444 # # # title(sprintf('rel. RMSE: #.2e\tCorr: #.3f Corrc: #.3f', relrmse_cs, corr_cs, corrc_cs));
445 # # # title 'Compressed Sensing - Debauchies Wavelets'
446 # # xlabel 'Degrees'
447 # # ylabel({'Power';'(arbitrary units)'})
448 # # legend('Comp. Sens.','Truth');
449 # #
450 # # # set(gcf,'Position',[749 143 528 881]); # CSL
451 # # # set(gcf,'Position',[885 -21 528 673]); # macbook
452 # # pause(0.01);
453
454
422
455 # # Store Results
423 # # Store Results
456 corr[0, snri, Ni] = corr_fourier;
424 corr[0, snri, Ni] = corr_fourier;
@@ -468,20 +436,18
468 corrc[2,snri,Ni] = corrc_maxent;
436 corrc[2,snri,Ni] = corrc_maxent;
469 corrc[3,snri,Ni] = corrc_cs;
437 corrc[3,snri,Ni] = corrc_cs;
470
438
471
439 print "--------Time performace--------"
472 print 'Capon:\t', elapsed_time_capon, 'sec';
440 print 'Capon:\t', elapsed_time_capon, 'sec';
473 print 'Maxent:\t',elapsed_time_maxent, 'sec';
441 print 'Maxent:\t',elapsed_time_maxent, 'sec';
474 print 'CS:\t',elapsed_time_cs, 'sec';
442 print 'CS:\t',elapsed_time_cs, 'sec\n';
475
443
476 print (NN*(snri+1) + Ni), '/', (SNRdBvec.size*NN);
444 print (NN*(snri+1) + Ni), '/', (SNRdBvec.size*NN), '\n';
477
445
478 print corr
446
479
447 #-------------------------------------------------------------------------------------------------
480 print corr.shape
448 # Analyze and plot statistics
481
449 #-------------------------------------------------------------------------------------------------
482
450
483 ## Analyze and plot statistics
484
485 metric = corr; # set this to rmse, corr, or corrc
451 metric = corr; # set this to rmse, corr, or corrc
486
452
487 # Remove outliers (this part was experimental and wasn't used in the paper)
453 # Remove outliers (this part was experimental and wasn't used in the paper)
@@ -499,28 +465,32
499 # end
465 # end
500 # end
466 # end
501
467
502 # Avg ignoring NaNs
468
503 def nanmean(data, **args):
469 # Avg ignoring NaNs
504 return numpy.ma.filled(numpy.ma.masked_array(data,numpy.isnan(data)).mean(**args), fill_value=numpy.nan)
470 ave = np.zeros(shape=(4))
505
471
506 # ave = np.zeros(shape=(4))
472 ave[0] = nanmean(metric[0,:,:]); # Fourier
507 #
473 ave[1] = nanmean(metric[1,:,:]); # Capon
508 # ave[0] = nanmean(metric, axis=0);
474 ave[2] = nanmean(metric[2,:,:]); # MaxEnt
509 # ave[1] = nanmean(metric, axis=1);
475 ave[3] = nanmean(metric[3,:,:]); # Compressed Sensing
510 # ave[2] = nanmean(metric, axis=2);
476
511 # ave[3] = nanmean(metric, axis=3);
477 # Plot based on chosen metric
512
513 #print ave
514 plt.figure(6);
478 plt.figure(6);
515 f = plt.scatter(SNRdBvec, corr[0], marker='+', color='b', s=60); # Fourier
479 f = plt.scatter(SNRdBvec, ave[0], marker='+', color='b', s=60); # Fourier
516 c = plt.scatter(SNRdBvec, corr[1], marker='o', color= 'c', s=60); # Capon
480 c = plt.scatter(SNRdBvec, ave[1], marker='o', color= 'g', s=60); # Capon
517 me= plt.scatter(SNRdBvec, corr[2], marker='s', color= 'y', s=60); # MaxEnt
481 me= plt.scatter(SNRdBvec, ave[2], marker='s', color= 'c', s=60); # MaxEnt
518 cs= plt.scatter(SNRdBvec, corr[3], marker='*', color='r', s=60); # Compressed Sensing
482 cs= plt.scatter(SNRdBvec, ave[3], marker='*', color='r', s=60); # Compressed Sensing
519
483
520
521 plt.legend((f,c,me,cs),('Fourier','Capon', 'MaxEnt', 'Comp. Sens.'),scatterpoints=1, loc='upper right')
484 plt.legend((f,c,me,cs),('Fourier','Capon', 'MaxEnt', 'Comp. Sens.'),scatterpoints=1, loc='upper right')
522 plt.xlabel('SNR')
485 plt.xlabel('SNR')
523 plt.ylabel('Correlation with Truth')
486 plt.ylabel('Correlation with Truth')
524
487
488 print "--------Correlations--------"
489 print "Fourier:", ave[0]
490 print "Capon:\t", ave[1]
491 print "MaxEnt:\t", ave[2]
492 print "CS:\t", ave[3]
493
525 plt.show()
494 plt.show()
526
495
496
@@ -4,7 +4,6
4 @author: Yolian Amaro
4 @author: Yolian Amaro
5 '''
5 '''
6
6
7 import numpy as np
8 from FSfarras import *
7 from FSfarras import *
9 from dualfilt1 import *
8 from dualfilt1 import *
10 from dualtree import *
9 from dualtree import *
@@ -4,7 +4,6
4 @author: Yolian Amaro
4 @author: Yolian Amaro
5 '''
5 '''
6
6
7 #from scipy.sparse import eye
8 from scipy import linalg
7 from scipy import linalg
9 import scipy.sparse as sps
8 import scipy.sparse as sps
10 import numpy as np
9 import numpy as np
@@ -27,17 +26,12
27 [Q,R] = linalg.qr(A.T.conj(), mode='economic');
26 [Q,R] = linalg.qr(A.T.conj(), mode='economic');
28
27
29
28
30 c = linalg.solve(R.T.conj(),b); # will be used later also
29 c = linalg.solve(R.T.conj(),b); # will be used later also
31 u = np.dot(Q,c); # minimum 2-norm solution
30 u = np.dot(Q,c); # minimum 2-norm solution
32 I = sps.eye(M);
31 I = sps.eye(M);
33
32
34 # Temporary N x N matrix
33 # Temporary N x N matrix
35 temp = np.zeros(shape=(N,N))
34 temp = np.zeros(shape=(N,N))
36
37 #---------- not needed, defined above--------------
38 # Spacing of floating point numbers
39 #eps = np.spacing(1)
40 #--------------------------------------------------
41
35
42 # Loop until damping parameter is small enough
36 # Loop until damping parameter is small enough
43 while (eps > 1e-7):
37 while (eps > 1e-7):
@@ -51,30 +45,12
51
45
52 # Update
46 # Update
53 w = (u**2 + eps)**(1-p/2.0);
47 w = (u**2 + eps)**(1-p/2.0);
54
55 # #---- Very inefficient- REMOVE THIS PART------
56 # k = 0
57 # # Sparse matrix
58 # for i in range (0, N):
59 # for j in range (0,N):
60 # if(i==j):
61 # temp[i,j] = w[k]
62 # k = k+1
63 #--------------------------------------------------
64
65 np.fill_diagonal(temp, w)
48 np.fill_diagonal(temp, w)
66 #-----------------------------------------------
49
67
68 # Compressed Sparse Matrix
50 # Compressed Sparse Matrix
69 W = sps.csr_matrix(temp); #Compressed Sparse Row matrix
51 W = sps.csr_matrix(temp); #Compressed Sparse Row matrix
70
52
71
72 WAT = W*A.T.conj();
53 WAT = W*A.T.conj();
73
74 #print "WAT", WAT.shape
75 #print "np.dot(A,WAT)", np.dot(A,WAT).shape
76 #print "np.dot(lambda1,I)", np.dot(lambda1,I).shape
77 #print "linalg.solve((np.dot(A,WAT) + np.dot(lambda1,I)), b)", linalg.solve((np.dot(A,WAT) + np.dot(lambda1,I)), b).shape
78
54
79 u_new = np.dot(WAT , linalg.solve((np.dot(A,WAT) + np.dot(lambda1,I)), b));
55 u_new = np.dot(WAT , linalg.solve((np.dot(A,WAT) + np.dot(lambda1,I)), b));
80
56
@@ -5,13 +5,7
5 '''
5 '''
6
6
7 from irls_dn import *
7 from irls_dn import *
8 from scipy.optimize import fsolve
8 from scipy.optimize import brentq
9 import numpy as np
10 from scipy.optimize import *
11 from dogleg import *
12 from numpy.linalg import norm
13
14 import matplotlib.pyplot as plt
15
9
16 def irls_dn2(A,b,p,G):
10 def irls_dn2(A,b,p,G):
17
11
@@ -31,7 +25,7
31 alpha = 2.0; # Line search parameter
25 alpha = 2.0; # Line search parameter
32 lambda1 = 1e5; # What's a reasonable but safe initial guess?
26 lambda1 = 1e5; # What's a reasonable but safe initial guess?
33 u = irls_dn(A,b,p,lambda1);
27 u = irls_dn(A,b,p,lambda1);
34 #print "u\n", u
28
35
29
36 fid = norm(np.dot(A,u)-b)**2;
30 fid = norm(np.dot(A,u)-b)**2;
37
31
@@ -41,34 +35,19
41 lambda1 = lambda1 / alpha; # Balance between speed and accuracy
35 lambda1 = lambda1 / alpha; # Balance between speed and accuracy
42 u = irls_dn(A,b,p,lambda1);
36 u = irls_dn(A,b,p,lambda1);
43 fid = norm(np.dot(A,u)-b)**2;
37 fid = norm(np.dot(A,u)-b)**2;
44 print 'lambda = %2e \t' % lambda1, '||A*u-b||^2 = %.1f\n' % fid;
38 print 'lambda = %2e \t' % lambda1, '||A*u-b||^2 = %.1f' % fid;
45 #print u
46
39
47 # Refinement using fzero/ brentq
40 # Refinement using fzero/ brentq
48 lambda0 = np.array([lambda1,lambda1*alpha]); # interval with zero-crossing
41
49
42 lambda0 = np.array([lambda1,lambda1*alpha]); # interval with zero-crossing
50
43
51 def myfun(lambda1):
44 def myfun(lambda1):
52 temp1 = np.dot(A, irls_dn(A,b,p,lambda1))
45 return norm(np.dot(A, irls_dn(A,b,p,lambda1)) - b)**2 - G;
53 temp2 = norm(temp1-b)
54 temp3 = temp2**2-G
55 #return np.linalg.norm(np.dot(A, irls_dn(A,b,p,lambda1)) - b)**2 - G;
56 return temp3
57
58 print "tolerancia=", 0.01*lambda1
59
46
60 #lambda1 = root(myfun, lambda1, method='krylov', tol=0.01*lambda1);
47 # Find zero-crossing at given interval (lambda1, lambda1*alpha)
61 #lambda1 = lambda1.x
62
63 print "lambda0[0]", lambda0[0]
64 print "lambda0[1]", lambda0[1]
65
66 lambda1 = brentq(myfun, lambda0[0], lambda0[1], xtol=0.01*lambda1)
48 lambda1 = brentq(myfun, lambda0[0], lambda0[1], xtol=0.01*lambda1)
67
68 print "lambda final=", lambda1
69
49
70 u = irls_dn(A,b,p,lambda1);
50 u = irls_dn(A,b,p,lambda1);
71
51
72
73 return u;
52 return u;
74
53
@@ -4,8 +4,7
4 @author: Yolian Amaro
4 @author: Yolian Amaro
5 '''
5 '''
6
6
7 from multirate import *
7 from multirate import upfirdn
8 import numpy as np
9 from cshift import *
8 from cshift import *
10
9
11 def sfb(lo, hi, sf):
10 def sfb(lo, hi, sf):
@@ -4,7 +4,6
4 @author: Yolian Amaro
4 @author: Yolian Amaro
5 '''
5 '''
6
6
7 import numpy as np
8 from modelf import *
7 from modelf import *
9
8
10 def y_hysell96(lambda1,g,sigma,F,G,H):
9 def y_hysell96(lambda1,g,sigma,F,G,H):
General Comments 0
You need to be logged in to leave comments. Login now