39 lines
872 B
Python
39 lines
872 B
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
# vytvoreni signalu
|
|
t = np.linspace(0, 2, 200)
|
|
clean_signal = np.sin(2 * np.pi * 1 * t)
|
|
|
|
# zasumeni signalu
|
|
noise = 0.1 * np.random.randn(len(t))
|
|
noisy_signal = clean_signal + noise
|
|
|
|
|
|
# 1D konvoluce
|
|
def convolution(signal, clean):
|
|
c = []
|
|
for i, bv in enumerate(clean):
|
|
for j, value in enumerate(signal):
|
|
if i+j < len(c):
|
|
c[i+j] += value*bv
|
|
else:
|
|
c.append(value*bv)
|
|
return c
|
|
|
|
|
|
# vytvoření filtru na signál
|
|
def filter_signal(signal, number):
|
|
return convolution(signal, [1/number for _ in range(number)])
|
|
|
|
|
|
# první obrázek - vytvořený signál
|
|
plt.plot(noisy_signal)
|
|
plt.show()
|
|
# druhý obrázek - signál po filtru n=5
|
|
plt.plot(filter_signal(noisy_signal, 5))
|
|
plt.show()
|
|
# třetí obrázek - signál po filtru n=20
|
|
plt.plot(filter_signal(noisy_signal, 20))
|
|
plt.show()
|
|
|