我试图从音频文件中获取宽度为17的矩阵。 我使用错误。*矩阵尺寸必须同意。它是什么意思,我想如何解决它?
错误位于windowed_frame = window.*input(((k-1)*bins*overlap)+1:(((k-1))*bins*overlap)+1+bins-1);
[input fs] = wavread('3_1.wav');
fn = fs/2;
bins = 512; %useable bins
frame_length = bins;
window = hamming(frame_length);
overlap = .5;
number_of_frames = (length(input)/(frame_length)/overlap) - 1;
for k=1:number_of_frames
windowed_frame = window.*input(((k-1)*bins*overlap)+1:(((k-1))*bins*overlap)+1+bins-1);
fft_window = fft(windowed_frame,bins*2);
fft_window = abs(fft_window(1:bins));
fft_energy = fft_window.^2;
freq_vector = [fn/bins:fn/bins:fn];
编辑:完整代码 function test_energy = energy_per_frame(file)
[输入fs] = wavread('1_2.wav');
fn = fs/2;
bins = 512; %useable bins
frame_length = bins;
window = hamming(frame_length);
overlap = .5;
number_of_frames = (length(input)/(frame_length)/overlap) - 1;
for k=1:number_of_frames
windowed_frame = (window.*input(((k-1)*bins*overlap)+1:(((k-1))*bins*overlap)+1+bins-1)).';
fft_window = fft(windowed_frame,bins*2);
fft_window = abs(fft_window(1:bins));
fft_energy = fft_window.^2;
freq_vector = [fn/bins:fn/bins:fn];
%map bark (z) to bins (1-512) and frequency (freq_vector)
for i=1:length(freq_vector)
z(i) = 13*atan(0.76*(freq_vector(i)/1000)) + 3.5*atan(((freq_vector(i)/1000)/7.5).^2);
end
z = floor(z);
for j=1:length(z);
if z(j) == 17
z(j)=16;
end
end
temp = 0;
temp_sum = 0;
count = 0;
%calculate the energy per bark band
for i=1:length(z)
if temp == z(i)
temp_sum = fft_energy(i)+ temp_sum;
test_energy(k,temp+1) = temp_sum;
count = count+1;
end
if temp ~= z(i)
test_energy(k,temp+1) = temp_sum/count;
temp = temp+1;
temp_sum = fft_energy(i);
count = 1;
end
end
end
答案 0 :(得分:0)
.*
运算符是逐元素乘法。在您的情况下,这意味着window
中的每个元素都乘以子矩阵input
中的相应元素,这是操作的结果:
input(((k-1)*bins*overlap)+1:(((k-1))*bins*overlap)+1+bins-1)
显然,两个矩阵都有不同的维度,您可以通过输入以下内容来验证:
size(window)
size(input(((k-1)*bins*overlap)+1:(((k-1))*bins*overlap)+1+bins-1))
我怀疑这是一个行列问题:生成的子矩阵input
是1 x 512行行向量,而windows
是512-by- 1 列向量。
解决方案:尝试添加点撇号(.'
),实际上是MATLAB中的transpose operator(与'
运算符相对, complex-conjugate转置,如果你的数据很复杂,可能不是你想要的那样),在子矩阵input
的末尾,如下所示:
windowed_frame = window .* input(blah blah...).' %// <-- Note the .'
我相信这应该可以纠正你的问题。
更通用的解决方案是使用colon operator(:
)强制两个向量成为列向量:
input_window = input(((k-1)*bins*overlap)+1:(((k-1))*bins*overlap)+1+bins-1);
windowed_frame = window(:) .* input_window(:);