{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Complex Ambiguity Function (CAF)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "from matplotlib import pylab\n", "from matplotlib.pyplot import plot, ylim, xlabel, ylabel, grid, savefig, imshow, contour\n", "import numpy as np\n", "from matplotlib.pyplot import psd\n", "pylab.rcParams['savefig.dpi'] = 300" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from caf_verilog import sim_helper as sh\n", "from caf_verilog.caf import simple_caf\n", "from gps_helper.prn import PRN\n", "from sk_dsp_comm import sigsys as ss\n", "from sk_dsp_comm import digitalcom as dc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prn = PRN(10)\n", "prn2 = PRN(20)\n", "fs = 625e3\n", "Ns = fs / 200e3\n", "prn_seq = prn.prn_seq()\n", "prn_seq = [*prn_seq, *prn_seq]\n", "prn_seq2 = prn2.prn_seq()\n", "prn_seq,b = ss.nrz_bits2(np.array(prn_seq), Ns)\n", "prn_seq2,b2 = ss.nrz_bits2(np.array(prn_seq2), Ns)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Px,f = psd(prn_seq, 2**12, Fs=fs)\n", "plot(f, 10*np.log10(Px))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f_size = 100\n", "foas = np.arange(-f_size, f_size + 1) * 1000\n", "foas, len(foas)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CAF Module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from caf_verilog.caf import CAF\n", "from caf_verilog.sim_helper import sim_shift\n", "from caf_verilog.xcorr import dot_xcorr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(prn_seq)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "center = 3000\n", "corr_length = len(foas) - 1\n", "shift = 15\n", "ncorr = np.arange(0, corr_length * 2)\n", "foa_offset = 16\n", "theta_shift = np.exp(1j*2*np.pi*ncorr*(foas[foa_offset])/float(fs))\n", "ref, rec = sim_shift(prn_seq, center, corr_length, shift=shift)\n", "caf = CAF(ref, rec * theta_shift, foas, fs=fs, n_bits=8, ref_i_bits=8, rec_i_bits=8)\n", "caf.gen_tb()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ref = np.array(ref)\n", "rec = np.array(rec)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xcorr_res_shift = dot_xcorr(ref, rec * theta_shift)\n", "xcorr_res = dot_xcorr(ref, rec)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot(rec.real)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot(np.abs(np.array(xcorr_res_shift)))\n", "ylim([0, corr_length])\n", "grid();\n", "xlabel(\"Inverse Center Offset (Samples)\")\n", "savefig('caf_test_shift.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "am_pos = np.argmax(abs(np.array(xcorr_res_shift)))\n", "am_pos, abs(np.array(xcorr_res))[am_pos]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot(abs(np.array(xcorr_res)))\n", "ylim([0, corr_length])\n", "grid();\n", "am_pos = np.argmax(abs(np.array(xcorr_res)))\n", "am_pos, abs(np.array(xcorr_res))[am_pos]\n", "xlabel(\"Inverse Center Offset (Samples)\")\n", "savefig('caf_test_signal.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CAF Surface Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from caf_verilog.caf import simple_caf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ref, rec = sim_shift(prn_seq, center, corr_length, shift=shift, freq_shift=foas[105], fs=fs, padding=True)\n", "ref = np.array(ref)\n", "rec = np.array(rec)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "caf_res, dt = simple_caf(ref, rec, foas=foas, fs=fs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "caf_res_np = np.array(caf_res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow(caf_res_np, interpolation='none', extent=[-corr_length, corr_length, corr_length, -corr_length])\n", "xlabel(\"N-th Sample\")\n", "ylabel(\"N-th Frequency Offset\")\n", "grid();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "contour(dt, foas / fs, caf_res_np, levels=int(np.ceil(np.log10(corr_length))))\n", "ylabel(\"Offset in Hz\")\n", "xlabel(\"Offset in Time (s)\")\n", "grid();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quanztization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from caf_verilog.quantizer import quantize\n", "from caf_verilog.caf import dot_caf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ref_dot, rec_dot = sim_shift(prn_seq, center, corr_length, shift=shift, freq_shift=foas[105], fs=fs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ref_quant = quantize(ref_dot, n_bits=8)\n", "rec_quant = quantize(rec_dot, n_bits=8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "caf_res_dot = dot_caf(ref_dot, rec_dot, foas=foas, fs=fs, n_bits=8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "caf_res_dot_np = np.array(caf_res_dot)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.shape(caf_res_dot_np)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow(np.abs(caf_res_dot_np), interpolation='none', extent=[corr_length, -corr_length, corr_length, -corr_length])\n", "xlabel(\"N-th Sample\")\n", "ylabel(\"N-th Frequency Offset\")\n", "grid();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "caf_res_dot_quant = dot_caf(ref_quant, rec_quant, foas=foas, fs=fs, n_bits=8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "caf_res_dot_quant_np = np.array(caf_res_dot_quant)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.shape(caf_res_dot_quant_np)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow(np.abs(caf_res_dot_quant_np), interpolation='none', extent=[corr_length, -corr_length, corr_length, -corr_length])\n", "xlabel(\"N-th Sample\")\n", "ylabel(\"N-th Frequency Offset\")\n", "grid();" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.18" } }, "nbformat": 4, "nbformat_minor": 4 }