0

I'm new to Python. I've done this particular task before in MATLAB, and I'm trying to get the hang of the syntax and particular behaviour of Python, as I'll be using this language much more in future.

The task: I am taking 43,200 single data points (integers, but written as decimals) and performing a fast-fourier transform on a "window" of 600 at a time, shifting this window by 60 data points each time. Hence, this transform will output 600 fourier coefficients, 720 times - I will end up with a 600 x 720 matrix (rows, columns).

These data points are initially contained within a list and turned into a column vector after being FFT'd. The issue comes when I try to build the maxtrix from a loop - take the first 600 points, FFT them, and dump them in an empty array. Take the next 600, do the same thing, but now add these two columns together to make two rows, then three, then four... etc. I've been trying for several hours now, but whatever I try I cannot get it to work - it consistently outputs my "final" matrix (the one that was meant to be the generated 600 x 720) as being the exact same dimensions as each generated "block".

My code (relevant sections):

for i in range(npoints):
newdata.append(float(newy.readline()))     #Read data from file

FFT_out = []        #Initialize empty FFT output array
window_size = 600   #Number of points in data "window"
window_skip = 60    #Number of points window moves across
j = 0               #FFT count variable

for i in range(0, npoints, window_skip):
    block = np.fft.fft(newdata[i:i+window_size])    #FFT Computation of "window"
    block = block[:, np.newaxis]                    #turn into column vector (n, 1)

if j == 0:
    FFT_out = block
    j = 1
else:
    np.hstack((FFT_out, block))
    j = j + 1

print("Shape of FFT matrix:")
print(np.shape(FFT_out))

print("Number of times FFT completed:")
print(j)

At this point, I'm willing to believe it's a fundamental flaw on my understanding of how Python does matrices or deals with arrays. I've tried reading about it, but I still cannot see where I'm going wrong. Any help would be greatly appreciated!

Yoshi
  • 671
  • 8
  • 20
  • ...and how was this downvoted within 30 seconds of myself posting it? I'd like to think it's a genuine question, I'm genuine in asking for help, and I've spent several hours trying to find the solution myself. – Yoshi Oct 04 '15 at 08:44
  • (I was not the downvoter) I guess is because somehow you are asking to solve your particular problem. In S.O. you should ask things that could help many people. I have the same problem my self over and over :D Is very annoying. – El Marce Oct 04 '15 at 09:09
  • Try to subdivide your question in parts. Strip the DSP from the python part. Python Gurus don't usually are DSP experts... You seem to want to add column vectors vc_n to form a matrix with them [vc_1, vc_2, vc_3] ? – El Marce Oct 04 '15 at 09:15
  • Yes, exactly! I just don't know if I'm declaring my arrays wrong, or if there's some mishap between pulling values from a list to form an array, etc. I mostly just included the FFT stuff so that there'd be some context to my problem - Basically numbers in a matrix get changed into complex numbers by a function, but it keeps the same dimensions. – Yoshi Oct 04 '15 at 09:41
  • check these answers, they may help: http://stackoverflow.com/questions/17428621/python-differentiating-between-row-and-column-vectors As well as: http://stackoverflow.com/questions/14741061/concatenating-column-vectors-using-numpy-arrays – El Marce Oct 04 '15 at 09:58
  • Thanks for the reply, but I've actually already tried and unsuccessfully applied each of these links to my own work. In the case of row vs. column vectors, I'm 90% sure that my algorithm forces each "block" to be produced as a column vector (as all information, by default, would be pulled from the list and put into this as a 1D array). In the case of the second link, I still get a concatenation error when I try to compile. I'm also unsure (once I can get it working) if it'll be able to subsequently add new columns to an already existing n-dimensional array. – Yoshi Oct 04 '15 at 11:33

1 Answers1

1

First thing to note is that Python is uses indentation to form blocks, so as posted you would only ever assign once to FFT_out and never actually call np.hstack.

Then assuming that this was in fact only a cut&paste issue when posting your question, you should note that hstack returns a concatenation of its arguments without actually modifying them. To accumulate the concatenation, you should then assign the result back to FFT_out:

FFT_out = np.hstack((FFT_out, block))

You should then be able to get a 600 x 720 matrix with:

for i in range(0, npoints, window_skip):
    block = np.fft.fft(newdata[i:i+window_size])
    block = block[:, np.newaxis]                    #turn into column vector (n, 1)

    if j == 0:
        FFT_out = block
        j = 1
    else:
        FFT_out = np.hstack((FFT_out, block))
        j = j + 1
SleuthEye
  • 14,379
  • 2
  • 32
  • 61
  • Thank you! That first but was a cut and paste error - I had correctly indented initially. It's nice to know it was such a simple error that had gotten me in trouble - that I hadn't been assigning the new concatenation to anything. Thanks! – Yoshi Oct 05 '15 at 01:30