In [1]:
# Graphing helper function
def setup_graph(title='', x_label='', y_label='', fig_size=None):
fig = plt.figure()
if fig_size != None:
fig.set_size_inches(fig_size[0], fig_size[1])
ax = fig.add_subplot(111)
ax.set_title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
In [27]:
t = linspace(0, 3, 200)
freq_1hz_amp_10 = 10 * sin(1 * 2*pi*t)
freq_3hz_amp_5 = 5 * sin(3 * 2*pi*t)
complex_wave = freq_1hz_amp_10 + freq_3hz_amp_5
setup_graph(x_label='time (in seconds)', y_label='amplitude', title='original wave', fig_size=(12,6))
_ = plot(t, complex_wave)
In [28]:
freq_1hz = sin(1 * 2*pi*t)
setup_graph(x_label='time (in seconds)', y_label='amplitude', title='original wave * 1Hz wave', fig_size=(12,6))
_ = plot(t, complex_wave * freq_1hz)
In [29]:
sum(complex_wave*freq_1hz)
Out[29]:
In [30]:
print "Amplitude of 1hz component: ", sum(complex_wave*freq_1hz) * 2.0 * 1.0/len(complex_wave)
In [31]:
freq_3hz = sin(3 * 2*pi*t)
setup_graph(x_label='time (in seconds)', y_label='amplitude', title='complex wave * 3Hz wave', fig_size=(12,6))
_ = plot(t, complex_wave * freq_3hz)
In [32]:
sum(complex_wave*freq_3hz)
Out[32]:
In [33]:
print "Amplitude of 3hz component: ", sum(complex_wave*freq_3hz) * 2.0/len(complex_wave)
In [34]:
freq_2hz = sin(2 * 2*pi*t)
setup_graph(x_label='time (in seconds)', y_label='amplitude', title='complex wave * 2Hz wave', fig_size=(12,6))
_ = plot(t, complex_wave * freq_2hz)
In [35]:
sum(complex_wave*freq_2hz)
Out[35]:
In [36]:
# Very close to 0
print "Amplitude of 3hz component: ", sum(complex_wave*freq_2hz) * 2.0/len(complex_wave)
In [37]:
# Same with 4Hz - close to 0
freq_4hz = sin(4 * 2*pi*t)
sum(complex_wave*freq_4hz)
Out[37]: