In [1]:
import numpy as np, cmath,scipy as sp
import scipy.io
from matplotlib import pyplot as plt

from numpy import pi, sin, cos, exp, sqrt, log, random  #import basic functions from numpy that we'll need

%matplotlib inline

In [3]:
import seaborn as sns
sns.set_palette('muted')
sns.set_style('darkgrid')

Figure 6.2


In [28]:
#create sine wave
srate = 1000.
time = np.arange(0,1+1/srate,1/srate)
frequency = 3

sinewave = sin(2*pi*frequency*time)

plt.figure()
plt.subplot(311)
plt.plot(time,sinewave,'r')
plt.axis([-.05,time[-1]*1.05,-1.1,1.1])
plt.title("continuous sine wave")

sampling1 = np.round(np.linspace(1,len(time),frequency*2)).astype(int) - 1
sampling2 = np.round(np.linspace(1,len(time),frequency*20)).astype(int) - 1
plt.plot(time[sampling1],sinewave[sampling1],'-o')
plt.plot(time[sampling2],sinewave[sampling2],'ko',markersize=5)


#need to cast these as integers, hence the .astype(int)
#also, python indexing starts at 0, so minus 1 
plt.subplot(312)
plt.plot(time[sampling1],sinewave[sampling1],'-o')
plt.title("Sampled at 2*frequency")

plt.subplot(313)
plt.plot(time[sampling2],sinewave[sampling2],'ko',markersize=5)
plt.title("Sampled at 20*frequency")

plt.tight_layout()