You are on page 1of 2

# -*- coding: utf-8 -*-

"""
Created on Mon Nov 11 22:09:26 2019

@author: user
"""

# -*- coding: utf-8 -*-


"""
Created on Mon Nov 11 20:47:05 2019

@author: user
"""
from scipy.signal import butter, lfilter

def butter_bandpass(lowcut, highcut, fs, orde=5):


nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(orde, [low, high], btype='band')
return b, a

def butter_bandpass_filter(data, lowcut, highcut, fs, orde=5):


b, a = butter_bandpass(lowcut, highcut, fs, orde=orde)
y = lfilter(b, a, data)
return y

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import freqz
import random

# Frekuensi sampling dan frekuensi Cutoff (dalam Hz)


fs = 5000.0
lowcut = eval(input("Masukkan lowcutt :"))
highcut = eval(input("Masukkan highcutt :"))

#Plot renspons frekuensi untuk setiap orde yang berbeda


plt.figure(1)
plt.clf()
for orde in [3, 6, 9]:
b, a = butter_bandpass(lowcut, highcut, fs, orde=orde)
w, h = freqz(b, a, worN=2000)
plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="orde = %d" % orde)

plt.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)],


'--', label='sqrt(0.5)')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.grid(True)
plt.legend(loc='best')

# Memfilter sinyal
T = 0.05
nsamples = T * fs
t = np.linspace(0, T, nsamples, endpoint=False)
a = 0.02
f0 = 600.0
#Data di Soal No,2
a = eval(input ("Masukkan Nilai a : "))
b = eval(input ("Masukkan Nilai b : "))
c = eval(input ("Masukkan Nilai c : "))
d = eval(input ("Masukkan Nilai d : "))
e = eval(input ("Masukkan Nilai e : "))

u = eval(input ("Masukkan Nilai u : "))


v = eval(input ("Masukkan Nilai v : "))
w = eval(input ("Masukkan Nilai w : "))
x = eval(input ("Masukkan Nilai x : "))
y = eval(input ("Masukkan Nilai y : "))

data = a*np.sin(u*2*np.pi*t) + b*np.cos(v*2*np.pi*t) +


c*np.sin(2*np.sqrt(w)*2*np.pi*np.sqrt(t)) + d*np.cos(x**2*2*np.pi*t)
+e*np.sin(y*2*np.pi*t**2+27 )

plt.figure(2)
plt.clf()
plt.plot(t, data, label='Sinyal Input')
y = butter_bandpass_filter(data, lowcut, highcut, fs, orde=6)
plt.plot(t, y, label='Sinyal terfilter (%g Hz)' % f0)
plt.xlabel('waktu (detik)')
plt.hlines([-a, a], 0, T, linestyles='--')
plt.grid(True)
plt.axis('tight')
plt.legend(loc='upper left')

plt.show()

You might also like