{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Lab2 - Time Frequency\n",
"
\n",
"originally designed by John Pauly, modified and extended by Michael Lustig, Translated to python by Frank Ong\n",
"\n",
"This week we will look at the processing and spectrum of time-varying signals. In the first part of the lab we will look at the short-time fourier transform and spectrograms. We will use this to analyze audio signals and broadcast FM. In the second we will look at frequency offset correction of the SDR using GSM cellphone basestation signals. In the last part we will look at generating digital packets using frequency modulation. "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Import general functions and libraries\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import pyaudio\n",
"from numpy import pi\n",
"from numpy import sin\n",
"from numpy import zeros\n",
"from numpy import ones\n",
"from numpy import r_\n",
"from scipy import signal\n",
"\n",
"from numpy import hanning\n",
"from numpy import outer\n",
"\n",
"from rtlsdr import RtlSdr\n",
"from numpy import mean\n",
"from numpy import power\n",
"from numpy.fft import fft\n",
"from numpy.fft import fftshift\n",
"from numpy.fft import ifft\n",
"from numpy.fft import ifftshift\n",
"\n",
"from numpy import isreal\n",
"\n",
"#Task VI\n",
"\n",
"%matplotlib inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part I: Spectrograms\n",
"In class we used the DFT to estimate the frequency of a segment of a signal. When we do this, we are implicitly assuming that the frequency is constant over that interval. This is a reasonable assumption for some signals, especially for a very short time windows. \n",
" \n",
"There are many times when we'd like analyze signals whose frequency is changing over time. In fact, most signals aren't interesting unless they do change! There are many examples, including speech, music, and the sounds that surround you in daily life. In this lab we will learn how to process these signals to determine how their spectrum changes with time.\n",
"\n",
"The basic problem is that we have long segment of a signal $x[n]$, where $n=0, ... ,N-1$. We want to know what its frequency is as a function of time. There are two basic approaches. One is to pass the signal through a bank of bandpass filters, and plot the outputs of the filters as a function of time. \n",
"\n",
"\n",
"The second approach is based on the short-time Fourier transform and is to break the signal up into short segments, and compute the spectrum of each of these separately. This is the approach we will use in this lab.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A sample signal is shown in Figure 1. You've seen this signal during the class. Obviously you can tell that the amplitude is changing as a function of time. When you examine the signal closely, you can also tell that the frequency is changing.\n",
"Since the frequency is changing, we want to break the signal into segments over which the frequency is relatively constant. Then we will analyze each segment using the DFT. The result of analyzing the first 256 samples, and another block of 256 samples that is 1024 samples later is shown in Figure 2. As is the convention with the DFT, zero frequency is the first sample, and the Nyquist rate corresponds to the middle sample. The sampling rate in this example was 8192Hz, so the maximum frequency is 4096Hz. Note that the frequency has dropped about 300 Hz between the first and second block."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"