def generate_dolph_chebyshev(lobe_fraction, tolerance): def cheb(m, x): if np.abs(x) <= 1: return np.cos(m * np.arccos(x)) else: return np.cosh(m * np.arccosh(np.abs(x))).real w = int((1 / np.pi) * (1 / lobe_fraction) * np.arccosh(1 / tolerance)) if w % 2 == 0: w -= 1 beta = np.cosh(np.arccosh(1 / tolerance) / float(w - 1)) print("lobe_fraction = {0}, tolerance = {1}, w = {2}, beta = {3}".format(lobe_fraction, tolerance, w, beta)) x = np.empty(w, dtype=np.complex128) for i in xrange(w): x[i] = cheb(w - 1, beta * np.cos(np.pi * i / w)) * tolerance x = fft(x, n=w) x = fftshift(x) x = np.real(x) return {"x": x, "w": w}
def generate_dolph_chebyshev(lobe_fraction, tolerance): def cheb(m, x): if np.abs(x) <= 1: return np.cos(m * np.arccos(x)) else: return np.cosh(m * np.arccosh(np.abs(x))).real w = int((1 / np.pi) * (1 / lobe_fraction) * np.arccosh(1 / tolerance)) if w % 2 == 0: w -= 1 beta = np.cosh(np.arccosh(1 / tolerance) / float(w - 1)) print('lobe_fraction = {0}, tolerance = {1}, w = {2}, beta = {3}'.format( lobe_fraction, tolerance, w, beta)) x = np.empty(w, dtype=np.complex128) for i in xrange(w): x[i] = cheb(w - 1, beta * np.cos(np.pi * i / w)) * tolerance x = fft(x, n=w) x = fftshift(x) x = np.real(x) return {'x': x, 'w': w}
def main(): code_1 = ca_code.generate(prn=1) | np.roll(ca_code.generate(prn=2), 100) code_2 = ca_code.generate(prn=1) # code_2 = np.roll(code_2, 100) # fftshift(ifft(fft(a,corrLength).*conj(fft(b,corrLength)))) q = 2 code_1 = sfft_aliasing.execute(code_1, q) code_2 = sfft_aliasing.execute(code_2, q) code_1_fft = fourier_transforms.fft(code_1) code_2_fft = fourier_transforms.fft(code_2) multiplied = code_1_fft * np.conj(code_2_fft) print multiplied.size params = Parameters( n=multiplied.size/2, k=1, B_k_location=2, B_k_estimation=2, estimation_loops=8, location_loops=5, loop_threshold=4, tolerance_location=1e-8, tolerance_estimation=1e-8 ) result = sfft_inverse.execute(params=params, x=multiplied) result = fourier_transforms.fftshift(result) result_actual = fourier_transforms.ifft(multiplied) result_actual = fourier_transforms.fftshift(result_actual) print 'sfft size', result.size print 'original size', result_actual.size fig = plt.figure() ax = fig.gca() ax.plot(np.linspace(-1, 1, result.size), np.abs(result)) ax.plot(np.linspace(-1, 1, result_actual.size), np.abs(result_actual)) plt.show()
def inner_loop_locate(x, n, filt, B, B_threshold, a, ai, b): assert n % B == 0 x_t_samp = np.zeros(B, dtype=np.complex128) # Permutate and dot product index = b for i in xrange(filt['size']): x_t_samp[i % B] += (x[index] * filt['time'][i]) index = (index + ai) % n x_samp = fft(x_t_samp, n=B) # samples = np.empty(B) # for i in xrange(B): # samples[i] = np.abs(x_samp[i]) # np.testing.assert_array_equal(np.abs(x_samp), samples) samples = np.empty(B) for i in xrange(B): # samples[i] = np.square(np.abs(x_samp[i])) samples[i] = x_samp[i].real*x_samp[i].real + x_samp[i].imag*x_samp[i].imag J = np.argsort(samples)[::-1][:B_threshold] np.testing.assert_array_equal(samples.take(J), np.sort(samples)[::-1][:B_threshold]) debug_inner_loop_locate( x=x, n=n, filt=filt, B_threshold=B_threshold, B=B, a=a, ai=ai, b=b, J=J, samples=samples ) return { 'x_samp': x_samp, 'J': J }
def inner_loop_locate(x, n, filt, B, B_threshold, a, ai, b): assert n % B == 0 x_t_samp = np.zeros(B, dtype=np.complex128) # Permutate and dot product index = b for i in xrange(filt['size']): x_t_samp[i % B] += (x[index] * filt['time'][i]) index = (index + ai) % n x_samp = fft(x_t_samp, n=B) # samples = np.empty(B) # for i in xrange(B): # samples[i] = np.abs(x_samp[i]) # np.testing.assert_array_equal(np.abs(x_samp), samples) samples = np.empty(B) for i in xrange(B): # samples[i] = np.square(np.abs(x_samp[i])) samples[i] = x_samp[i].real * x_samp[i].real + x_samp[i].imag * x_samp[ i].imag J = np.argsort(samples)[::-1][:B_threshold] np.testing.assert_array_equal(samples.take(J), np.sort(samples)[::-1][:B_threshold]) debug_inner_loop_locate(x=x, n=n, filt=filt, B_threshold=B_threshold, B=B, a=a, ai=ai, b=b, J=J, samples=samples) return {'x_samp': x_samp, 'J': J}
def make_multiple(x, w, n, b): print("w = {0}, n = {1}, b = {2}".format(w, n, b)) assert b <= n assert w <= n g = np.zeros(n, dtype=np.complex128) h = np.zeros(n, dtype=np.complex128) g[0 : w - (w / 2)] = x[(w / 2) :] g[n - (w / 2) :] = x[: (w / 2)] g = fft(g, n=n) s = 0 for i in xrange(b): s += g[i] max = 0 offset = int(b / 2) for i in xrange(n): h[(i + n + offset) % n] = s max = np.maximum(max, np.abs(s)) s += g[(i + b) % n] - g[i] h /= max offsetc = 1 step = np.exp(-2 * np.pi * 1j * (w / 2) / n) for i in xrange(n): h[i] *= offsetc offsetc *= step x = ifft(h, n=n) return {"time": x, "size": w, "freq": h}
def make_multiple(x, w, n, b): print('w = {0}, n = {1}, b = {2}'.format(w, n, b)) assert b <= n assert w <= n g = np.zeros(n, dtype=np.complex128) h = np.zeros(n, dtype=np.complex128) g[0:w - (w / 2)] = x[(w / 2):] g[n - (w / 2):] = x[:(w / 2)] g = fft(g, n=n) s = 0 for i in xrange(b): s += g[i] max = 0 offset = int(b / 2) for i in xrange(n): h[(i + n + offset) % n] = s max = np.maximum(max, np.abs(s)) s += (g[(i + b) % n] - g[i]) h /= max offsetc = 1 step = np.exp(-2 * np.pi * 1j * (w / 2) / n) for i in xrange(n): h[i] *= offsetc offsetc *= step x = ifft(h, n=n) return {'time': x, 'size': w, 'freq': h}
def debug_inner_loop_locate(x, n, filt, B_threshold, B, a, ai, b, J, samples): np.testing.assert_array_equal(samples.take(J), np.sort(samples)[::-1][:B_threshold]) x_f = np.empty(n, dtype=np.complex128) pxdotg = np.zeros(n, dtype=np.complex128) # pxdotgn = np.empty(n, dtype=np.complex128) # pxdotgw = np.empty(n, dtype=np.complex128) fft_large = np.array([]) fft_large_t = np.array([]) index = b for i in xrange(n): x_f[i] = x[index] index = (index + ai) % n w = filt['size'] pxdotg[:w] = x_f[:w] for i in xrange(w): pxdotg[i] *= filt['time'][i] print('Using {0}x ({1}^-1)'.format(a, ai)) x_f = fft(x_f, n=n) pxdotgn = fft(pxdotg, n=n) pxdotgw = fft(pxdotg, n=w) t_n = np.linspace(0, 1, num=n, endpoint=False) t_w = np.linspace(0, 1, num=w, endpoint=False) t_B = np.linspace(0, 1, num=B, endpoint=False) for i in xrange(B_threshold): np.testing.assert_approx_equal(J[i] / float(B), t_B[J[i]]) if J[i]: fft_large_t = np.append(fft_large_t, (J[i] - 0.1) / float(B)) fft_large = np.append(fft_large, 0) fft_large_t = np.append(fft_large_t, J[i] / float(B)) # fft_large_t = np.append(fft_large_t, t_B[J[i]]) # fft_large = np.append(fft_large, np.sqrt(samples[J[i]])) fft_large = np.append(fft_large, np.sqrt(samples[J[i]]) * n) # fft_large = np.append(fft_large, 1) if J[i] < B - 1: fft_large_t = np.append(fft_large_t, (J[i] + 0.1) / float(B)) fft_large = np.append(fft_large, 0) print fft_large_t print fft_large fig = plt.figure() ax = fig.gca() ax.plot(t_n, np.abs(pxdotgn) * n, '-x', t_w, np.abs(pxdotgw) * n, '-x', t_B, np.sqrt(samples) * n, '-x', fft_large_t, fft_large, '-x', t_n, np.abs(x_f), '-x') ax.legend(('n-dim convolved FFT', 'w-dim convolved FFT', 'sampled convolved FFT', 'largest in sample', 'true FFT'))
def generate_output(self): self.y = fourier_transforms.fft(self.x)
def debug_inner_loop_locate(x, n, filt, B_threshold, B, a, ai, b, J, samples): np.testing.assert_array_equal(samples.take(J), np.sort(samples)[::-1][:B_threshold]) x_f = np.empty(n, dtype=np.complex128) pxdotg = np.zeros(n, dtype=np.complex128) # pxdotgn = np.empty(n, dtype=np.complex128) # pxdotgw = np.empty(n, dtype=np.complex128) fft_large = np.array([]) fft_large_t = np.array([]) index = b for i in xrange(n): x_f[i] = x[index] index = (index + ai) % n w = filt['size'] pxdotg[:w] = x_f[:w] for i in xrange(w): pxdotg[i] *= filt['time'][i] print('Using {0}x ({1}^-1)'.format(a, ai)) x_f = fft(x_f, n=n) pxdotgn = fft(pxdotg, n=n) pxdotgw = fft(pxdotg, n=w) t_n = np.linspace(0, 1, num=n, endpoint=False) t_w = np.linspace(0, 1, num=w, endpoint=False) t_B = np.linspace(0, 1, num=B, endpoint=False) for i in xrange(B_threshold): np.testing.assert_approx_equal(J[i] / float(B), t_B[J[i]]) if J[i]: fft_large_t = np.append(fft_large_t, (J[i] - 0.1) / float(B)) fft_large = np.append(fft_large, 0) fft_large_t = np.append(fft_large_t, J[i] / float(B)) # fft_large_t = np.append(fft_large_t, t_B[J[i]]) # fft_large = np.append(fft_large, np.sqrt(samples[J[i]])) fft_large = np.append(fft_large, np.sqrt(samples[J[i]]) * n) # fft_large = np.append(fft_large, 1) if J[i] < B - 1: fft_large_t = np.append(fft_large_t, (J[i] + 0.1) / float(B)) fft_large = np.append(fft_large, 0) print fft_large_t print fft_large fig = plt.figure() ax = fig.gca() ax.plot( t_n, np.abs(pxdotgn) * n, '-x', t_w, np.abs(pxdotgw) * n, '-x', t_B, np.sqrt(samples) * n, '-x', fft_large_t, fft_large, '-x', t_n, np.abs(x_f), '-x' ) ax.legend( ( 'n-dim convolved FFT', 'w-dim convolved FFT', 'sampled convolved FFT', 'largest in sample', 'true FFT' ) )