Skip to content

Instantly share code, notes, and snippets.

@endolith
Last active September 9, 2020 09:54
Show Gist options
  • Save endolith/990ad1d350c3eeec3a9d to your computer and use it in GitHub Desktop.
Save endolith/990ad1d350c3eeec3a9d to your computer and use it in GitHub Desktop.
Time derivative of phase spectrogram
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 03 2016
Show how the time derivative of phase spectrogram is constant, shows the
frequency offset within a bin
"""
from numpy import sin, linspace, pi, abs, angle, diff, unwrap
from numpy.random import randn
from scipy.signal.spectral import _spectral_helper
import matplotlib.pyplot as plt
fs = 100000
t = linspace(0, 1, fs)
f = 15000
# Slight frequency sweep, some noise to break up the phase in the quiet parts
sig = sin(2*pi*(f+100*t)*t) + randn(fs)*0.001
f, t, R = _spectral_helper(sig, sig, fs=fs, nperseg=512, noverlap=300,
mode='complex')
plt.subplot(3, 1, 1)
plt.imshow(abs(R), cmap='afmhot',
aspect='auto', origin='lower', extent=[t[0], t[-1], 0, fs/2])
plt.ylabel('Frequency [Hz]')
plt.title('Magnitude')
plt.subplot(3, 1, 2)
plt.imshow(angle(R), cmap='hsv',
aspect='auto', origin='lower', extent=[t[0], t[-1], 0, fs/2])
plt.xlabel('Time [s]')
plt.ylabel('Frequency [Hz]')
plt.title('Angle')
plt.subplot(3, 1, 3)
plt.imshow(diff(unwrap(angle(R), axis=1)), cmap='hsv',
aspect='auto', origin='lower', extent=[t[0], t[-1], 0, fs/2])
plt.xlabel('Time [s]')
plt.ylabel('Frequency [Hz]')
plt.title('Time derivative of angle')
plt.tight_layout()
@endolith
Copy link
Author

endolith commented Feb 4, 2016

derivative of phase is much more useful than plotting the phase spectrogram alone:

diff of phase spectrogram

@bzamecnik
Copy link

Actually, derivative of the phase spectrogram is one of the parts of time-frequency reassignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment