[SciPy-user] Help with FFT code

Travis Oliphant oliphant at ee.byu.edu
Thu Jul 1 01:30:53 EDT 2004


Aaron Williams wrote:

> Hi
>
> I am a new user of python and scipy. I am trying to write a code that 
> Fast Fourier Transforms 2-D arrays saved as binary FORTRAN files. 
> I would like for my code to also create an output file containing the 
> certain statistics about the imported data set.

What format would you like this output file to be in?   Raw binary, text 
file, importable Python module, matlab .mat file?  There are lots of 
options here.  The answer usually depends on what you are going to do 
with the saved data.

If you will be sticking with using the data in Python later I would 
recommend  io.save(name, dict)


>  
> First I am having a problem calling my program from the command line. 
> When I run the code with Python shell IDLE nothing happens.

Probably due to the raw_input command.  This usually reads from standard 
input. 

Just run your program from the prompt, for example:

python myprogram.py


> Second I was wondering how to create an output file in my code.
>  
>  Here is an example of my code.
> #################################
> # FFT converter
> #!/usr/bin/python
> /from scipy import *
> /

> /io.array_import
> /

(not sure what you are trying to do here?)

> #open Fortran Binary array
> x = raw_input("enter in file path, i.e. 'path' else enter 'No'")
> if x == No:

should have quotes around No 
   if x=="No":

> print "no data file enter end of program"
> else:
> /fid=io.fopen(x,'r','n')
> /

default is 'n'  so fid=io.fopen(x,'r')  should do


> /z1=fid.fort_read(256,dtype='f')
> /#data array is brought in as 1D array needs to be 2D
> /zcl=reshape(z1,(256,256))
> zcl2=fftpack.fft2(zcl)
> zcl3=fftpack.fftshift(zcl2)
> zcl4=zcl3*conjugate(zlc3)
> /#power spectrum
> /zcl5=zcl4.real/

fft2 and fftshift can be accessed from scipy name space

zcl3 = fftshift(fft2(zcl))
zcl5 = real(zcl3*conj(zcl3))

> #xplt.imagesc(zcl5)
> #xplt.imagesc(zcl)

should work

> #Ask for the name of plot
> q=raw_input("Enter name of transformed File")
> /xplt.imagesc(log(zcl5))/
> xplt.eps(q)
> s=raw_input("Enter Name of Original")
> /xplt.imagesc(zcl)/
> xplt.eps(s)
>
> #####################################
>
> I have been able to enter the italicized parts into the command 
> individually, but this not efficient for as many transforms I need to do.


Again, it looks like the trouble might be the way raw_input is 
interacting with IDLE.

To save data for later use in Python use

io.save("mydata", {'varname1':vardata1, 'vardata2':vardata2}) 

Later

import mydata

will give you mydata.varname1 and mydata.varname2

Or you can use

io.write_arrray to write a text file

or

fid.fwrite    for low-level binary data writing (not usually recommended).

-Travis O.




More information about the SciPy-User mailing list