def test_renyi_information(self): """Check if Renyi entropy computation is correct.""" sig = atoms(128, np.array([[64., 0.25, 20., 1.]])) tfr, _, _ = WignerVilleDistribution(sig).run() R1 = pproc.renyi_information(tfr) sig = atoms(128, np.array([[32., 0.25, 20., 1.], [96., 0.25, 20., 1.]])) tfr, _, _ = WignerVilleDistribution(sig).run() R2 = pproc.renyi_information(tfr) self.assertAlmostEqual(R2 - R1, 0.98, places=1)
# # Copyright © 2015 jaidev <jaidev@newton> # # Distributed under terms of the MIT license. """ ============================================================ STFT of Gaussian Wave Packets with a Hamming Analysis Window ============================================================ This example demonstrates the construction of a signal containing two transient components, having the same Gaussian amplitude modulation and the same frequency, but different time centers. It also shows the effect of a Hamming window function when used with th STFT. Figure 3.7 from the tutorial. """ import numpy as np import matplotlib.pyplot as plt from tftb.generators import atoms from scipy.signal import hamming from tftb.processing.linear import ShortTimeFourierTransform coords = np.array([[45, .25, 32, 1], [85, .25, 32, 1]]) sig = atoms(128, coords) x = np.real(sig) window = hamming(65) stft = ShortTimeFourierTransform(sig, n_fbins=128, fwindow=window) stft.run() stft.plot(show_tf=True, cmap=plt.cm.gray)
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2015 jaidev <jaidev@newton> # # Distributed under terms of the MIT license. """ ================================================================================ Sampling Effects on the Wigner-Ville Distribution of a Real Valued Gaussian Atom ================================================================================ This example shows the Wigner-Ville distribution of a real valued Gaussian atom. If a signal is sampled at the Nyquist rate, the WVD is affected by spectral aliasing and many additional interferences. To fix this, either the signal may be oversampled, or an analytical signal may be used. Figure 4.6 from the tutorial. """ import numpy as np from tftb.generators import atoms from tftb.processing import WignerVilleDistribution x = np.array([[32, .15, 20, 1], [96, .32, 20, 1]]) g = atoms(128, x) spec = WignerVilleDistribution(np.real(g)) spec.run() spec.plot(kind="contour", show_tf=True, scale="log")
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2015 jaidev <jaidev@newton> # # Distributed under terms of the MIT license. """ Example from section 4.1.4 of the tutorial. """ import numpy as np from tftb.generators import atoms from tftb.processing import MargenauHillDistribution sig = atoms(128, np.array([[32, 0.15, 20, 1], [96, 0.32, 20, 1]])) tfr = MargenauHillDistribution(sig) tfr.run() tfr.plot(show_tf=True, kind='contour', sqmod=False, threshold=0, contour_y=np.linspace(0, 0.5, tfr.tfr.shape[0] / 2))
This example demonstrates the effect of frequency-dependent smoothing that is accomplished in a Morlet scalogram. Note that the localization at lower frequencies is much better. Figure 4.18 from the tutorial. """ from tftb.processing import Scalogram from tftb.generators import atoms import numpy as np from mpl_toolkits.axes_grid1 import make_axes_locatable import matplotlib.pyplot as plt sig = atoms(128, np.array([[38, 0.1, 32, 1], [96, 0.35, 32, 1]])) tfr, t, freqs, _ = Scalogram(sig, fmin=0.05, fmax=0.45, time_instants=np.arange(1, 129)).run() t, f = np.meshgrid(t, freqs) fig, axContour = plt.subplots() axContour.contour(t, f, tfr) axContour.grid(True) axContour.set_title("Morlet scalogram of complex sinusoids") axContour.set_ylabel('Frequency') axContour.yaxis.set_label_position('right') axContour.set_xlabel('Time') divider = make_axes_locatable(axContour)
# -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2015 jaidev <jaidev@newton> # # Distributed under terms of the MIT license. """ Examples showing Renyi information measurement. """ import numpy as np from scipy.io import loadmat from tftb.generators import atoms from tftb.processing import renyi_information from tftb.processing.cohen import WignerVilleDistribution sig = atoms(128, np.array([[64, 0.25, 20, 1]])) tfr, t, f = WignerVilleDistribution(sig).run() ideal = loadmat("/tmp/foo.mat") print(renyi_information(tfr, t, f)) # -0.2075 sig = atoms(128, np.array([[32, 0.25, 20, 1], [96, 0.25, 20, 1]])) tfr, t, f = WignerVilleDistribution(sig).run() print(renyi_information(tfr, t, f)) # 0.77 sig = atoms(128, np.array([[32, 0.15, 20, 1], [96, 0.15, 20, 1], [32, 0.35, 20, 1], [96, 0.35, 20, 1]])) tfr, t, f = WignerVilleDistribution(sig).run() print(renyi_information(tfr, t, f)) # 1.8029
condt1 = np.real(re_mat[:, icol]) - freqs == 0 condt2 = np.imag(re_mat[:, icol]) - icol == 0 condt3 = tfr[:, icol] > threshold indices = np.logical_and(condt1, condt2, condt3) if np.any(indices): time_points = np.ones((indices.sum(), )) * ti freq_points = np.arange( indices.shape[0])[indices] / (2.0 * n_fbins) elif method in ("rsp", "type1"): for icol in range(tfrcol): ti = timestamps[icol] condt1 = np.real(re_mat[:, icol]) - freqs == 0 condt2 = np.imag(re_mat[:, icol]) - icol == 0 condt3 = tfr[:, icol] > threshold indices = np.logical_and(condt1, condt2, condt3) if np.any(indices): time_points = np.ones((indices.sum(), )) * ti freq_points = np.arange(indices.shape[0])[indices] / n_fbins else: raise ValueError("Unknown time frequency representation.") return time_points, freq_points if __name__ == '__main__': from tftb.generators import atoms from tftb.processing import Spectrogram s = atoms(64, np.array([[32, 0.3, 16, 1]])) spec = Spectrogram(s) tfr, t, f = spec.run() print(renyi_information(tfr, t, f, 3))