Skip to content

Instantly share code, notes, and snippets.

@kuoe0
Created October 12, 2014 12:20
Show Gist options
  • Save kuoe0/49e0c4bb170a8d6f5760 to your computer and use it in GitHub Desktop.
Save kuoe0/49e0c4bb170a8d6f5760 to your computer and use it in GitHub Desktop.
Python FFT Example
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 KuoE0 <[email protected]>
#
# Distributed under terms of the MIT license.
"""
"""
import pylab
import scipy.fftpack
import scipy
import csv
import sys
x = scipy.linspace(0, 5 * scipy.pi, 1000)
acc = lambda t: 10 * scipy.sin(2 * scipy.pi * 2.0 * t) + 5 * scipy.sin(2 * scipy.pi * 8.0 * t) + 8 * scipy.random.random(len(t))
signal = acc(x)
fft = abs(scipy.fft(signal))
fft_freq = scipy.fftpack.fftfreq(signal.size, x[1] - x[0])
writer = csv.writer(sys.__stdout__, delimiter=',')
pylab.subplot(211)
pylab.plot(x, signal)
pylab.subplot(212)
pylab.vlines(fft_freq, 0, fft, colors='b')
pylab.show()
fft_freq, fft = zip(*sorted(zip(fft_freq.tolist(), fft.tolist())))
for data in zip(signal.tolist(), fft_freq, fft):
writer.writerow(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment