简体   繁体   English

需要帮助来运行旧的Matlab代码

[英]Need help running this old Matlab code

I want to implement FFT(1d) using Matlab arrays. 我想使用Matlab数组实现FFT(1d)。 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. 但是,这似乎是2007年编写的旧代码,而我正在使用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. PS:我知道Matlab具有内置的fft例程。 However I want to implement it using Matlab arrays as I will be using this code for some other purpose in the future. 但是我想使用Matlab数组来实现它,因为将来我会将这段代码用于其他目的。 Also I'll need the fft to work for arrays of size 44k so the basic DFT implementation won't work then. 另外,我还需要fft来处理大小为44k的数组,因此基本的DFT实现将无法使用。

The problem with the code is at line 3: 代码的问题在第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). 该代码假定您的输入信号的长度为2的幂(例如4096个样本或9192)。 p in here will create a matrix larger than your input if you use numbers less than power of 2. 如果使用小于2的幂的数字,则此处的p将创建一个比输入大的矩阵。

There are 2 possible fixes: 有2个可能的修复:

1, pad your input with additional zeros using padarray until it reaches the next power of 2. 1,使用padarray用额外的零填充输入,直到达到下一个2的幂。

2, change the ceil into floor to cut down the data evaluated. 2,将ceil改为floor以减少评估数据。 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: 如果要使用方法1,则需要执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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