import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
Fs = 50
N = 1024
t = np.linspace(0, N/Fs, N)
A0 = 5
A1 = 4
f1 = 1
A2 = 2
f2 = 3
x = A0 + A1*np.sin(2*np.pi*f1*t) + A2*np.sin(2*np.pi *
f2*t) + (-5)*np.random.random(N)
y = np.fft.fft(x)
freq = np.fft.fftfreq(N, d=(t[0]-t[1]))
def filter(freq, ft, flag=0, threshold=5):
for i in range(N):
if flag == 1:
if np.abs(freq[i]) == 0:
ft[i] = 0
elif flag == 2:
if np.abs(freq[i]) < threshold and np.abs(freq[i]) != 0:
ft[i] = 0
elif flag == 3:
if np.abs(freq[i]) > threshold:
ft[i] = 0
elif flag == 4:
if np.abs(ft[i])/N < 0.2:
ft[i] = 0
elif flag == 5:
if np.abs(freq[i]) > 3.5:
ft[i] = 0
else:
print("no filter")
figure, ax = plt.subplots()
figure.suptitle('频谱&滤波')
plt.subplot(221)
plt.axis([0, 5, -10, 10])
plt.plot(t, x, 'b')
plt.subplot(222)
plt.axis([-5, 5, 0, 3])
y1 = np.abs(y)/N
plt.plot(freq, y1, 'r')
filter(freq, y, 5, threshold=2)
x2 = np.fft.ifft(y)
plt.subplot(224)
plt.axis([-5, 5, 0, 3])
y1 = np.abs(y)/N
plt.plot(freq, y1, 'r')
plt.subplot(223)
plt.axis([0, 5, -10, 10])
plt.plot(t, x2.real, 'b')
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.figure('a beautiful girl')
beauty = cv2.imread('beauty.jpg', 0)
plt.subplot(221)
plt.imshow(beauty, 'gray')
y = np.fft.fft2(beauty)
y = np.fft.fftshift(y)
y1 = np.log(np.abs(y))
plt.subplot(222)
plt.imshow(y1, 'gray')
row, col = beauty.shape
mask = np.zeros(beauty.shape, np.uint8)
radius = 20
mask[row//2-radius:row//2+radius, col//2-radius:col//2+radius] = 1
y2 = y*mask
y2 = np.fft.ifftshift(y2)
x1 = np.fft.ifft2(y2)
plt.subplot(223)
x1 = np.abs(x1)
plt.imshow(x1, 'gray')
plt.subplot(224)
mask2 = np.abs(mask)
plt.imshow(mask2, 'gray')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
R = 10
theta = np.arange(0, 2 * np.pi, 0.01)
X = R * np.cos(theta)
Y = R * np.sin(theta)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(X**2 + Y**2)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.coolwarm)
ax.set_xlabel('x label', color='r')
ax.set_ylabel('y label', color='g')
ax.set_zlabel('z label', color='b')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
line2, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
line2.set_data([], [])
return line, line2,
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x-0.01*i))
y2 = np.cos(2 * np.pi * (x-0.01*i))
line.set_data(x, y)
line2.set_data(x, y2)
return line, line2,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
anim.save('im.htm', metadata={'artist': 'Guido'})
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
figure = plt.figure()
R = 10
t = np.linspace(-np.pi, np.pi, 100)
X = R * np.cos(t)
Y = R * np.sin(t)
X = R * np.outer(np.cos(t), np.sin(t))
Y = R * np.outer(np.sin(t), np.sin(t))
Z = R * np.outer(np.ones(np.size(t)), np.cos(t))
"""
不明白为什么这样绘制出来的是球形
"""
axes = figure.add_subplot(121, projection='3d')
axes.plot_surface(X, Y, Z, rstride=4, cstride=4, color='b')
axes = figure.add_subplot(122, projection='3d')
axes.plot_wireframe(X, Y, Z)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams['font.sans-serif'] = ['Fangsong']
plt.rcParams['axes.unicode_minus'] = False
R = 10
b = 1
theta = np.arange(0, 4 * np.pi, 0.01)
X = R * np.cos(theta)
Y = R * np.sin(theta)
Z = b * theta
def plane_view():
plt.axis('equal')
plt.plot(X, Y, 'r', label='俯视图')
plt.plot(X, Z, 'g', label='正视图')
plt.plot(Y, Z, 'b', label='侧视图')
plt.legend()
def view_3d():
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(X, Y, Z, c='r', label='螺旋线')
ax.set_xlabel('x label', color='r')
ax.set_ylabel('y label', color='g')
ax.set_zlabel('z label', color='b')
plt.legend()
if __name__ == '__main__':
plane_view()
view_3d()
plt.show()