I’m writing a program to read in data from a list, Fourier transform and shift it before plotting. So far, the code takes the spectroscopy data from the DICOM file and puts it into a list, with each element an array containing the values of each individual FID/spectra.
from pylab import *
import dicom
plan=dicom.read_file("")
all_points = array(plan.SpectroscopyData)
cmplx_data = all_points[0::2] + 1j*all_points[1::2]
frames = int(plan.NumberOfFrames)
fid_pts = len(cmplx_data)/frames
fid_list = []
for fidN in arange(frames):
offset = fidN * fid_pts
current_fid = cmplx_data[offset:offset+fid_pts]
fid_list.append(current_fid)
This works fine to group the data, but I encounter problems when trying to use the arrays generated. Firstly, when trying to display only the complex part of the data, for example:
plot(complex(fid_list[0]))
Returns
Traceback (most recent call last)
/home/dominicc/Desktop/<ipython-input-37-4146b7fbfd7c> in <module>()
----> 1 plot(complex(fid_list[0]))
TypeError: only length-1 arrays can be converted to Python scalars
Secondly, and most importantly though, I encounter infinite recursion when trying to plot the zero-frequency shift of the FFTed data:
plot(fftshift(fft(fid_list[0])))
Getting the following error
/home/dominicc/Desktop/New_Script.py in fftshift(fid_in)
23
24 def fftshift(fid_in):
---> 25 fft_fid_in = fft(fid_in)
26 plot(fftshift(fft_fid_in))
27 show()
/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in fft(a, n, axis)
162 """
163
--> 164 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache)
165
166
/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in _raw_fft(a, n, axis, init_function, work_function, fft_cache)
43 def _raw_fft(a, n=None, axis=-1, init_function=fftpack.cffti,
44 work_function=fftpack.cfftf, fft_cache = _fft_cache ):
---> 45 a = asarray(a)
46
47 if n is None:
RuntimeError: maximum recursion depth exceeded
Can anyone suggest ways to improve my code so as to avoid these problems? Thanks.
Solved by adding another for loop that Fourier transformed the data before the fftshit: