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)
For our purposes, we will just be using the discrete version...
Meaning:
In [3]:
freq = 1 #hz - cycles per second
amplitude = 3
time_to_plot = 2 # second
sample_rate = 100 # samples per second
num_samples = sample_rate * time_to_plot
t = linspace(0, time_to_plot, num_samples)
signal = [amplitude * sin(freq * i * 2*pi) for i in t] # Explain the 2*pi
In [4]:
setup_graph(x_label='time (in seconds)', y_label='amplitude', title='time domain')
plot(t, signal)
Out[4]:
In [5]:
fft_output = numpy.fft.rfft(signal)
magnitude_only = [sqrt(i.real**2 + i.imag**2)/len(fft_output) for i in fft_output]
frequencies = [(i*1.0/num_samples)*sample_rate for i in range(num_samples/2+1)]
In [6]:
setup_graph(x_label='frequency (in Hz)', y_label='amplitude', title='frequency domain')
plot(frequencies, magnitude_only, 'r')
Out[6]:
In [6]: