Created
August 3, 2015 11:09
-
-
Save adamczykm/b065d34bce3febde56d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import pyqtgraph as pq | |
import numpy as np | |
import matplotlib.mlab as mlab | |
import pylab | |
from matplotlib.cm import Accent | |
def PlotCustomSpectrogram(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, | |
window=mlab.window_hanning, noverlap=128, | |
cmap=None, xextent=None, pad_to=None, sides='default', | |
scale_by_freq=None, minfreq = None, maxfreq = None, **kwargs): | |
""" | |
call signature:: | |
PlotCustomSpectrogram(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, | |
window=mlab.window_hanning, noverlap=128, | |
cmap=None, xextent=None, pad_to=None, sides='default', | |
scale_by_freq=None, minfreq = None, maxfreq = None, **kwargs) | |
Compute a spectrogram of data in *x*. Data are split into | |
*NFFT* length segments and the PSD of each section is | |
computed. The windowing function *window* is applied to each | |
segment, and the amount of overlap of each segment is | |
specified with *noverlap*. | |
%(PSD)s | |
*Fc*: integer | |
The center frequency of *x* (defaults to 0), which offsets | |
the y extents of the plot to reflect the frequency range used | |
when a signal is acquired and then filtered and downsampled to | |
baseband. | |
*cmap*: | |
A :class:`matplotlib.cm.Colormap` instance; if *None* use | |
default determined by rc | |
*xextent*: | |
The image extent along the x-axis. xextent = (xmin,xmax) | |
The default is (0,max(bins)), where bins is the return | |
value from :func:`mlab.specgram` | |
*minfreq, maxfreq* | |
Limits y-axis. Both required | |
*kwargs*: | |
Additional kwargs are passed on to imshow which makes the | |
specgram image | |
Return value is (*Pxx*, *freqs*, *bins*, *im*): | |
- *bins* are the time points the spectrogram is calculated over | |
- *freqs* is an array of frequencies | |
- *Pxx* is a len(times) x len(freqs) array of power | |
- *im* is a :class:`matplotlib.image.AxesImage` instance | |
Note: If *x* is real (i.e. non-complex), only the positive | |
spectrum is shown. If *x* is complex, both positive and | |
negative parts of the spectrum are shown. This can be | |
overridden using the *sides* keyword argument. | |
**Example:** | |
.. plot:: mpl_examples/pylab_examples/specgram_demo.py | |
""" | |
##################################### | |
# modified axes.specgram() to limit | |
# the frequencies plotted | |
##################################### | |
# this will fail if there isn't a current axis in the global scope | |
ax = pylab.gca() | |
Pxx, freqs, bins = mlab.specgram(x, NFFT, Fs, detrend, | |
window, noverlap, pad_to, sides, scale_by_freq) | |
# modified here | |
##################################### | |
if minfreq is None: | |
minfreq = 0.0 | |
if minfreq is not None and maxfreq is not None: | |
Pxx = Pxx[(freqs >= minfreq) & (freqs <= maxfreq)] | |
freqs = freqs[(freqs >= minfreq) & (freqs <= maxfreq)] | |
##################################### | |
Z = 10. * np.log10(Pxx) | |
Z = np.flipud(Z) | |
if xextent is None: xextent = 0, np.amax(bins) | |
xmin, xmax = xextent | |
freqs += Fc | |
extent = xmin, xmax, freqs[0], freqs[-1] | |
im = ax.imshow(Z, cmap, extent=extent, **kwargs) | |
ax.axis('auto') | |
return Pxx, freqs, bins, im | |
def readSpectro(filename): | |
df = pd.read_csv(filename) | |
sampling_rate = 125 | |
return PlotCustomSpectrogram(df.signal, | |
Fs=sampling_rate, | |
NFFT=10*sampling_rate, | |
noverlap=-20*sampling_rate, | |
cmap=Accent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment