Skip to content

Instantly share code, notes, and snippets.

@kuoe0 kuoe0/fft.py
Created Oct 12, 2014

Embed
What would you like to do?
Python FFT Example
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 KuoE0 <kuoe0.tw@gmail.com>
#
# 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
You can’t perform that action at this time.