\n",
" \n",
" * Distributed under the terms of the GPL License\n",
" * Maintainer: dcline@mbari.org\n",
" * Authors: Danelle Cline dcline@mbari.org, John Ryan ryjo@mbari.org"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fpirgMqVxy1j"
},
"source": [
"## Basic Exploration of the 256 kHz Pacific Ocean Audio Data in the AWS Open Data Registry\n",
"\n",
"---\n",
"An extensive (5+ years and growing) archive of sound recordings from a deep-sea location [along the eastern margin of the North Pacific Ocean](https://www.mbari.org/at-sea/cabled-observatory/) has been made available through AWS Open data. Temporal coverage of the recording archive has been 95% since project inception in July 2015. The original recordings have a sample rate of 256 kHz. This notebook illustrates basic methods to access and process the original audio data using Python.\n",
"\n",
"If you use this data set, please **[cite our project](https://ieeexplore.ieee.org/document/7761363).**\n",
"\n",
"
\n",
"
A delayed version of this data can be heard here on this live audio station
\n",
" \n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zKFvjnD-xy1j"
},
"source": [
"## Data Overview\n",
"The full-resolution audio data are in [WAV](https://en.wikipedia.org/wiki/WAV) format in s3 buckets named pacific-sound-256khz-yyyy, where yyyy is 2015 or later. Buckets are stored as objects, so the data aren't physically stored in folders or directories as you may be famaliar with, but you can think of it conceptually as follows:\n",
"\n",
"```\n",
"pacific-sound-256khz-2021\n",
" |\n",
" individual 10-minute files\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b7NrfPluxy1j"
},
"source": [
"## Install required dependencies\n",
"\n",
"First, let's install the required software dependencies. \n",
"\n",
"If you are using this notebook in a cloud environment, select a Python3 compatible kernel and run this next section. This only needs to be done once for the duration of this notebook.\n",
"\n",
"If you are working on local computer, you can skip this next cell. Change your kernel to *pacific-sound-notebooks*, which you installed according to the instructions in the [README](https://github.com/mbari-org/pacific-sound-notebooks/) - this has all the dependencies that are needed. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "jdAnv62Jxy1j",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f751c972-6c79-4e5a-f079-3922c9c4f924"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[K |████████████████████████████████| 132 kB 15.0 MB/s \n",
"\u001b[K |████████████████████████████████| 79 kB 8.4 MB/s \n",
"\u001b[K |████████████████████████████████| 9.2 MB 73.3 MB/s \n",
"\u001b[K |████████████████████████████████| 140 kB 82.5 MB/s \n",
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
"requests 2.23.0 requires urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1, but you have urllib3 1.26.12 which is incompatible.\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install -q boto3\n",
"!pip install -q soundfile\n",
"!pip install -q scipy\n",
"!pip install -q numpy\n",
"!pip install -q matplotlib"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XiZOH_WHxy1k"
},
"source": [
"### Import all packages"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "CQ2a2Rq2xy1k"
},
"outputs": [],
"source": [
"import boto3\n",
"from botocore import UNSIGNED\n",
"from botocore.client import Config\n",
"from six.moves.urllib.request import urlopen\n",
"import io\n",
"import scipy\n",
"from scipy import signal, interpolate\n",
"import numpy as np\n",
"import soundfile as sf\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uYx5C7MHxy1k"
},
"source": [
"## List the contents of a monthly directory"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "Us6lj9anxy1k"
},
"outputs": [],
"source": [
"s3 = boto3.client('s3',\n",
" aws_access_key_id='',\n",
" aws_secret_access_key='', \n",
" config=Config(signature_version=UNSIGNED))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "TTQs4wqTxy1k",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "9a28a2a3-9b6c-4db8-b4ea-fe170c54c792"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"01/MARS_20180101_000406.wav\n",
"01/MARS_20180101_001406.wav\n",
"01/MARS_20180101_002406.wav\n",
"01/MARS_20180101_003406.wav\n",
"01/MARS_20180101_004406.wav\n",
"01/MARS_20180101_005406.wav\n",
"01/MARS_20180101_010406.wav\n",
"01/MARS_20180101_011406.wav\n",
"01/MARS_20180101_012406.wav\n",
"01/MARS_20180101_013406.wav\n",
"01/MARS_20180101_014406.wav\n",
"01/MARS_20180101_015406.wav\n",
"01/MARS_20180101_020406.wav\n",
"01/MARS_20180101_021406.wav\n",
"01/MARS_20180101_022406.wav\n",
"01/MARS_20180101_023406.wav\n",
"01/MARS_20180101_024406.wav\n",
"01/MARS_20180101_025406.wav\n",
"01/MARS_20180101_030406.wav\n",
"01/MARS_20180101_031406.wav\n",
"01/MARS_20180101_032406.wav\n",
"01/MARS_20180101_033406.wav\n"
]
}
],
"source": [
"bucket = 'pacific-sound-256khz-2018'\n",
"\n",
"for i, obj in enumerate(s3.list_objects_v2(Bucket=bucket)['Contents']):\n",
" print(obj['Key'])\n",
" if i > 20:\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-NqKzJm7xy1k"
},
"source": [
"## Read metadata from a file"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "YPGuS-I8xy1k",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2aec1265-c393-4cf7-81ff-deb519abedcc"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Reading metadata from https://pacific-sound-256khz-2018.s3.amazonaws.com/01/MARS_20180101_092406.wav\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<_io.BytesIO object at 0x7fe050a21710>\n",
"samplerate: 256000 Hz\n",
"channels: 1\n",
"duration: 1e+01:0.000 min\n",
"format: WAV (Microsoft) [WAV]\n",
"subtype: Signed 24 bit PCM [PCM_24]\n",
"endian: FILE\n",
"sections: 1\n",
"frames: 153600000\n",
"extra_info: \"\"\"\n",
" Length : 460800356\n",
" RIFF : 460800348\n",
" WAVE\n",
" LIST : 304\n",
" INFO\n",
" IART : icListen HF #1689\n",
" IPRD : RB9-ETH R4\n",
" ICRD : 2018-01-01T01:24:06-08\n",
" ISFT : Lucy V4.3.0\n",
" INAM : MARS_20180101_092406.wav\n",
" ICMT : 3.000000 V pk, -177 dBV re 1uPa, 38.6 % RH, 6.1 deg C, 8388608 = Max Count\n",
" fmt : 16\n",
" Format : 0x1 => WAVE_FORMAT_PCM\n",
" Channels : 1\n",
" Sample Rate : 256000\n",
" Block Align : 3\n",
" Bit Width : 24\n",
" Bytes/sec : 768000\n",
" data : 460800000\n",
" End\n",
" \"\"\""
]
},
"metadata": {},
"execution_count": 5
}
],
"source": [
"bucket = 'pacific-sound-256khz-2018'\n",
"filename = '01/MARS_20180101_092406.wav'\n",
"url = f'https://{bucket}.s3.amazonaws.com/{filename}'\n",
"print(f'Reading metadata from {url}')\n",
"sf.info(io.BytesIO(urlopen(url).read()), verbose=True) "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "x2K0pt0gxy1k"
},
"source": [
"## Read data from a file"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "lxdjGRc_xy1l",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "745ea691-846c-4876-8ad3-23c36c61ffdd"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Reading data from https://pacific-sound-256khz-2018.s3.amazonaws.com/01/MARS_20180101_092406.wav\n"
]
}
],
"source": [
"print(f'Reading data from {url}')\n",
"x, sample_rate = sf.read(io.BytesIO(urlopen(url).read()),dtype='float32')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HnQfUH3_xy1l"
},
"source": [
"### Calibrated Spectrum Levels"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-40IJ3g1xy1l"
},
"source": [
"### Calibration metadata\n",
"Frequency-dependent hydrophone sensitivity data are defined in the following files, one for each deployment:\n",
"* https://bitbucket.org/mbari/pacific-sound/src/master/MBARI_MARS_Hydrophone_Deployment01.json\n",
"* https://bitbucket.org/mbari/pacific-sound/src/master/MBARI_MARS_Hydrophone_Deployment02.json"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yDhLi0r7xy1l"
},
"source": [
"### Compute spectrogram\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "PqZfCBTGxy1m",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "a7820d98-482f-4947-94e9-b3d25b922c98"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"600 segments of length 1 seconds in 600.0 seconds of audio\n"
]
}
],
"source": [
"# convert scaled voltage to volts\n",
"v = x*3 \n",
"nsec = (v.size)/sample_rate # number of seconds in vector\n",
"spa = 1 # seconds per average\n",
"nseg = int(nsec/spa)\n",
"print(f'{nseg} segments of length {spa} seconds in {nsec} seconds of audio')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "I2wUsQqtxy1m",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "dc5cea9c-13e4-44c0-c926-6b89fa10dd12"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(128001, 600)"
]
},
"metadata": {},
"execution_count": 8
}
],
"source": [
"# initialize empty LTSA\n",
"nfreq = int(sample_rate/2+1)\n",
"nfreq,nseg\n",
"sg = np.empty((nfreq, nseg), float)\n",
"sg.shape"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "phr-wXexxy1m",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "793c312b-a617-4859-8a33-b1ed9180365b"
},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:9: RuntimeWarning: divide by zero encountered in log10\n",
" if __name__ == '__main__':\n"
]
}
],
"source": [
"# get window for welch\n",
"w = scipy.signal.get_window('hann',sample_rate)\n",
"\n",
"# process spectrogram\n",
"for x in range(0,nseg):\n",
" cstart = x*spa*sample_rate\n",
" cend = (x+1)*spa*sample_rate\n",
" f,psd = scipy.signal.welch(v[cstart:cend],fs=sample_rate,window=w,nfft=sample_rate)\n",
" psd = 10*np.log10(psd)\n",
" sg[:,x] = psd"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "roSC9Ynfxy1m"
},
"source": [
"### Apply calibration\n",
"Frequency-dependent hydrophone sensitivity data are reported in the json files identified above. This example file is from the second hydrophone deployment, for which the calibration data are manually entered below. Note that the lowest measured value, at 250 Hz, is assumed to cover lower frequencies and repeated as a value at 0 Hz to allow interpolation to the spectrogram output frequencies across the full frequency range. \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "zdgOWhedxy1m",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"outputId": "c6c77c29-bb6c-416b-f7f5-572c623f768a"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
""
]
},
"metadata": {},
"execution_count": 10
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"