Last active
February 1, 2019 03:17
-
-
Save bagustris/65537598464f712b2e81e334dde5b6e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/share/env python3 | |
# demo zeropadding | |
# dimodifikasi dari: https://dsp.stackexchange.com/questions/10363/algorithm-to-zero-pad-data-before-fft | |
# ada implementasi yang lebih mudah dari ini yakni dengan menggunakan np.pad | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from scipy.fftpack import fft, fftshift | |
Fs = 16000 # Frekuensi sampling | |
f0 = 5000 # Frekuensi tengah | |
N = 128 # Samples dalam domain waktu | |
K = 8 # faktor Zero-pad | |
# Buat indeks watu | |
t = np.linspace(0, N/Fs, N) | |
# Buat sinyal dengan panjang N*Fs pada frekuensi f0 Hz | |
x = np.cos(2*np.pi*f0*t)/N | |
# membuat zeropad dengan faktor K-1 kali panjang x dengan zeros | |
# xpad = np.concatenate((x, np.zeros((K-1)*len(x)))) | |
# bisa juga dengan np.pad seperti ini: | |
xpad = np.pad(x,(0, (K-1)*len(x)), 'constant') | |
# Hitung fft dari x dan xpad | |
xf = np.abs(fftshift(fft(x, len(x)))) | |
xpadf = np.abs(fftshift(fft(xpad, len(xpad)))) | |
# plot hasil | |
plt.plot(np.linspace(-N/2.0,N/2.0, len(x)), xf, color='b', lw=1.5) | |
plt.plot(np.linspace(-N/2.0,N/2, len(xpad)),xpadf,color='r', lw=1.5) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment