简体   繁体   中英

Need help running this old Matlab code

I want to implement FFT(1d) using Matlab arrays. Upon googling I came across this code . However, this seems to be an old code which was written in 2007 and I'm using Matlab 2012.

I've got errors like " index exceeds matrix dimensions ". So could someone make changes to the code so that I can run it on my computer?

PS: I know Matlab has an inbuilt fft routine. However I want to implement it using Matlab arrays as I will be using this code for some other purpose in the future. Also I'll need the fft to work for arrays of size 44k so the basic DFT implementation won't work then.

The problem with the code is at line 3:

 p=ceil(l)

The code assumes your input signal to be of length in powers of 2 (ie 4096 samples or 9192 for example). p in here will create a matrix larger than your input if you use numbers less than power of 2.

There are 2 possible fixes:

1, pad your input with additional zeros using padarray until it reaches the next power of 2.

2, change the ceil into floor to cut down the data evaluated. However, this will mean that only part of your input is evaluated.

If you want to go for method 1, here is what you need to do:

change the function to this:

size=length(input);
l=log2(size);
p=ceil(l); 
input(2^p) = 0;
Y=input'; %//NOTE HERE!!!!! the `'` is used if your data is a COLUMN vector, if it is a ROW VECTOR remove this `'`
N = 2^p;
N2=N/2; 
YY = -pi*sqrt(-1)/N2; 
WW = exp(YY);  
JJ = 0 : N2-1;  
 W=WW.^JJ;
for L = 1 : p-1
   u=Y(:,1:N2);
   v=Y(:,N2+1:N);
   t=u+v;
   S=W.*(u-v);
   Y=[t ; S];
   U=W(:,1:2:N2);
   W=[U ;U];
   N=N2;
   N2=N2/2;
end;
u=Y(:,1);
v=Y(:,2);
Y=[u+v;u-v];
Y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM