示例#1
0
def test_FTgene2(N_line=10):
    from functools import partial
    from ..util.SignalToNoise import findnoiselevel
    M = 500
    N = 5 * 1024
    noise = 0.01
    lb = 10.0
    (fid0, fidN) = generatefid2(N, N_line, noise=noise, LB=lb)
    Mode = "sampling"  # "random"   "truncate"
    tr = SL0.transformations(N, M, debug=0)
    tr.post_ft = partial(em, linebroad=lb)
    tr.tpost_ft = partial(em, linebroad=-lb)
    if Mode == "random":
        raise 'a faire'
    elif Mode == "truncate":
        fid = fidN[:M]
    elif Mode == "sampling":
        tr.sampling = np.array(sorted(np.random.permutation(N)[:M]))
        fid = tr.sample(fidN)
    tr.check()
    plt.subplot(3, 1, 1)
    #    plt.plot(fft.fft(x))
    plt.title(
        'noisy original, %d lines, noise at %.1f %% of smallest peak, generated on %d points'
        % (N_line, 100 * noise, N))
    plt.plot(fft.fft(fidN))

    plt.subplot(3, 1, 2)
    plt.plot(fidN)
    plt.title('best FT using %d points (truncated at %.1f %%)' %
              (M, 100.0 * M / N))

    plt.subplot(3, 1, 3)
    sigma_min = 1  #10*findnoiselevel(fft.fft(fid),10)
    sdf = 0.95
    L = 5
    t0 = time.time()
    print("START")
    sol = SL0.SL0gene(tr.transform, tr.ttransform, fid, sigma_min, sdf, L)
    print(time.time() - t0, 'sec')
    plt.subplot(3, 1, 3)
    plt.plot(sol)
    plt.title(
        'SL0 inversion, using %d points (sampled at %.1f %% using %s mode)' %
        (M, 100.0 * M / N, Mode))
    plt.show()
示例#2
0
def test_igor1():
    """
    from http://nuit-blanche.blogspot.com/2011/11/how-to-wow-your-friends-in-high-places.html
    code from Igor Carron
    """
    M = 200
    N = 5000
    noise = 0.1
    A = np.random.randn(M, N)
    x = np.zeros(N)
    x[23] = 1
    x[140] = 1
    y1 = np.dot(A, x) + noise * np.random.randn(M)
    plt.subplot(2, 2, 1)
    plt.plot(x, 'o')
    plt.title(' original')

    x2 = np.linalg.lstsq(A, y1)[0]
    plt.subplot(2, 2, 2)
    plt.plot(x2, '*')
    plt.title(' using lstsq')

    print(np.dot(A, A.T.conj()).shape)
    A3 = np.dot(A.T.conj(), np.linalg.inv(np.dot(A, A.T.conj())))
    print(A3.shape)
    #    A3 = linalg.pinv(A)
    x3 = np.dot(A3, y1)
    plt.subplot(2, 2, 3)
    plt.plot(x3, '*')
    plt.title(' using pinv')

    plt.subplot(2, 2, 4)
    # following parameters for all my computations: sigma_min = 0.00004, sdf= 0.95 )
    sigma_min = 0.00004
    sdf = 0.95
    x4 = SL0.SL0(A, y1, sigma_min, sdf)  #, true_s=x)
    plt.plot(x4, '*')
    plt.title(' using SL0 solver')
    plt.show()
示例#3
0
def test_igor0():
    """
    igor1 modified for my own sake
    """
    M = 200  # sampling
    N = 5000  # spectrum
    noise = 0.01
    A = np.random.rand(M, N)
    x = np.zeros(N)
    x[23] = 1
    x[140] = 1
    y1 = np.dot(A, x) + noise * np.random.randn(M)
    plt.plot(y1)
    plt.figure()
    plt.plot(x, 'x', label=' original')

    # following parameters for all my computations: sigma_min = 0.00004, sdf= 0.95 )
    sigma_min = 0.00004
    sdf = 0.95
    x4 = SL0.SL0(A, y1, sigma_min, sdf, true_s=x)
    plt.plot(x4, '+', label='SL0 solution')
    plt.legend()
    plt.show()
示例#4
0
def test_igor2():
    """from http://nuit-blanche.blogspot.com/2007/09/compressed-sensing-how-to-wow-your.html """
    n = 512  #size of signal
    x0 = np.zeros(n)
    # function is f(x)=2*sin(x)+sin(2x)+21*sin(300x)
    x0[1] = 2
    x0[2] = 1
    x0[300] = 21
    # evaluating f at all sample points xx
    # f(1), f(4), f(6).....f(340) f(356) f(400)
    xx = np.array([
        1, 4, 6, 12, 54, 69, 75, 80, 89, 132, 133, 152, 178, 230, 300, 340,
        356, 400
    ])
    # C is measurement matrix
    C = np.zeros((len(xx), n))
    for i in range(n):
        C[:, i] = np.sin(i * xx)
    b = np.dot(C, x0)
    print(b)
    # b is the result of evaluating f at all sample points xx
    # f(1), f(4), f(6).....f(340) f(356) f(400)
    # C is the measurement matrix
    # let us solve for x and see if it is close to x0
    # solve using SL0
    sigma_min = 0.000004
    sdf = 0.95
    L = 5
    sol = SL0.SL0(C, b, sigma_min, sdf, L)  #, true_s=x0)

    #    plt.plot(abs(x-x0),'o')
    #    plt.title('Error between solution solution found by L1 and actual solution')
    #    figure(2)
    plt.plot(sol, '*', label='solved')
    plt.plot(x0, 'o', label='original')
    plt.legend()
    plt.show()
示例#5
0
def decompress(img, x_01, x_02, mu, lamb, m_ratio, display, method, output):

    img_width, img_height = img.shape[1], img.shape[1]
    row_idx = img_height // 2
    col_idx = img_width // 2

    # Calculate our phi and stuff
    log_map1 = logistic_map(0.11, img_width // 2)
    log_map2 = logistic_map(0.23, img_width // 2)

    if method == 'hadamard':
        m = int(m_ratio * img_width // 2)
        had_src = hadamard(img_width // 2).astype(np.float32)
        phi1_indices = np.argsort(log_map1)
        phi2_indices = np.argsort(log_map2)

        phi1 = had_src[phi1_indices[:m], :]
        phi2 = had_src[phi2_indices[:m], :]
    elif method == 'circulant':
        phi1 = phi_matrix(log_map1, lamb, m_ratio)
        phi2 = phi_matrix(log_map2, lamb, m_ratio)

    r1 = R_matrix(phi1, lamb)
    r2 = R_matrix(phi2, lamb)

    img_width_e, img_height_e = img.shape[:2]
    row_idx_e = img_width_e // 2
    col_idx_e = img_height_e // 2

    e1, e2, e3, e4 = img[:row_idx_e, :col_idx], img[row_idx_e:, :col_idx], img[
        row_idx_e:, col_idx:], img[:row_idx_e, col_idx:]

    #Step 2: inverse pixel exchange
    d4, d1 = rand_pixel_exchange(e4, e1, r2, mode='decrypt')
    d3, d4 = rand_pixel_exchange(e3, d4, r1, mode='decrypt')
    d2, d3 = rand_pixel_exchange(e2, d3, r2, mode='decrypt')
    d1, d2 = rand_pixel_exchange(d1, d2, r1, mode='decrypt')

    #Step 3: run SL0 algorithm with phi matrix
    s1 = SL0(phi1, d1, 1e-12, sigma_decrease_factor=0.5, L=3)
    s2 = SL0(phi2, d2, 1e-12, sigma_decrease_factor=0.5, L=3)
    s3 = SL0(phi1, d3, 1e-12, sigma_decrease_factor=0.5, L=3)
    s4 = SL0(phi2, d4, 1e-12, sigma_decrease_factor=0.5, L=3)

    f1 = cv2.idct(s1)
    f2 = cv2.idct(s2)
    f3 = cv2.idct(s3)
    f4 = cv2.idct(s4)

    final = np.zeros(shape=img.shape)
    final = np.block([[f1, f4], [f2, f3]])

    #tsts = cv2.normalize(final, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
    #cv2.imshow('image final', tsts.astype(np.uint8))
    #cv2.waitKey(0)

    final[final > 255] = 255
    final[final < 0] = 0

    f_name = os.path.splitext(os.path.basename(output))[0]
    f_file = output.replace(f_name, f_name + '_decompressed')
    logging.info('Saving decompressed file: {}'.format(f_file))
    cv2.imwrite(f_file, final.astype(np.uint8))

    if display:
        cv2.imshow('image final', final.astype(np.uint8))
        cv2.waitKey(0)

    return final
示例#6
0
def test_FTgene(N_line=10):
    import functools
    M = 500
    N = 5 * 1024
    noise = 0.1
    x0, x = generatefid(N, N_line, noise)
    Mode = "sampling"  # "random"   "truncate"
    if Mode == "random":
        raise 'a faire'
    elif Mode == "truncate":
        # trans = functools.partial(trans_trunc, chsz=M)
        # ttrans = functools.partial(ttrans_trunc, chsz=N)
        # y1 = x[:M]  # y1 is the measure
        tr = SL0.transformations(N, M, debug=1)
        trans = tr.transform
        ttrans = tr.ttransform
        tr.check()
        y1 = x[0:M]  # y1 is the measure
        print(x.shape, y1.shape, y1.dtype)
    elif Mode == "sampling":
        tr = SL0.transformations(N, M, debug=0)
        tr.sampling = np.array(sorted(np.random.permutation(N)[:M]))
        trans = tr.transform
        ttrans = tr.ttransform
        tr.check()
        y1 = tr.sample(x)
        print("#########", x.shape, y1.shape, y1.dtype)
    plt.subplot(3, 1, 1)
    #    plt.plot(fft.fft(x))
    plt.title(
        'noisy original, %d lines, noise at %.1f %% of smallest peak, generated on %d points'
        % (N_line, 100 * noise, N))
    plt.plot(fft.fft(x))

    plt.subplot(3, 1, 2)
    xx = np.zeros(N, 'complex128')
    xx[:M] = x[:M] * np.hanning(M)
    plt.plot(np.abs(fft.fft(xx)))
    plt.title('best FT using %d points (truncated at %.1f %%)' %
              (M, 100.0 * M / N))

    plt.subplot(3, 1, 3)
    sigma_min = max(noise / 2, 0.0001)
    sdf = 0.9
    L = 2
    t0 = time.time()
    if Mode == "random":
        #        sol = SL0.SL0FT(A, y1, sigma_min, sdf, L)
        print("START")
        sol = SL0.SL0gene(trans, ttrans, y1, sigma_min, sdf, L)
    else:
        #        sol = SL0.SL0FT(A, y1, sigma_min, sdf, L, A_pinv=A.T, true_s=fft.fft(x0))
        print("START")
        sol = SL0.SL0gene(trans,
                          ttrans,
                          y1,
                          sigma_min,
                          sdf,
                          L,
                          true_s=fft.fft(x0))
    print(time.time() - t0, 'sec')
    plt.subplot(3, 1, 3)
    plt.plot(sol)
    plt.title(
        'SL0 inversion, using %d points (sampled at %.1f %% using %s mode)' %
        (M, 100.0 * M / N, Mode))
    plt.show()
示例#7
0
def test_FT(N_line=10):
    M = 300
    N = 5 * 1024
    noise = 0.0001
    Mode = "sampling"  # "random"   "truncate"
    if Mode == "random":
        A = np.random.randn(M, N)  # random superposition
    elif Mode == "truncate":
        A = build_sampling(N, M, trunc=True)  # trunacation
    elif Mode == "sampling":
        A = build_sampling(N, M)  # random sampling
    x0, x = generatefid(N, N_line, noise)

    # plt.subplot(3,1,1)
    # plt.plot(x)
    # mask=np.zeros(N)
    # mask[0]=2
    # mask[1]=-1
    # for i in range(M):
    #     j = np.random.randint(N)
    #     print j
    #     mask[j]=1.0
    # plt.subplot(3,1,2)
    # plt.plot(mask)
    # plt.subplot(3,1,3)
    # plt.plot(mask*x)
    # plt.show()
    # exit()

    plt.subplot(3, 1, 1)
    plt.plot(fft.fft(x))
    plt.title('noisy original, %d lines' % (N_line))
    y1 = np.dot(A, x)  # y1 is the measure

    plt.subplot(3, 1, 2)
    xx = np.zeros(N, 'complex128')
    xx[:M] = x[:M] * np.hanning(M)
    plt.plot(np.abs(fft.fft(xx)))

    plt.subplot(3, 1, 3)
    #    plt.plot(fft.fft(np.dot(A.T, y1)))
    #    plt.show()
    sigma_min = max(noise / 10, 0.0001)
    sdf = 0.9
    L = 2
    t0 = time.time()
    if Mode == "random":
        sol = SL0.SL0FT(A, y1, sigma_min, sdf, L)  #, A_pinv=(1./N)*A.T)
    else:
        sol = SL0.SL0FT(A,
                        y1,
                        sigma_min,
                        sdf,
                        L,
                        A_pinv=A.T,
                        true_s=fft.fft(x0))
    print(time.time() - t0, 'sec')
    plt.plot(sol)
    plt.title(
        'SL0 inversion, noise %.1f %% of smallest peak; sampled at %.1f %%' %
        (100 * noise, 100.0 * M / N))
    plt.show()