Menu

[r3211]: / trunk / py4science / examples / extras / fft_demo.py  Maximize  Restore  History

Download this file

48 lines (38 with data), 945 Bytes

 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
#!/usr/bin/env python
"""Simple demo of basic 1-d FFT."""
import numpy as N
import pylab as P
P.rc('lines', linewidth=2)
dx = 0.001
x = N.linspace(0.0, 1.0, 1000)
s = N.sin(2*4*N.pi*x*x)
F = N.fft(s)
nfreq = len(s)/2+1
freqs = N.linspace(0, 1/(2*dx), nfreq) # the freq vector
F = F[:nfreq] # extract positive frequencies
magnitude = N.absolute(F)/len(s)
phase = N.arctan2(F.imag,F.real)
# make the BODE plot
P.figure()
P.title('Bode plot')
P.subplot(211)
P.semilogy(freqs, magnitude)
P.xlim(0,20)
P.grid()
P.ylabel('Amplitude')
P.subplot(212)
P.plot(freqs, phase)
P.xlim(0,20)
P.grid()
P.ylabel('Phase')
# now do the reconstuction
def component(i):
# scale by 2 because we are exluding negative freqs
if i==0: scale = 1
else: scale =2
return scale*magnitude[i]*N.cos(2*N.pi*freqs[i]*x + phase[i])
tot = component(0)
for i in range(1,10): tot += component(i)
P.figure()
P.plot(x, tot, 'r--', x, s, 'b-', lw=2)
P.show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.