In [15]:
# 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 [16]:
sample_rate = 10 # samples per sec
total_sampling_time = 3
num_samples = sample_rate * total_sampling_time
t = linspace(0, total_sampling_time, num_samples)
# between x = 0 and x = 1, a complete revolution (2 pi) has been made, so this is
# a 1 Hz signal with an amplitude of 5
frequency_in_hz = 1
wave_amplitude = 5
f = lambda x: wave_amplitude * sin(frequency_in_hz * 2*pi*x)
sampled_f = [f(i) for i in t]
In [17]:
setup_graph(title='time domain', x_label='time (in seconds)', y_label='amplitude', fig_size=(12,6))
_ = plot(t, sampled_f)
In [18]:
fft_output = numpy.fft.fft(sampled_f)
In [19]:
setup_graph(title='FFT output', x_label='FFT bins', y_label='amplitude', fig_size=(12,6))
_ = plot(fft_output)
In [20]:
rfft_output = numpy.fft.rfft(sampled_f)
In [21]:
setup_graph(title='rFFT output', x_label='frequency (in Hz)', y_label='amplitude', fig_size=(12,6))
_ = plot(rfft_output)
In our DFT equation $$G(\frac{n}{N}) = \sum_{k=0}^{N-1} g(k) e^{-i 2 \pi k \frac{n}{N} }$$
In [22]:
# Getting frequencies on x-axis right
rfreqs = [(i*1.0/num_samples)*sample_rate for i in range(num_samples/2+1)]
In [23]:
# So you can see, our frequencies go from 0 to 5Hz. 5 is half of the sample rate,
# which is what it should be (Nyquist Frequency).
rfreqs
Out[23]:
In [24]:
setup_graph(title='Corrected rFFT Output', x_label='frequency (in Hz)', y_label='amplitude', fig_size=(12,6))
_ = plot(rfreqs, rfft_output)
In [25]:
rfft_mag = [sqrt(i.real**2 + i.imag**2)/len(rfft_output) for i in rfft_output]
In [26]:
setup_graph(title='Corrected rFFT Output', x_label='frequency (in Hz)', y_label='magnitude', fig_size=(12,6))
_ = plot(rfreqs, rfft_mag)
In [27]:
irfft_output = numpy.fft.irfft(rfft_output)
In [28]:
setup_graph(title='Inverse rFFT Output', x_label='time (in seconds)', y_label='amplitude', fig_size=(12,6))
_ = plot(t, irfft_output)
In [28]: