Skip to content

Instantly share code, notes, and snippets.

@mattvenn
Created February 4, 2019 16:45
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 mattvenn/cd79555536e64c5434cf3273ec35f587 to your computer and use it in GitHub Desktop.
Save mattvenn/cd79555536e64c5434cf3273ec35f587 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import glob
Fs = 32000000 / 16 # sampling rate
Ts = 1.0/Fs # sampling interval
def load(filename):
y = []
x = []
count = 0
with open(filename) as fh:
for val in fh.readlines():
count += 1
x.append(len(x))
y.append(int(val))
# if count == 1000:
# break
return x, y
def plot():
fig = plt.figure()
# freq plot
ax = fig.add_subplot(1,1,1)
ax.grid(True)
ax.set_xlabel('Freq (Hz)')
ax.set_ylabel('|Y(freq)|')
for filename in glob.glob("data-10k*/*csv"):
print(filename)
x, y = load(filename)
ref = 1000 # arbitrary ref
N = len(y)
win = np.hamming(N)
window = y[0:N] * win # Take a slice and multiply by a window
sp = np.fft.rfft(window) # Calculate real FFT
s_mag = np.abs(sp) * 2 / np.sum(win) # Scale the magnitude of FFT by window and factor of 2,
freq = np.arange((N / 2) + 1) / (float(N) / Fs) # Frequency axis
ax.plot(freq, s_mag)
plt.show()
if __name__ == '__main__':
plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment