Skip to content

Instantly share code, notes, and snippets.

@Holzhaus
Created March 23, 2021 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Holzhaus/46ab8ea32d3b89025a0a640db171a14b to your computer and use it in GitHub Desktop.
Save Holzhaus/46ab8ea32d3b89025a0a640db171a14b to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plot
def analog(subplot):
# Get x values of the sine wave
time = np.arange(0, 10, 0.001)
# Amplitude of the sine wave is sine of a variable like time
amplitude = np.sin(time)
# Plot a sine wave using time and amplitude obtained for the sine wave
subplot.plot(time, amplitude)
# Give a title for the sine wave plot
subplot.set_title('Analog Signal')
def value_discrete(subplot):
# Get x values of the sine wave
time = np.arange(0, 10, 0.001)
# Amplitude of the sine wave is sine of a variable like time
amplitude = np.sin(time)
amplitude_quantized = [(round(x*5)/5) for x in amplitude]
# Plot a sine wave using time and amplitude obtained for the sine wave
subplot.plot(time, amplitude_quantized)
# Give a title for the sine wave plot
subplot.set_title('Time-Continuous Value-Discrete Signal')
def time_discrete(subplot):
# Get x values of the sine wave
time = np.arange(0, 10.2, 0.2)
# Amplitude of the sine wave is sine of a variable like time
amplitude = np.sin(time)
# Plot a sine wave using time and amplitude obtained for the sine wave
subplot.stem(time, amplitude)
# Give a title for the sine wave plot
subplot.set_title('Time-Discrete Value-Continuous Signal')
def digital(subplot):
# Get x values of the sine wave
time = np.arange(0, 10.2, 0.2)
# Amplitude of the sine wave is sine of a variable like time
amplitude = np.sin(time)
amplitude_quantized = [(round(x*5)/5) for x in amplitude]
# Plot a sine wave using time and amplitude obtained for the sine wave
subplot.stem(time, amplitude_quantized)
# Give a title for the sine wave plot
subplot.set_title('Digital Signal')
# Use 'Mixxx' color scheme
plot.rcParams['axes.prop_cycle'] = plot.cycler(color=['#dc5d1e'])
for param, value in plot.rcParams.items():
if 'color' in param and value in ('black', 'white'):
plot.rcParams[param] = 'white' if value == 'black' else 'black'
figure = plot.figure(figsize=(20, 9))
axis = figure.subplots(2, 2)
for subplot, func in ((axis[0, 0], analog), (axis[0, 1], value_discrete), (axis[1, 0], time_discrete), (axis[1, 1], digital)):
func(subplot)
# Give x axis label for the sine wave plot
subplot.set_xlabel('Time')
subplot.set_xlim(left=0, right=10)
subplot.set_xticks(np.arange(0, 11, 1))
# Give y axis label for the sine wave plot
subplot.set_ylabel('Amplitude')
subplot.set_yticks(np.arange(-1, 1.2, 0.2))
subplot.grid(True, which='both', color='#404040', linestyle='dashed')
subplot.axhline(y=0, color='white')
plot.savefig('analog_digital.svg', transparent=True)
plot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment