{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Frequency Shift" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pylab inline\n", "pylab.rcParams['savefig.dpi'] = 300\n", "from caf_verilog.freq_shift import FreqShift" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fs = 625e3\n", "freq_res = 200\n", "n_bits = 8" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = np.arange(0,10e3)\n", "x = np.exp(2*np.pi*((50e3)/fs)*n*1j)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Px, ff = psd(x,2**10, fs)\n", "plot(ff, 10*np.log10(Px))\n", "xlim([0, 100e3])\n", "ylim([-80, 0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fq = FreqShift(x, freq_res, fs, n_bits)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fq.gen_tb(20e3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "max(fq.x_quant.real), min(fq.x_quant.real), max(fq.x_quant.imag), min(fq.x_quant.imag)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "max(x.real), min(x.real), max(x.imag), min(x.imag)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Px_in, ff_in = psd(fq.x_quant,2**10, fs)\n", "plot(ff_in, 10*np.log10(Px_in))\n", "xlim([0, 100e3])\n", "ylim([-80, 40])\n", "savefig('freq_shift_in.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generated Shifted Signal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from caf_verilog.io_helper import read_complex_output\n", "gss = read_complex_output(fq.test_output_filename)\n", "sss = read_complex_output(fq.submodules['sig_gen'].test_output_filename)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(len(gss), len(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert len(gss) == len(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(gss[:5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Px, ff = psd(sss,2**12, fs)\n", "plot(ff, 10*np.log10(Px))\n", "xlim([15000, 25000])\n", "ylim([-80, 20])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Px_out, ff_out = psd(gss,2**12, fs)\n", "plot(ff_out, 10*np.log10(Px_out))\n", "xlim([0, 100e3])\n", "savefig('freq_shift_out.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f, axarr = plt.subplots(2, sharex=True, gridspec_kw={'hspace': 0})\n", "axarr[0].plot(ff_in, 10*np.log10(Px_in))\n", "axarr[0].set_xlim([0, 100e3])\n", "axarr[0].set_ylim([-80, 40])\n", "axarr[1].plot(ff_out, 10*np.log10(Px_out))\n", "axarr[1].set_xlim([0, 100e3])\n", "axarr[1].set_xlabel('Frequency (Hz)')\n", "axarr[0].set_title('Power Spectral Density (dB/Hz)')\n", "f.savefig('freq_shift_inout.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generated Negative Shifted Signal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%mkdir -p ./neg_shift" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fqn = FreqShift(x, freq_res, fs, 8, neg_shift=True, output_dir='./neg_shift')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fqn.gen_tb(20000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "gssn = read_complex_output(os.path.join(fqn.output_dir, fqn.test_output_filename))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert len(gssn) == len(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sssn = []\n", "for ss in sss:\n", " sssn.append(ss.real + (ss.imag*-1j))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for ii in range(len(gssn)):\n", " check_res = fqn.x_quant[ii] * sssn[ii]\n", " check_i = int(check_res.real) >> 8\n", " assert gssn[ii].real == check_i\n", " check_q = int(check_res.imag) >> 8\n", " assert gssn[ii].imag == check_q" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Px, ff = psd(gssn,2**12, fs)\n", "plot(ff, 10*np.log10(Px))\n", "xlim([0, 100e3])\n", "ylim([-80, 40])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }