MATLAB代码出错

时间:2013-06-20 15:50:27

标签: matlab debugging

所以我写了下面的代码,我得到一个错误,上面写着:

Error using ==> times
Matrix dimensions must agree.
Error in ==> Untitled2 at 28
edges =ifft(fft(song).*fft(myFilter));

这是我使用的代码:

[song,FS] = wavread('c scale fast.wav');
P     = 20000/44100*FS;                   % length of filter 
N     = length(song);                     % length of song
t     = 0:1/FS:(length(song)-1)/FS;       % and get sampling frequency
song      = song/max(abs(song));

% Gaussian Filter
x    = linspace( -1, 1, N); % create a vector of N values between -1 and 1 inclusive
sigma    = 0.335; % standard deviation used in Gaussian formula
myFilter = -x .* exp( -(x.^2)/(2*sigma.^2));% compute first derivative
myFilter = myFilter / sum( abs( myFilter ) ); % normalize

% fft convolution
song     = song(:);
myFilter = myFilter(:);
song(length(song)+length(myFilter)-1) = 0;
myFilter(length(song)+length(myFilter)-1) = 0;
edges    = ifft(fft(song).*fft(myFilter));
tedges   = edges(P/2:N+P/2-1);         % shift by P/2 so peaks line up w/ edges
tedges   = tedges/max(abs(tedges));    % normalize

有人可以告诉我这里有什么问题吗?

1 个答案:

答案 0 :(得分:3)

很快,您的错误会告诉您错误在哪一行。所以你去那条线。它还告诉你导致错误的函数是什么 - “次”。你不知道那是什么,所以你在doc文件中查找它。原来它是。*运算符。您的错误告诉您“矩阵尺寸不一致” - 这是什么意思?矩阵,尺寸,不同意 - 您的两个变量是不同的大小。

song(length(song)+length(myFilter)-1) = 0;
myFilter(length(song)+length(myFilter)-1) = 0;
edges =ifft(fft(song).*fft(myFilter));

这些行是您的错误所在。您正在增加song的长度,然后将myFilter定义为song的长度,然后使用song尝试。*(转换)。除非你告诉fft / ifft要使用多少个点,否则你得到的输出大小与输入相同,所以这不会起作用。我想你希望它们两者的长度相同。然后,这样的事情应该这样做:

song(length(song)+length(myFilter)-1) = 0;
myFilter(length(song)) = 0;

我认为你不打算改变歌曲的长度。我想你需要做的就是:

myFilter(length(song)) = 0;

而不是

song(length(song)+length(myFilter)-1) = 0;
myFilter(length(song)+length(myFilter)-1) = 0;