-1

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

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

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

4

1 回答 1

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;
于 2013-06-20T16:02:24.047 回答