Created
February 1, 2022 22:47
-
-
Save andres-fr/17a78a3e3c079843157757872178c8cd to your computer and use it in GitHub Desktop.
Interactive 3D plot of complex signals for visual inspection
This file contains hidden or 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
#!/usr/env/bin python | |
# -*- coding:utf-8 -*- | |
""" | |
Interactive plot for visual inspection: | |
Complex vectors are plotted as lines on a 3D space, such that 2 dimensions are | |
the real and imaginary components, and the third dimension goes through the | |
vector values (could be e.g. time or frequency). | |
Copyright 2021 aferro (ORCID: 0000-0003-3830-3595) | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from seaborn import color_palette as palette | |
# ############################################################################## | |
# SIGNAL PROCESSING | |
# ############################################################################## | |
def unit_spiral(n_samples, n_cycles=1): | |
""" | |
""" | |
x = np.linspace(0, 2 * np.pi * n_cycles, n_samples) | |
result = np.zeros(n_samples, dtype=np.complex128) | |
result.real = np.cos(x) | |
result.imag = np.sin(x) | |
return result | |
def dirac_delta(n_samples, idx): | |
""" | |
""" | |
result = np.zeros(n_samples) | |
result[idx] = 1 | |
return result | |
# ############################################################################## | |
# PLOTTER | |
# ############################################################################## | |
def plot_complex(*signals, domain=None, bg_color=(1, 1, 1, 0), | |
axis_alpha=0.1, signal_alpha=1.0, | |
axis_color="black"): | |
""" | |
""" | |
lengths = [len(s) for s in signals] | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection="3d") | |
ax.set_box_aspect((3, 1, 1)) # xy aspect ratio is 1:1, but stretches z | |
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) | |
if domain is not None: | |
ax.set_xlabel(domain) | |
ax.set_ylabel("real") | |
ax.set_zlabel("imaginary") | |
ax.set_xticks([], []) | |
ax.set_yticks([], []) | |
ax.set_zticks([], []) | |
ax.w_xaxis.set_pane_color(bg_color) | |
ax.w_yaxis.set_pane_color(bg_color) | |
ax.w_zaxis.set_pane_color(bg_color) | |
# Draw zero-axis and unit circle for reference | |
axis_line = np.zeros(max(lengths), dtype=np.float32) | |
ax.plot3D(range(len(axis_line)), axis_line, axis_line, color=axis_color, | |
alpha=axis_alpha) | |
# | |
unit_circle = unit_spiral(360, 1) | |
ax.plot3D(np.zeros(360), unit_circle.real, unit_circle.imag, | |
color=axis_color, alpha=axis_alpha) | |
# | |
scalar1 = np.linspace(0, 1, 5) | |
ax.plot3D(np.zeros(5), scalar1, np.zeros(5), color=axis_color, | |
alpha=axis_alpha) | |
# | |
for i, s in enumerate(signals): | |
color = palette("colorblind")[i] | |
ax.plot3D(range(len(s)), s.real, s.imag, color=color, | |
alpha=signal_alpha) | |
# | |
return fig | |
# ############################################################################## | |
# MAIN ROUTINE | |
# ############################################################################## | |
if __name__ == "__main__": | |
N = 1000 | |
a = dirac_delta(N, 0) | |
b = dirac_delta(N, 1) | |
c = dirac_delta(N, 2) | |
d = dirac_delta(N, 3) | |
A = np.fft.fft(a) | |
B = np.fft.fft(b) | |
C = np.fft.fft(c) | |
D = np.fft.fft(d) | |
fig = plot_complex(A, B, C, D, domain="freq") | |
fig.show() | |
# | |
import pdb; pdb.set_trace() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment