{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# The debug library - you can set a debug breakpoint with e.g.\n", "# pdb.set_trace()\n", "# and then examine the values of variables, types etc.\n", "import pdb\n", "\n", "def read_spectrum (filename): \n", "\n", " # Read in the data. It is binary format, unsigned 8 bit integers 'unit8'\n", " # With real value followed by imaginary value for each data point\n", " data = np.fromfile(filename, dtype='uint8')\n", "\n", " # This will be the number of distinct complex values, i.e. half the\n", " # number of total values.\n", " datasize = int(np.size(data)/2)\n", "\n", " # Create a numpy array (initially filled with zeros) to hold\n", " # the complex data \n", " dataz = np.zeros(datasize,dtype=complex)\n", "\n", " # Assigned the comlex values, using numpy indexing trickery.\n", " # real values start on 0 and step over 2\n", " # imag values start on 1 and step over 2\n", " dataz=data[0::2]+1j*data[1::2]\n", "\n", " # Calculate the mean of the data set and subtract\n", " offset=np.mean(dataz)\n", " dataz-=offset\n", "\n", " # The sampled time chunks in the data are 512 elements long\n", " # This will be the size of the fft we want to do\n", " fftsize=512;\n", "\n", " # nspec is the number of distinct time chunks in the time ordered data\n", " # This will also be the number of output spectra \n", " nspec=int(np.floor(datasize/fftsize))\n", " nsamp=nspec*fftsize\n", "\n", " # This makes a 2D array out of the 1-D data\n", " # with nspec rows and fftsize columns\n", "\n", " dataz= dataz.reshape((nspec,fftsize))\n", " # This calculates the fft, but only of the column axis (axis =1, fftsize long).\n", " fft_dataz= np.fft.fft(dataz,axis=1)\n", "\n", " # This reorders the fft data to that the 0 frequency channel is in the middle,\n", " # (FFT routines expect and return \"folded\" data ordering 0 to f, -f to 0)\n", " fft_dataz_shift= np.fft.fftshift(fft_dataz,axes=(1,))\n", "\n", " # This averages all the the individual 512 spectra together in the 2D array.\n", " # Notice we specify axis = 0 , the opposite axis to which the FFT was performed\n", " # over.\n", " spec = np.mean(np.abs(fft_dataz_shift),axis=0)\n", " \n", "\n", " # Calculate the corresponding frequency axis\n", " samprate=2.4e6;\n", " fcentre=1420.0e6;\n", " tau=1/samprate;\n", " fstep=1/(tau*fftsize);\n", " freq=fcentre+(fstep*(np.arange(0,fftsize)-fftsize/2));\n", "\n", " # return the freq axis and spectrum arrays\n", " return (freq,spec)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Students enter your code here." ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }