In [14]:
# Import functions and libraries
# from __future__ import division
import numpy as np, matplotlib.pyplot as plt, bokeh.plotting as bk
import threading,time, queue, pyaudio 
# I have to install pyaudio and queue
# pip install queuelib
# brew update
# brew install portaudio
# pip install pyaudio
from matplotlib.pyplot import *
import matplotlib.cm as cm
from scipy import signal
from numpy import *

%matplotlib inline
In [15]:
# your code here
# generate time index
reclen  = 10;
# generate f_of_t
fm = 40;
fs = 2*fm
f0 =5;
t = np.r_[0:reclen:(1/fs)]
f_of_t = f0 + (fm-f0)/reclen*t
# generate phi_of_t
phi_of_t = 2*np.pi*np.cumsum(f_of_t)*(1/fs)
# generate chirp signal
s_chirp = 0.5*np.sin(phi_of_t)
In [16]:
# Set the aspect ratio such that the image is wide
width, height = figaspect(0.2)
fig = figure(figsize=((width),height))

#Your code below:

plt.plot(s_chirp)
plt.xlabel("n")
plt.ylabel("signal")
plt.title("Chirp function")
Out[16]:
Text(0.5,1,'Chirp function')
In [17]:
xd = s_chirp[::2]
width, height = figaspect(0.5)
fig = figure(figsize=(width,height))

#Your code below:

plt.plot(xd)
plt.xlabel("n")
plt.ylabel("signal")
plt.title("decimated chirp function")
Out[17]:
Text(0.5,1,'decimated chirp function')
In [6]:
width, height = figaspect(0.5)
fig = figure(figsize=(width,height))
fstft, tstft, Zxx = signal.stft(np.real(s_chirp[::2]), fs, nperseg = 32);
plt.pcolormesh(tstft,fstft,np.abs(Zxx), vmin = 0, vmax = .2)
plt.title('STFT')
Out[6]:
Text(0.5,1,'STFT')
In [23]:
M = 3;
xd = s_chirp[::M]

width, height = figaspect(0.2*M)
fig = figure(figsize=(width,height))

#Your code below:

plt.plot(xd)
plt.xlim((0, t[::M].shape[0]))
plt.xlabel("n")
plt.ylabel("signal")
plt.title("Chirp function")


fig = figure(figsize=(width,height))
fstft, tstft, Zxx = signal.stft(np.real(s_chirp[::M]), fs, nperseg = 32);
plt.pcolormesh(tstft,fstft,np.abs(Zxx), vmin = 0, vmax = .2)
plt.title('STFT')
Out[23]:
Text(0.5,1,'STFT')
In [22]:
#What anti-alias?
ft = fft.fftshift(fft.fft(s_chirp))
width, height = figaspect(0.2)
fig = figure(figsize=(width,height))
omega = np.r_[:reclen*fs]*2*pi/fs/reclen-pi



plt.plot(omega,np.abs(ft))
plt.xlabel('omega')
plt.title('chirp spectrum')
plt.ylabel('|X|')
aafilt = np.abs(omega)<pi/M

width, height = figaspect(0.2)
fig = figure(figsize=(width,height))
plt.plot(omega,abs(aafilt*ft))
plt.title('lowpass filtered')
plt.xlabel('omega')
plt.ylabel('|X|')
Out[22]:
Text(0,0.5,'|X|')
In [12]:
xsm = fft.ifft(fft.ifftshift(aafilt*ft))
width, height = figaspect(0.2*M)
fig = figure(figsize=(width,height))
plt.plot(np.real(xsm))
plt.xlim((0, t.shape[0]))
plt.xlabel('n')
title('lowpassed chirp')


fig = figure(figsize=(width,height))
fstft, tstft, Zxx = signal.stft(np.real(xsm), fs, nperseg = 32);
plt.pcolormesh(tstft,fstft,np.abs(Zxx), vmin = 0, vmax = .2)
plt.title('STFT')
Out[12]:
Text(0.5,1,'STFT')
In [13]:
width, height = figaspect(0.2*M)
fig = figure(figsize=(width,height))
plt.plot(np.real(xsm[::M]))
plt.xlim((0, t[::M].shape[0]))
plt.xlabel('n')
title('resampled lowpass')


fig = figure(figsize=(width,height))
fstft, tstft, Zxx = signal.stft(np.real(xsm[::M]), fs, nperseg = 20);
plt.pcolormesh(tstft,fstft,np.abs(Zxx), vmin = 0, vmax = .2)
plt.title('STFT')
Out[13]:
Text(0.5,1,'STFT')