t0 = store['t0']
f0 = store['f0']
noise_sigma = store['noise_sigma']
m = len(y)

r = 1
n = 200#len(s)

filt = el.filtering.MatchedDoppler(s, n, m, xdtype=np.complex_)
A = radarmodel.point.Forward(s, n, m, r)
Astar = radarmodel.point.Adjoint(s, n, m, r)

# matched filter recovery
h_matched = filt(y)[:, filt.nodelay]

# compressed sensing recovery
x0 = np.zeros(A.inshape, A.indtype)
x1 = prx.l1rls(A, Astar, y, lmbda=.125, x0=x0, printrate=10)

x = x1/np.sqrt(n) + Astar(y - A(x1))*np.sqrt(n)

h_cs = x[:, Astar.delays >= 0]
    
range_idx = (t0 + np.arange(m)*ts)*sp.constants.c/2
velocity_idx = np.fft.fftfreq(int(n), ts)/f0*sp.constants.c/2

recovered = dict(h_matched=h_matched, h_cs=h_cs,
                 range_idx=range_idx, velocity_idx=velocity_idx)
with open('three_targets_recovered.pkl', 'wb') as f:
    cPickle.dump(recovered, f, protocol=-1)
        code_delay = 0
    
    delay = Astar.delays + code_delay
    nodelays.append(slice(np.searchsorted(delay, 0), np.searchsorted(delay, m)))

vlt_sig = np.zeros((data.vlt.shape[0], n, m), data.vlt.dtype)
vlt_noise = np.zeros_like(vlt_sig)
h = np.zeros_like(data.vlt.real)
h_sig = np.zeros_like(h)
h_noise = np.zeros_like(data.vlt)
x0s = [np.zeros(A.inshape, A.indtype) for A in As]
for p in xrange(data.vlt.shape[0]):
    y = data.vlt[p]
    A = As[p % len(As)]
    Astar = Astars[p % len(Astars)]
    x = prx.l1rls(A, Astar, y, lmbda=lmbda, x0=x0s[p % len(As)], printrate=100)
    
    nz = Astar(y - A(x))
    
    # matched filter result with sidelobes removed is vlt_sig + vlt_noise
    nodelayslc = nodelays[p % len(nodelays)]
    vlt_sig[p] = x[:, nodelayslc]/np.sqrt(n)
    vlt_noise[p] = nz[:, nodelayslc]*np.sqrt(n)
    
    h_sig[p] = np.sqrt(np.sum(vlt_sig[p].real**2 + vlt_sig[p].imag**2, axis=0))
    # use zero Doppler noise since by definition noise is wideband with no Doppler shift
    h_noise[p] = np.abs(vlt_noise[p, 0])
    # sqrt(n) factor included in noise term by summing n terms of nz[0]
    h[p] = np.sqrt(np.sum(np.abs(vlt_sig[p] + nz[0, nodelayslc])**2, axis=0))
    
    x0s[p % len(As)] = x