{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "EE120 Fall 2016 PS 8, Quadrature amplitude modulation for 4 channels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To ouput python notebook to printable format, use ``ipython nbconvert file.ipynb'' from command prompt. You may need to install pandoc first." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "print 4+5 # check to see if iPython is running..." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using matplotlib backend: Qt4Agg\n", "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import scipy as sp\n", "print 'imported numpy and scipy'\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from scipy.io import wavfile\n", "print 'numpy version is', np.__version__\n", "print 'finished importing'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Graphing helper function\n", "def setup_graph(title='', x_label='', y_label='', fig_size=None):\n", " fig = plt.figure()\n", " if fig_size != None:\n", " fig.set_size_inches(fig_size[0], fig_size[1])\n", " ax = fig.add_subplot(111)\n", " ax.set_title(title)\n", " ax.set_xlabel(x_label)\n", " ax.set_ylabel(y_label)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Write signal to .wav files after down sampling by 16 and scaling\n", "def wavwrite(name,sig,length):\n", " rate1 = 44100 # CD audio quality,\n", " out = np.zeros(length/16)\n", " out=sig.real[0:length:16] # downsample\n", " scale = 32767.0/(max(np.max(out),-np.min(out)))\n", " out = np.multiply(scale,out)\n", " wavfile.write(name, rate1, out.astype(np.int16)) # 16 bit integer\n", " print 'wrote file',name" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# import file\n", "from scipy.io import wavfile\n", "rate,data= wavfile.read('xmit-signal.wav') # 16 bit data from transmitter\n", "print 'rate =', rate\n", "print 'data =', data\n", "length = np.size(data)\n", "print 'length = ', length\n", "k = np.linspace(0,length-1,length)\n", "\n", "####################\n", "# parameters used:\n", "dt = 1.0/(44.1*16)*1e-3 # sample period\n", "print 'Ts =', dt\n", "time = dt * np.linspace(0,length-1,length)\n", "signal = np.zeros(length)\n", "omega_c = 2.0 * np.pi * 3e5\n", "omega_d = 2.0 * np.pi * 3.16e5 # use channel spacing of 16 kHz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Need to specify $k_{min}$, $k_{max}$, $k_{c}$, $k_{d}$ parameters in next cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "##################\n", "## PARAMETERS TO BE DETERMINED\n", "\n", "kc =1 # CHANGE THIS\n", "kd =2 # change this\n", "print 'k at wc=', kc\n", "print 'k at wd=', kd\n", "\n", "k_min = 0 # change this value\n", "k_max = 500000 # CHNAGE THIS VALUE" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# plot FFT of modulated signal near carrier frequency\n", "kcarrier = kc\n", "Y = np.fft.fft(data) \n", "setup_graph(title='FFT of input data', x_label='$k$', y_label='$Y[k]$', fig_size=(6,3))\n", "_ = plt.plot(k[kcarrier-k_min:kcarrier+k_max],Y[kcarrier-k_min:kcarrier+k_max])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Specify digital low pass filter $H[k]$ with cutoff of 8 kHz" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# specify low pass filter\n", "H = np.zeros(length)\n", "k_cutoff = 0 # CHANGE THIS\n", "print 'reconstruction k_cutoff =', k_cutoff\n", "# \n", "H[0:1]=1 ### CHANGE THIS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the next cell enter code for calculating recovered signals s1[n], s2[n], s3[n], s4[n]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# code here to extract s1....s4 from ``data''\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# this cell writes all data for you, and plots S1[k]\n", "wavwrite('sig1.wav',s1,length)\n", "wavwrite('sig2.wav',s2,length)\n", "wavwrite('sig3.wav',s3,length)\n", "wavwrite('sig4.wav',s4,length)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# plot FFT of s1\n", "Sig1 = np.fft.fft(s1) \n", "setup_graph(title='FFT of s1', x_label='$k$', y_label='$S1[k]$', fig_size=(12,3))\n", "plt.subplot(1,2,1)\n", "_ = plt.plot(k[0:2*k_cutoff],Sig1.real[0:2*k_cutoff],'b')\n", "ylabel('real')\n", "plt.subplot(1,2,2)\n", "_ = plt.plot(k[0:2*k_cutoff],Sig1.imag[0:2*k_cutoff],'g')\n", "ylabel('Imag')\n", "xlabel('k')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }