{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# EE 123 Lab1 - Time Domain Sonar Lab\n", "\n", "### Written by Miki Lustig and Frank Ong 2016\n", "#### Edited, debugged and ported to Rapberry Pi Nick Antipa, Li-Hao Yeh and Miki Lustig 2018" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this lab, we will interact with physical time-domain signals. The first part will involve generating and recording sounds on the Raspberry-Pi. We will use the chirp signal to characterize the response of the speaker-microphone system and look at detecting signals using cross-correlation.\n", "In the second part, we will build on part one and use the speaker-microphone system to develop a simple sonar.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### When using the raspberry pi -- \n", "\n", "The RaspberryPi does not have a microphone input. In order to use both audio input and output, we will use a USB soundcard. This USB soundcard is in fact and Analog-to-Digital and Digital-to-Analog device. \n", "\n", "* please connect the USB audio to the pi\n", "* connect the microphone to the mic input of the USB audio\n", "* connect the speaker to the speaker output of th USB audio\n", "* make sure the speaker is powered -- e.g. connected to a USB either on your computer, or the pi\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Import functions and libraries\n", "import numpy as np, matplotlib.pyplot as plt\n", "import threading,time, queue, pyaudio \n", "from matplotlib.pyplot import *\n", "import matplotlib.cm as cm\n", "from scipy import signal\n", "from numpy import *\n", "from threading import Lock\n", "\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 1: Chirping!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this assignment you will use the the raspberry-pi equipped with a USB sound card, a USB powered speaker and a microphone. When playing a sound and recording, the signal goes through several systems. In particular it goes through the response of the USB soundcard output, the speaker, the room we are in and the response of the microphone and receive part of the USB soundcard.\n", "\n", "A chirp is a a signal in which the frequency increases linearly with time. In this assignment we will generate a chirp signal and use it to measure the amplitude of the frequency response of our speaker-room-microphone system. This lab will work best in a quiet environment -- We recommend that you execute the lab at home or in a quiet place before submitting it. \n", "\n", "A simultaneous frequency is defined as the derivative of the phase of a signal, $f = \\frac{1}{2\\pi} \\frac{d\\phi (t)}{ dt} $. For example, the simultaneous frequency of $\\cos(\\phi(t))=\\cos(2\\pi f_0 t)$ is \n", "\n", "$$f = \\frac{d\\phi (t)}{2\\pi dt} = f_0$$ \n", "\n", "\n", "For a linear chirp, the frequency changes linearly over time. The simultaneous frequency is therefore defined as \n", "\n", "