在我感受到寫程式可以便利的想像轉化為圖像之前,
我都不確定會寫程式對於學習有這樣大的妙用。
以前在修電子工程系的必修課—訊號與系統時,
我因為不太會使用Matlab,
所以就會去跟同學借程式碼來看,
再自己改了當作業交出去。
但是不是所有遇到難以理解的東西都有同學的程式碼,
所以前一陣子我就花了一點時間學習Python,
現在就到了收穫的時候了。
前幾天我在翻閱Simon Haykin和Barry Van Veen合著的Signals and Systems Second Edition,
發現一處我當初念不是很懂的地方,
在第37頁的離散弦波條件。
但是如果你將這一切圖像化就顯得容易明白了。
不信請看。
參考資料:
(1) https://ptolemy.eecs.berkeley.edu/eecs20/week8/discreteperiodic.html
(2) https://stackoverflow.com/questions/22566692/python-how-to-plot-graph-sine-wave
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 23 10:13:08 2018
@Editor: ShihHsing Chen
"""
import matplotlib.pyplot as plt # For ploting
import numpy as np # to work with numerical data efficiently
import pandas as pd
import matplotlib as mpl
mpl.style.use("ggplot") #Look Pretty
#If the above line throws an error, use plt.style.use('ggplot') instead
fig0 = plt.figure(0) #We need to number the figures in case of overlapping.
fig0.subplots_adjust(hspace=0.2, wspace=0.2) #Adjust height-spacing to
#de-overlap titles and ticks
ax1 = fig0.add_subplot(2, 1, 1)
fs = 100 # sampling rate
f = 2 # the frequency of the signal
x = np.arange(fs) # the points on the x axis for plotting
# compute the value (amplitude) of the sin wave at the for each sample
y = [ np.sin(2*np.pi*f * (i/fs)) for i in x]
plt.stem(x,y, 'r', )
plt.plot(x,y)
plt.title("Discrete Sine Wave w/ freq = 2", fontsize = 16)
ax2 = fig0.add_subplot(2, 1, 2)
fs = 100 # sampling rate
f = np.pi # the frequency of the signal
x = np.arange(fs) # the points on the x axis for plotting
# compute the value (amplitude) of the sin wave for each sample
y = [ np.sin(2*np.pi*f * (i/fs)) for i in x]
plt.stem(x,y, 'r', )
plt.plot(x,y)
plt.title("Discrete Sine Wave w/ freq = pi", fontsize = 16)
Comments
Post a Comment