Show the code
import numpy as np
from matplotlib import pyplot as plt
import numpy as np
from matplotlib import pyplot as plt
### Define the properties of a sine wave
= 400 # Frequency
frequency = 0.01 # Duration of sound
duration = 1.0 # Amplitude
amplitude = 0.75 # Phase
phase = 22050 # Sampling rate (per second)
Fs
# This code creates the sine wave with the properties you detailed above
= int(Fs * duration)
num_samples = np.arange(num_samples) / Fs
t = amplitude * np.sin(2 * np.pi * (frequency * t - phase))
x = plt.subplots(figsize=(7.5, 2.75))
fig, ax ='red')
ax.plot(t, x, color'Time (s)')
ax.set_xlabel("Sine (400 Hz)")
ax.set_title('Air pressure deviation')
ax.set_ylabel(-1.05, 1.05])
ax.set_ylim([-1, 1.5, 1.0))
ax.set_yticks(np.arange(0.0, 0.01])
ax.set_xlim([0, 0.0125, 0.0025))
ax.set_xticks(np.arange(
ax.grid()'', xy=(0.0025, 0), xytext=(0.0025, 1),
ax.annotate(=dict(arrowstyle='<->', mutation_scale=15,
arrowprops='0.3'), size=2)
color0.0025, 0.5, "Amplitude", size=12,
ax.text(='0.3', ha="center", va="center")
color'', xy=(0, 1), xytext=(0.0025, 1),
ax.annotate(=dict(arrowstyle='<->', mutation_scale=19,
arrowprops='0.3'), size=2)
color0.00125, 0.85, "Period", size=12,
ax.text(='0.3', ha="center", va="center")
color
plt.show()
Let’s combine sine waves of different frequency (400, 600, 1600 Hz).
import numpy as np
from matplotlib import pyplot as plt
= plt.figure()
fig 6)
fig.set_figheight(10)
fig.set_figwidth(
= plt.subplot2grid(shape=(6, 3), loc=(0, 1), colspan=2, rowspan=3)
ax1 = plt.subplot2grid(shape=(6, 3), loc=(3, 1), colspan=2, rowspan=3)
ax3 = plt.subplot2grid(shape=(6, 3), loc=(3, 0), colspan=1)
ax2 = plt.subplot2grid(shape=(6, 3), loc=(4, 0), colspan=1)
ax4 = plt.subplot2grid(shape=(6, 3), loc=(5, 0), colspan=1)
ax5
= 400 # Frequency
frequency = 0.01 # Duration of sound
duration = 1.0 # Amplitude
amplitude = 0.75 # Phase
phase = 22050 # Sampling rate (per second)
Fs
= int(Fs * duration)
num_samples = np.arange(num_samples) / Fs
t = amplitude * np.sin(2 * np.pi * (frequency * t - phase))
x
='red', linewidth=2.0, linestyle='-')
ax1.plot(t, x, color'Time (s)')
ax1.set_xlabel("Sine (400 Hz)")
ax1.set_title('Air pressure deviation')
ax1.set_ylabel(-1.05, 1.05])
ax1.set_ylim([-1, 1.5, 1.0))
ax1.set_yticks(np.arange(0.0, 0.01])
ax1.set_xlim([0, 0.0125, 0.0025))
ax1.set_xticks(np.arange(
ax1.grid()
'', xy=(0.0025, 0), xytext=(0.0025, 1),
ax1.annotate(=dict(arrowstyle='<->',
arrowprops=15, color='0.3'), size=2)
mutation_scale0.0025, 0.5, "Amplitude", size=12, color='0.3',
ax1.text(="center", va="center")
ha'', xy=(0, 1), xytext=(0.0025, 1),
ax1.annotate(=dict(arrowstyle='<->', mutation_scale=19,
arrowprops='0.3'), size=2)
color0.00125, 0.85, "Period", size=12,
ax1.text(='0.3', ha="center", va="center")
color
# Combine several sine waves (here are three frequencies)
= 400
frequency1 = 600
frequency2 = 1600
frequency3 = 0.01
duration = 1.0
amplitude = 0.75
phase = 20050
Fs
= int(Fs * duration)
num_samples = np.arange(num_samples) / Fs
t = amplitude * np.sin(2 * np.pi * (frequency1 * t - phase)) # 1st sine
x1 = amplitude * np.sin(2 * np.pi * (frequency2 * t - phase)) # 2nd sine
x2 = amplitude * np.sin(2 * np.pi * (frequency3 * t - phase)) # 3rd sine
x3
='red')
ax2.plot(t, x1, color='red')
ax4.plot(t, x2, color='red')
ax5.plot(t, x3, color
"400 Hz")
ax2.set_title("600 Hz")
ax4.set_title("1600 Hz")
ax5.set_title(
0, 0.0125, 0.0025))
ax2.set_xticks(np.arange(0.0, 0.01])
ax2.set_xlim([-1, 1.5, 1.0))
ax2.set_yticks(np.arange(
0, 0.0125, 0.0025))
ax4.set_xticks(np.arange(0.0, 0.01])
ax4.set_xlim([-1, 1.5, 1.0))
ax4.set_yticks(np.arange(
0, 0.0125, 0.0025))
ax5.set_xticks(np.arange(0.0, 0.01])
ax5.set_xlim([-1, 1.5, 1.0))
ax5.set_yticks(np.arange(
=.001, wspace=0.5)
fig.subplots_adjust(hspace
# Combine all three (sum and divide by 3 to keep the amplitude as original)
= (x1+x2+x3)/3
x123
='blue', linewidth=2.0, linestyle='-')
ax3.plot(t, x123, color'Time (s)')
ax3.set_xlabel("Complex tone (sines of 400 Hz + 600 Hz + 1600 Hz)")
ax3.set_title('')
ax3.set_ylabel(-1.01, 1.01])
ax3.set_ylim([0, 0.01])
ax3.set_xlim([0, 0.0125, 0.0025))
ax3.set_xticks(np.arange(-1, 1.5, 1.0))
ax3.set_yticks(np.arange(
ax3.grid()
fig.tight_layout()
'', xy=(1.11/100, -9.3), xytext=(1.01/100, 0),
ax2.annotate(=dict(width=0.5, headlength=3, headwidth=3,
arrowprops='0.3'), size=2, annotation_clip=False)
color'', xy=(1.063/100, 0), xytext=(1.01/100, 0),
ax4.annotate(=dict(width=0.5, headlength=3, headwidth=3,
arrowprops='0.3'), size=2, annotation_clip=False)
color'', xy=(1.11/100, 9.3), xytext=(1.01/100, 0),
ax5.annotate(=dict(width=0.5, headlength=3, headwidth=3,
arrowprops='0.3'), size=2, annotation_clip=False)
color1.09/100, -0.6, r'$\sum$', size=9, backgroundcolor='0.8')
ax4.text(
plt.show()