Tugas PSG
Tugas PSG
Oleh:
Faris suhada
140710120020
SOAL
1. Buatlah sinyal digital dengan menggunakan fungsi berikut, dengan data sebanyak
2000 buah, koefisien c={10,5, 2}, frekuensi f={50, 150, 300}, dan fasa 0.
Tampilkan plot dari 100 data pertama.
2. Beri derau (noise) pada sinyal diatas menggunakan fungsi berikut, dengan
amplitudo 5. Tampilkan pula plot dari sinyal dengan derau tersebut.
3. Lakukan
pemfilteran
pada
sinyal
pertama
1.0/sqrt(1.0+(w/wn)**(2.0*n)),
dimana
dengan
w
fungsi
adalah
filter:
frekuensi,
wn=200 adalah frekuensi potong, dan n=3 adalah orde. Buat fungsi filter
tersebut. Tampilkan plot sinyal hasil filternya.
5. Lakukan
korelasi-diri
(Auto-correlation)
dari
signal
dengan
derau,
Korelasi
dilakukan
menggunakan
perintah
berikut:
sc=sg.correlate(yn,yn,same)
yn adalah sinyal yang memiliki derau, dan sc/(pi*N) adalah sinyal hasilnya, N
adalah jumlah data. Plot sinyalsinyal tersebut, amati dan beri penjelasan.
JAWABAN
Script Program :
import numpy as np
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt
from pylab import *
from scipy import signal
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
def sinyal(n,c,f,p):
y=np.zeros(n)
t=np.linspace(0,2*np.pi,n)
nf=len(c)
for i in range(nf):
y+=c[i]*np.sin(f[i]*t)
return y;
def derau(y,am):
seed()
yn=y+(0.5-random(len(y)))*am
return yn
def butterworth(w,wn,n):
retw=1.0/sqrt(1.0+(w/wn)**(2.0*n));
return retw;
#imput
n=2000
c=[10,5,2]
f=array([50,150,300])
p=[0]
t=linspace(0,2*pi,n)
am=5
#sinyal dan noise
ynon=sinyal(n,c,f,p)
ynoise=derau(ynon,am)
#pemplotkan sinyal
plt.subplot(2, 1, 1)
plt.plot(t[:100], ynon[:100], 'b-', label='Sinyal Utama')
plt.xlabel('Waktu')
plt.grid()
plt.legend()
plt.subplot(2, 1, 2)
plt.plot(t[:100], ynoise[:100], 'g-', label='Sinyal Dengan Noise')
plt.xlabel('Waktu')
plt.grid()
plt.legend()
plt.subplots_adjust(hspace=0.35)
plt.show()
#input untuk filter
order = 3
fs = 500
cutoff = 200
F = fft(ynoise)
#pem-filteran sinyal
output = butterworth(F,cutoff,order)
hasil=2000*ifft(output)
ya=butterworth(f,cutoff,order)
yb=sinyal(n,c,ya,p)
#pemplotan hasil
plt.subplot(3, 1, 1)
plt.plot(t[:100], ynon[:100], 'r-', label='Sinyal Tanpa Noise')
plt.plot(t[:100], ynoise[:100], 'b-', label='Sinyal Dengan Noise')
plt.plot(t[1:100],hasil[1:100],'g-', label='Hasil Filter')
plt.title('Sinyal Hasil Filter')
plt.xlabel('Waktu')
plt.ylabel('Amplitudo')
plt.grid()
plt.legend()
plt.subplot(3, 1, 2)
plt.plot(t, yb, 'g-', linewidth=2, label='Hasil lowpass filter')
plt.xlabel('Waktu'),
plt.grid()
plt.legend()
plt.subplot(3, 1, 3)
plt.plot( ya, 'g-', linewidth=2, label='Hasil lowpass filter')
plt.xlabel('Waktu'),
plt.grid()
plt.legend()
plt.subplots_adjust(hspace=0.35)
plt.show()
#Low Pass Filter
b, a = butter_lowpass(cutoff, fs, order)
w, h = freqz(b, a, worN=8000)
#menampilkan respon frekuensi
plt.subplot(2, 1, 1)
plt.plot(0.5*fs*w/np.pi, np.abs(h), 'b')
plt.plot(cutoff, 0.5*np.sqrt(2), 'ko')
plt.axvline(cutoff, color='k')
plt.xlim(0, 0.5*fs)
plt.title("Respon frekuensi lowpass filter")
plt.xlabel('Frekuensi')
plt.grid()
#Low pass filter
y = butter_lowpass_filter(ynoise, cutoff, fs, order)
#pemplotan hasil filter
plt.subplot(2, 1, 2)
plt.plot(t[:100], ynon[:100], 'r-', label='Sinyal Tanpa Noise')
plt.plot(t[:100], ynoise[:100], 'b-', label='Sinyal Dengan Noise')
plt.plot(t[:100], y[:100], 'g-', linewidth=2, label='Hasil lowpass')
plt.xlabel('Waktu')
plt.grid()
plt.legend()
plt.subplots_adjust(hspace=0.35)
plt.show()
#FFT
hasil2= fft(y)
x=4*fs*w/np.pi
#pemplotan sinyal dalam domain frekuensi
plt.subplot(2, 1, 1)
plt.plot(x[:1000], hasil2[:1000], 'g')
plt.title("Hasil Filter Sinyal Dalam Domain frekuensi")
plt.xlabel('Frekuensi')
plt.grid()
plt.subplot(2, 1, 2)
plt.plot(x[:2000], hasil2[:2000], 'b')
plt.title("Hasil Filter Sinyal Dalam Domain frekuensi")
plt.xlabel('Frekuensi')
plt.grid()
plt.show()
plt.subplot(2, 1, 1)
plt.plot(t[:100], ynon[:100], 'r-', label='Sinyal Tanpa Noise')
plt.plot(t[:100], ynoise[:100], 'b-', label='Sinyal Dengan Noise')
plt.plot(t[1:100], hasil[1:100], 'g-', linewidth=2, label='Hasil Filter')
plt.xlabel('Waktu')
plt.grid()
plt.legend()
#pemplotan seluruh hasil filter
plt.subplot(2, 1, 2)
plt.plot(t[:100], ynon[:100], 'r-', label='Sinyal Tanpa Noise')
plt.plot(t[:100], ynoise[:100], 'b-', label='Sinyal Dengan Noise')
plt.plot(t[:100], y[:100], 'g-', linewidth=2, label='Hasil Lowpass filter')
plt.xlabel('Waktu')
plt.grid()
plt.legend()
plt.subplots_adjust(hspace=0.35)
plt.show()
#mengkorelasikan sinyal
sc = signal.correlate (y,ynoise,"same")
output=sc/(pi*n)
#pemplotan korelasi sinyal
plot(t[:100],output[:100])
plt.title('Sinyal Terkorelasi')
plt.xlabel('Waktu')
plt.ylabel('Amplitudo')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
show()
Tampilan :
1. Membuat Sinyal dan derau
2. Hasil Filter
Domain Frekuensi
4. Korelasi Sinyal
5. Bandingkan Metode
Penjelasan: