-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft.py
56 lines (52 loc) · 1.37 KB
/
fft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''
fft.py calculates and plots the fast fourier transform of a function.
'''
import scipy
import numpy
import pylab
def step(transition, starts):
x = numpy.arange(1000)
y = numpy.zeros(1000)
if starts == 0:
y[transition:] = 1
elif starts == 1:
y[:transition] = 1
X = numpy.arange(-500, 500, 1)
Y = abs(numpy.fft.fft(y))
_Y = numpy.zeros(1000)
_Y[:500] = Y[500:]
_Y[500:] = Y[:500]
Y = _Y
return x, y, X, Y
def squarewave(trans1, trans2, starts):
x = numpy.arange(1000)
if starts == 0:
y = numpy.zeros(1000)
y[trans1:trans2] = 1
elif starts == 1:
y = numpy.ones(1000)
y[trans1:trans2] = 0
X = numpy.arange(-500, 500, 1)
Y = abs(numpy.fft.fft(y))
_Y = numpy.zeros(1000)
_Y[:500] = Y[500:]
_Y[500:] = Y[:500]
Y = _Y
return x, y, X, Y
if __name__ == '__main__':
pylab.suptitle('Functions and Their FFTs')
pylab.subplot(221)
Step = step(100, 0)
pylab.title('Step Function')
pylab.plot(Step[0], Step[1])
pylab.subplot(222)
Square = squarewave(450, 550, 0)
pylab.title('Square Function')
pylab.plot(Square[0], Square[1])
pylab.subplot(223)
pylab.title('FFT of Step Function')
pylab.plot(Step[2], Step[3])
pylab.subplot(224)
pylab.title('FFT of Square Function')
pylab.plot(Square[2], Square[3])
pylab.show()