%pylab inline
Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['f', 'mod'] `%matplotlib` prevents importing * from pylab and numpy
f = 10 # Hz
time = 1 #seconds
sr = 44100
phs = linspace(0, 2* pi* f* time, time*sr)
plot(sin(phs))
[<matplotlib.lines.Line2D at 0x7fe5930342d0>]
f2 = 2
phs2 = linspace(0, 2* pi* f2* time, time*sr)
plot(sin(phs2))
[<matplotlib.lines.Line2D at 0x7fe592f60c50>]
out = sin(phs) * sin(phs2) # Ring modulation
plot(out)
[<matplotlib.lines.Line2D at 0x7fe592e9b1d0>]
out = sin(phs) * (1 + sin(phs2)) # Amplitude modulation
plot(out)
[<matplotlib.lines.Line2D at 0x7fe592dc6c50>]
phs = linspace(0, 2* pi* 30* time, 300, endpoint=False)
plot(sin(phs))
[<matplotlib.lines.Line2D at 0x7fe592d10790>]
phs2 = linspace(0, 2* pi* 3* time, 300, endpoint=False)
plot(sin(phs2))
[<matplotlib.lines.Line2D at 0x7fe592cb5950>]
out = sin(phs) * sin(phs2)
plot(out)
[<matplotlib.lines.Line2D at 0x7fe592bda7d0>]
plot(abs(fft.rfft(out)))
xlim((0, 40))
grid()
out = sin(phs) * (0.5 + sin(phs2))
plot(out)
[<matplotlib.lines.Line2D at 0x7fe592e71090>]
plot(abs(fft.rfft(out)))
xlim((0, 40))
grid()
f = 10 # Hz
time = 1 #seconds
sr = 44100
phs = linspace(0, 2* pi* f* time, time*sr)
plot(sin(phs))
[<matplotlib.lines.Line2D at 0x7fe592a0f2d0>]
Linear phase means constant frequency (frequency is the first derivative of phase).
It's simpler to do phase modulation (in ipython...):
fm = 2
mod = sin(linspace(0, 2 * pi * fm * time, time*sr))
mod_phs = phs + mod
plot(mod_phs)
[<matplotlib.lines.Line2D at 0x7fe5929e7890>]
mod_phs = phs + (2*pi*mod)
plot(mod_phs)
[<matplotlib.lines.Line2D at 0x7fe5929256d0>]
plot(sin(mod_phs))
[<matplotlib.lines.Line2D at 0x7fe592857dd0>]
plot(abs(rfft(sin(mod_phs))))
[<matplotlib.lines.Line2D at 0x7fe59279d110>]
plot(abs(rfft(sin(mod_phs))))
xlim((0, 30))
(0, 30)
f = 4410 # Hz
time = 1 #seconds
sr = 44100
phs = linspace(0, 2* pi* f * time, time*sr)
fm = 2205
mod = sin(linspace(0, 2 * pi * fm * time, time*sr))
mod_phs = phs + mod
plot(abs(rfft(sin(mod_phs))))
[<matplotlib.lines.Line2D at 0x7fe59282fcd0>]
plot(abs(rfft(sin(mod_phs))))
xlim((2000, 8000))
grid()
from JSAnimation import IPython_display
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 22050), ylim=(0,17000))
ax.grid()
line, = ax.plot([], [])
amp_text = ax.text(10000, 15000, "")
x = np.linspace(0, 22050, 22051)
phs = linspace(0, 2* pi* f, time*sr)
mod_amp = linspace(0, 4*pi, 100)
def init():
line.set_data([], [])
return line,
def animate(i):
mod_phs = phs + (mod_amp[i] * mod)
line.set_data(x, abs(rfft(sin(mod_phs))))
amp_text.set_text("mod_amp = %f"%mod_amp[i])
return line,
animation.FuncAnimation(fig, animate, init_func=init,
frames=len(mod_amp), interval=300, blit=True)
The ratio of the carrier and modulator frequencies is called the C:M ratio
To do frequency modulation, we need to work with phase increments per sample:
f = 10
phs_inc = ones((time*sr)) * 2*pi*f/(time*sr)
phs = cumsum(phs_inc)
plot(sin(phs))
[<matplotlib.lines.Line2D at 0x7fe590d76a50>]
... Left as an exercise to the reader ...
By: Andrés Cabrera mantaraya36@gmail.com
For Course MAT 240B at UCSB
This ipython notebook is licensed under the CC-BY-NC-SA license: http://creativecommons.org/licenses/by-nc-sa/4.0/