目录
写在前面:
冬天的 Palo Alto 时而降温时而回升,CrossTownShuttle便是不二之选。通勤的四十分钟里,大部分时间会读 Kindle 第二顺位是发呆,最后是 Deadline 临近抽出背包的电脑工作。司机的车技都不是很好,就是那种直路平地晃。每天下午四点四十回家的车,司机会放广播,还不错,比如那首 Stitches。颠簸着看完了黑客与画家,Hackers and painters really worth reading!!
黑客的出发点是原创,最终得到一个优美的结果;而科学家的出发点是别人优美的结果,最终得到原创性。
翻译的太差了。Anyway, 作为一个对 Coding 有偏执喜欢的人,想好好琢磨这句话。
This blog will record how do I hands-on python.
Python
在 Mike 同学的指导下,开始了Python
的探索。还好目前任务只是迁移Matlab
到Numpy
的过程,顺便练习一下 Python
的句法。主要还是一些跟Matlab
类似的矩阵运算、显示,高级一些就是Scipy
中的频域算法,傅里叶变换什么的。有些类似也有些差异,总之很好上手。不能忘记的差异是Python
的数组是从「0」开始 index 的.
Mike 同学说:
For transitioning, I would read this: https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html
And then there are good practice problems here for you to start off with http://codingbat.com/python
- 用
matplotlib
绘图
这是自学的第一个例子,简单直观。
import matplotlib.pyplot as plt
import numpy as np
# 简单的绘图
x = np.linspace(0, 2 * np.pi, 50)
plt.plot(x, np.sin(x)) # 如果没有第一个参数 x,图形的 x 坐标默认为数组的索引
plt.show() # 显示图形
x = np.linspace(0, 2 * np.pi, 50)
plt.plot(x, np.sin(x),
x, np.sin(2 * x))
plt.show()
# 自定义曲线的外观
x = np.linspace(0, 2 * np.pi, 50)
plt.plot(x, np.sin(x), 'r-o',
x, np.cos(x), 'g--')
plt.show()
# 使用子图
x = np.linspace(0, 2 * np.pi, 50)
plt.subplot(2, 1, 1) # (行,列,活跃区)
plt.plot(x, np.sin(x), 'r')
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x), 'g')
plt.show()
# 简单的散点图
x = np.linspace(0, 2 * np.pi, 50)
y = np.sin(x)
plt.scatter(x,y)
plt.show()
# 彩色映射散点图
x = np.random.rand(1000)
y = np.random.rand(1000)
size = np.random.rand(1000) * 50
colour = np.random.rand(1000)
plt.scatter(x, y, size, colour)
plt.colorbar()
plt.show()
# 直方图
x = np.random.randn(1000)
plt.hist(x, 50)
plt.show()
# 添加标题,坐标轴标记和图例
x = np.linspace(0, 2 * np.pi, 50)
plt.plot(x, np.sin(x), 'r-x', label='Sin(x)')
plt.plot(x, np.cos(x), 'g-^', label='Cos(x)')
plt.legend() # 展示图例
plt.xlabel('Rads') # 给 x 轴添加标签
plt.ylabel('Amplitude') # 给 y 轴添加标签
plt.title('Sin and Cos Waves') # 添加图形标题
plt.show()
-
从
.csv
导入数据这个例子把我折腾了一晚上,并不是找不到导入方法,事实上从最简单的
import csv
到使用高级的Pandas
,最起码有三种方式,可我的数据怎么也导入不了,后来才发现是因为我的数据就一维,而且全是float
的数据,没有string
,所以用Pandas
的时候,数据全部变成了column names
,导致数据为空。而Pandas
可处理更高级的混合数据,从而进行数据挖掘。
import pandas as pd
import numpy as np
import csv
disp_matrix = np.loadtxt(open("eggs.csv","rb"),delimiter=",",skiprows=0)
#我的csv文件没有项目名称,所以column names全部变成了数据
#Pandas的使用方法
dp = pd.read_csv("eggs.csv")
dp['A'].astype(float)
- 快速傅里叶变换实现时频转换
from scipy.fftpack import fft, fftshift
plt.figure()
S2 = fft(I2, 2048) / (len(I2)/2.0)
#freq2 = np.linspace(-0.5, 0.5, len(S2))
response2 = 20 * np.log10(np.abs(fftshift(S2 / abs(S2).max())))
#plt.plot(freq1, response1)
plt.plot(response1)
#plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Hann window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]”)
- 开窗
SW1 = np.linspace(0, 0, S1.size)
SW1[w1] = S1[w1]
Python for reading-friendly clippings of Kindle
To be frank, it’s not easier to read clippings Kindle offers you. The .txt document really sucks. So here I found a little tool helping you to sort of clippings in different books. Originally, making such a software written by Python would my best option, while after I found it in Github, just giving up the idea because of laziness. Here’s the final .md
documents in the folder.

What is Macro
From Wikipedia:
a single instruction that expands automatically into a set of instructions to perform a particular task.
尾巴
用了一个半晚上写好了Dispersion Compensation
的算法,Mike说他半小时搞定的,是的,我可以向他学到很多东西。喜欢他!