# 傅立葉轉換（三）

``````import numpy as np
from math import floor
from random import randint
import matplotlib.pyplot as plt

def blending(t):
return 6 * (t ** 5) - 15 * (t ** 4) + 10 * (t ** 3)
blending = np.frompyfunc(blending, 1, 1)

def lerp(s1, s2, t):
return s1 + t * (s2 - s1)
lerp = np.frompyfunc(lerp, 3, 1)

return s if n % 2 == 0 else -s

np.random.seed(10) # 為了每次取得相同雜訊，這邊設定了亂數種子
rand_table = np.random.randint(255, size = 256)
def perlin1(x):
xi = np.floor(x)
s = x - xi
a = rand_table[(xi % 256).astype(np.int)]
b = rand_table[((xi + 1) % 256).astype(np.int)]

def singal(t, sample_rate):
noise_level = 10  # 雜訊的變化程度
x = np.linspace(0, noise_level * t, int(t * sample_rate), endpoint = False)
return perlin1(x)

t = 2
sample_rate = 800

samples = singal(t, sample_rate)
x = np.linspace(0, t, int(t * sample_rate), endpoint = False)

plt.title('Perlin noise')
plt.xlabel('x')
plt.ylabel('y')
plt.gca().set_aspect(1)
plt.plot(x, samples)
plt.show()
``````

``````import numpy as np
from math import floor
from random import randint
import matplotlib.pyplot as plt

...signal 等函式的定義同上…略

t = 2
sample_rate = 800

samples = singal(t, sample_rate)
sp = np.fft.fft(samples)

freq = np.fft.fftfreq(samples.size, d = 1 / sample_rate) # 頻率
amp = np.abs(sp)                   # 振幅
phi = np.arctan2(sp.real, sp.imag) # 相位

amp_normal = amp / np.max(amp)     # 振幅標準化

# 振幅大於 0.05 的部份
amp_greater_005 = np.where(amp_normal > 0.05)
freq2 = freq[amp_greater_005]
amp2 = amp[amp_greater_005]
phi2 = phi[amp_greater_005]

# 正頻率的部份
freq_positive = np.where(freq2 > 0)
freq3 = freq2[freq_positive]
amp3 = amp2[freq_positive]
amp3_normal = amp3 / np.max(amp3)
phi3 = phi2[freq_positive]

# 頻率-振幅
plt.subplot(2, 1, 1)
plt.stem(freq3, amp3_normal)

# 頻率-相位
plt.subplot(2, 1, 2)
plt.stem(freq3, phi3)

plt.show()
``````

``````import numpy as np
from math import floor
from random import randint
import matplotlib.pyplot as plt

...signal 等函式的定義同上…略

t = 2
sample_rate = 800

samples = singal(t, sample_rate)
sp = np.fft.fft(samples)

freq = np.fft.fftfreq(samples.size, d = 1 / sample_rate) # 頻率
amp = np.abs(sp)                   # 振幅
phi = np.arctan2(sp.real, sp.imag) # 相位

amp_normal = amp / np.max(amp)     # 振幅標準化

# 振幅大於 0.05 的部份
amp_greater_005 = np.where(amp_normal > 0.05)
freq2 = freq[amp_greater_005]
amp2 = amp[amp_greater_005]
phi2 = phi[amp_greater_005]

# 正頻率的部份
freq_positive = np.where(freq2 > 0)
freq3 = freq2[freq_positive]
amp3 = amp2[freq_positive]
amp3_normal = amp3 / np.max(amp3)
phi3 = phi2[freq_positive]

# 繪圖
ax = plt.axes(projection='3d')
ax.set_xlabel('t')
ax.set_ylabel('f')
ax.set_zlabel('amp')

stop = 1
t = np.linspace(0, stop, int(stop * sample_rate), endpoint = False)
zero = np.zeros(t.size)
for fi in range(0, len(freq3)):
f = zero + freq3[fi]
amp = np.sin(phi3[fi] + freq3[fi] * 2 * np.pi * t) * amp3_normal[fi]
ax.plot(t, f, amp)

plt.show()
``````

``````import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

freq = 5
stop = .25
sample_rate = 800

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

plt.imshow(gray.reshape(1, 200).repeat(200, axis = 0), cmap = cm.gray)
plt.show()
``````