{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Spectrogram\n\nPlotting a spectrogram using `~.Axes.specgram`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\ndt = 0.0005\nt = np.arange(0.0, 20.5, dt)\ns1 = np.sin(2 * np.pi * 100 * t)\ns2 = 2 * np.sin(2 * np.pi * 400 * t)\n\n# create a transient \"chirp\"\ns2[t <= 10] = s2[12 <= t] = 0\n\n# add some noise into the mix\nnse = 0.01 * np.random.random(size=len(t))\n\nx = s1 + s2 + nse # the signal\nNFFT = 1024 # the length of the windowing segments\nFs = 1/dt # the sampling frequency\n\nfig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)\nax1.plot(t, x)\nax1.set_ylabel('Signal')\n\nPxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs)\n# The `specgram` method returns 4 objects. They are:\n# - Pxx: the periodogram\n# - freqs: the frequency vector\n# - bins: the centers of the time bins\n# - im: the .image.AxesImage instance representing the data in the plot\nax2.set_xlabel('Time (s)')\nax2.set_ylabel('Frequency (Hz)')\nax2.set_xlim(0, 20)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.specgram` / `matplotlib.pyplot.specgram`\n\n" ] } ], "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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }