[SciPy-user] Help with FFT code
Karthikesh Raju
karthik at james.hut.fi
Thu Jul 1 01:54:42 EDT 2004
Hi All,
Regarding the saving and loading data part of this question, i was faced
with the dilema of shifting data between python and matlab. i have written
a small module dataloader.py (the latest version of which is at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286171
)
dumpData takes a dictionary of variables and makes separate dat files in
ascii
format and saves them in these files. When all the keys are traversed, it
compresses all the files into an archive.
loadData does the reverse, it loads the data from a file to a dictionary.
i also have the equivalent matlab routines
loadData.m - when paseed a tar.gz file, it extracts the variable name and
loads it in the workspace
dumpData.m - when passed with variables in the workspace, it writes them
in ascii files and compresess them.
The both work quite well.
Side note: please let me know of methods to improve the python module.
Warm regards
karthik
-----------------------------------------------------------------------
Karthikesh Raju, email: karthik at james.hut.fi
Researcher, http://www.cis.hut.fi/karthik
Helsinki University of Technology, Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,
Espoo, FINLAND
-----------------------------------------------------------------------
On Wed, 30 Jun 2004, Travis Oliphant wrote:
> 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.
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
>
More information about the SciPy-User
mailing list