Skip to content

Instantly share code, notes, and snippets.

@bagustris
Last active February 1, 2019 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bagustris/65537598464f712b2e81e334dde5b6e1 to your computer and use it in GitHub Desktop.
Save bagustris/65537598464f712b2e81e334dde5b6e1 to your computer and use it in GitHub Desktop.
#!/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