Untitled
raw download clone
TEXT
views 20
,
size 987 b
import matplotlib.pyplot as plt
import numpy as np
from math import *


def signal(x):
    return 1.0 + sin(2.0 * pi * x) + 2.0 * cos(4.0 * pi * x) + 7.2 * sin(7.0 * pi * x)


def dft(data):
    n = len(data)
    spectr = [0 + 0j] * n
    coef = 2.0 / n
    arg = pi * coef
    for j in range(n):
        spectr[j] = 0.0 + 0.0j
        for i in range(n):
            spectr[j] += data[i] * (cos(arg * i * j) - sin(arg * i * j) * 1j)
    return np.array(spectr, dtype=complex)


f = float(input('f:'))
T = float(input('T:'))
n = int(input('n:'))
t = np.linspace(0, T, n)
u = [0] * n
for i in range(n):
  u[i] = signal(f*t[i])
spec1 = dft(u)
spec2 = np.fft.fft(u)
freq = np.fft.fftfreq(n, T / n)
plt.plot(t, u)
plt.show()
plt.plot(freq[0:n//2], (np.hypot(spec1.real, spec1.imag)/n*2.0)[0:n//2], label = 'DPF')

plt.legend(loc= 'best')
plt.show()
plt.plot(freq[0:n//2], (np.hypot(spec2.real, spec2.imag)/n*2.0)[0:n//2], '-.',  label = 'BPF')
plt.show()
close fullscreen
Login or Register to edit or fork this paste. It's free.