# 聊聊 signal 模組

``````import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

freq = 10           # 頻率
stop = .25          # 取樣範圍為 0 ~ stop
sample_rate = 800   # 取樣率，單位 x 取樣幾次
resample_rate = 0.1 # 重新取樣比例

x = np.linspace(0, stop, int(stop * sample_rate), endpoint = False)
y = np.sin(freq * 2 * np.pi * x)

# 重新取樣
x1 = np.linspace(0, stop, int(stop * sample_rate * resample_rate), endpoint = False)
y1 = signal.resample(y, int(x.size * resample_rate))

plt.plot(x, y)
plt.plot(x1, y1)
plt.show()
``````

``````import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

freq = 10           # 頻率
stop = .25          # 取樣範圍為 0 ~ stop
sample_rate = 25    # 取樣率，單位 x 取樣幾次

x = np.linspace(0, stop, int(stop * sample_rate), endpoint = False)
y = np.sin(freq * 2 * np.pi * x)

# 新模擬點的 x 座標
newx = np.linspace(0, stop, int(stop * sample_rate * 5), endpoint = False)

# 舊的資料間距
dx = 1 / sample_rate
newy1 = signal.qspline1d_eval(                  # 計算 y 值
signal.qspline1d(y), newx, dx = dx) # 計算係數
newy2 = signal.cspline1d_eval(
signal.cspline1d(y), newx, dx = dx)

plt.plot(x, y)
plt.plot(newx, newy1)
plt.plot(newx, newy2)

plt.show()
``````

``````import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

t = np.linspace(0, 5, 100)
x = t + np.random.normal(size=100)
x_detrended = signal.detrend(x)

plt.plot(t, x)
plt.plot(t, x_detrended)

plt.show()
``````