user6052232
user6052232

Reputation: 169

Find FFT and summation in 4D matrix

Please, I have F(8*409600*3) matrix. I want to reshape it into B(8*512*3*800) matrix (each row should divide into 800 parts) then I have to find Fast Fourier transform(FFT) for each (8*512) rows and multiply it by its conjugate and divide by a constant after that I have to find summation of elements of each row in fourth dimension and average them by the number of slices within the fourth dimension (800). I mean

(B(1,1,1,1) +B(1,1,1,2)+B(1,1,1,3)……+B(1,1,1,800))/800
(B(1,2,1,1) +B(1,2,1,2)+B(1,2,1,3)……+B(1,2,1,800))/800

And so on for each row.

I used below code it seems work but does not give a correct result.

 F=rand(8,409600,3);
 B=reshape(F,8,512,3,[]);
 C1=fft(B, [],2);
 C2=C1.*conj(C1);
 C3=C2/(3000);
 C4=sum(C3,4)/800;

Thanks

Upvotes: 1

Views: 217

Answers (1)

Ahmed Fasih
Ahmed Fasih

Reputation: 6927

I think you want to try B = reshape(F, 8, 512, [], 3); to get 8 by 512 by 800 by 3 array.

Then do C1 = abs(fft(B, [], 2)).^2; to take the 512-point FFT and convert the spectrum to a power spectrum. (Note that for complex c, conj(c) * c == abs(c)^2 to machine precision.)

Then you want to average those 800 512-point PSDs: C2 = squeeze(mean(C1, 3));. If you don’t use squeeze here, you’ll have 8 by 512 by 1 by 3 array, and squeeze just eliminates the 1-length dimensions, leaving C2 to be a 8 by 512 by 3 array. I prefer to use mean here instead of sum and divide because it’s more obvious what you’re trying to achieve.

Finally, you seem to have a 3000 factor, so: C3 = C2 / 3000;.


Here’s how I’m thinking about your problem. You have eight sensors (microphone, photodiode, whatever). Each sensor collects 409600 samples. You do this for three days. So you have your original F matrix that’s 8 by 409600 by 3.

Now, you want to see how the power spectral density changes for each sensor over the three days. So you split up each sensor/day’s 409600 samples into 800 chunks, and each chunk gets 512 samples—no overlap between chunks. In the code above, you take each 512-long chunk’s FFT and average all 800 of them. This leaves you with a 8 by 512 by 3 array, and you now have what you wanted: an estimate of each sensor’s PSD for each day.

Upvotes: 1

Related Questions