jailton_ex7

Autor: jailton
Data: 05/05/2009
 1 # Exercício da aula teórica- Prof. Clésio- sobre a transformada de Fourier
 2 # A função f(x)=cos(x) foi utilizada para testes
 3 def fxd(M):
 4     fxd=numpy.zeros(M)
 5     for x in range(M):
 6       fxd[x]=math.cos(x)
 7     return fxd
 8 def jDFT(fxd):
 9     import numpy
10     import math
11     M=len(fxd)
12     F=[0j]*M
13     for u in range(M):
14         F[u]=complex(0,0)
15         for x in range(M):
16           F[u]=complex(F[u])+complex(1.0/M*fxd[x]*math.cos(2*numpy.pi*x*u/M),-1.0/M*math.cos(x)*math.sin(2*numpy.pi*x*u/M))
17     return F
18 M=10
19 fxd=fxd(M)
20 Fu=jDFT(fxd)
21 print "Função F(u)--utilizando a função jDFT:"
22 print Fu
23 def magnitudeF(Fu):
24     import numpy
25     import math
26     F=Fu
27     M=len(Fu)
28     Mag=(numpy.zeros(M))
29     for u in range(M):
30         Mag[u]=round(abs(F[u]),2)
31     return Mag
32 magnitude=magnitudeF(Fu)
33 print "Função Magnitude de F(u)--utilizando a função criada magnitudeF"
34 print magnitude
35 def faseF(Fu):
36     import numpy
37     import math
38     F=Fu
39     M=len(Fu)
40     Fase=(numpy.zeros(M))
41     for u in range(M):
42         Fase[u]=round(math.atan(F[u].imag/F[u].real)*180/numpy.pi,2)
43     return Fase
44 fase=faseF(Fu)
45 print "Função Fase em Graus de F(u)--utilizando a função criada faseF:"
46 print fase
47 def potenciaF(Fu):
48     import numpy
49     import math
50     F=Fu
51     M=len(Fu)
52     Mag=magnitudeF(Fu)
53     Pot=(numpy.zeros(M))
54     for u in range(M):
55         Pot[u]=round(Mag[u]*Mag[u],2)
56     return Pot
57 poten=potenciaF(Fu)
58 print "Função Potência do Espectro de F(u)--utilizando a função criada potenciaF:"
59 print poten
60 def jIDFT(Fu):
61     import numpy
62     import math
63     F=Fu
64     M=len(Fu)
65     f=[0j]*M
66     for x in range(M):
67         f[x]=complex(0,0)
68         for u in range(M):
69           f[x]=round(((f[x]+complex(F[u]*math.cos(2*numpy.pi*x*u/M),F[u]*math.sin(2*numpy.pi*x*u/M))).real),2)
70     return f
71 fx=jIDFT(Fu)
72 print "Função f(x)--utilizando a função criada jIDFT:"
73 print fx
74 import numpy
75 from numpy.fft import fft
76 from numpy import array
77 print fxd
78 b=fft(fxd)
79 print b
Função F(u)--utilizando a função jDFT:
[(0.042162378262054656+0j), (0.0067743883565619112+0.20113882287359977j), (0.19091748751694237-0.37811760512784109j), (0.11890327486555906-0.1029683984080219j), (0.10891686971783707-0.040056461154426598j), (0.10681358082414499+6.2265624991477108e-17j), (0.108916869717837+0.040056461154426716j), (0.11890327486555911+0.10296839840802216j), (0.19091748751694179+0.37811760512784104j), (0.0067743883565616336-0.20113882287360024j)]
Função Magnitude de F(u)--utilizando a função criada magnitudeF
[ 0.04  0.2   0.42  0.16  0.12  0.11  0.12  0.16  0.42  0.2 ]
Função Fase em Graus de F(u)--utilizando a função criada faseF:
[  0.    88.07 -63.21 -40.89 -20.19   0.    20.19  40.89  63.21 -88.07]
Função Potência do Espectro de F(u)--utilizando a função criada potenciaF:
[ 0.    0.04  0.18  0.03  0.01  0.01  0.01  0.03  0.18  0.04]
Função f(x)--utilizando a função criada jIDFT:
[1.01, 0.55000000000000004, -0.40999999999999998, -0.98999999999999999, -0.65000000000000002, 0.27000000000000002, 0.96999999999999997, 0.77000000000000002, -0.17000000000000001, -0.91000000000000003]
[ 1.          0.54030231 -0.41614684 -0.9899925  -0.65364362  0.28366219
  0.96017029  0.75390225 -0.14550003 -0.91113026]
[ 0.42162378 +0.00000000e+00j  0.06774388 +2.01138823e+00j
  1.90917488 -3.78117605e+00j  1.18903275 -1.02968398e+00j
  1.08916870 -4.00564612e-01j  1.06813581 -1.33226763e-15j
  1.08916870 +4.00564612e-01j  1.18903275 +1.02968398e+00j
  1.90917488 +3.78117605e+00j  0.06774388 -2.01138823e+00j]