我正在尝试编写一个.m文件来从音轨中提取能量特征,但是我似乎在实现它时遇到了麻烦:
% Formula for calculating RMS
[f, fs, nb] = wavread('Three.wav');
frameWidth=441; %10ms
numSamples=length(x);
numFrames=(numSamples/1);
energy(frame)=0;
for frame=1:numFrames,
startSample=(frame-1)*frameWidth+1;
endSample=startSample+frameWidth-1;
% Calculate frame energy
for i=startSample:endSample
energy(frame)=energy(frame)+x(i)^2;
end
end
我在MATLAB中运行该文件并得到以下错误:
??? Attempted to access x(2); index out of bounds because numel(x)=1. Error in ==> myrms at 12 energy(frame)=energy(frame)+x(i)^2;
任何帮助将非常感激。
最佳答案
您应该使用f
而不是x
,因为f
是从.wav文件加载的实际信号。变量x
可能只是工作空间中的其他标量,这就是为什么您看到错误的原因。
还应该对您的代码进行其他一些更正/改进。首先,作为Paul R pointed out,您需要更正numFrames
的计算方式。其次,energy
应该初始化为零 vector 。第三,您可以将内部for循环简化为单行矢量化操作。
这是我重写代码的方式(编辑:基于注释,我更新了代码以保存循环中计算出的一些额外变量):
[y, fs, nb] = wavread('Three.wav'); %# Load the signal into variable y
frameWidth = 441; %# 10 msec
numSamples = length(y); %# Number of samples in y
numFrames = floor(numSamples/frameWidth); %# Number of full frames in y
energy = zeros(1,numFrames); %# Initialize energy
startSample = zeros(1,numFrames); %# Initialize start indices of frame
endSample = zeros(1,numFrames); %# Initialize end indices of frame
for frame = 1:numFrames %# Loop over frames
startSample(frame) = (frame-1)*frameWidth+1; %# Starting index of frame
endSample(frame) = frame*frameWidth; %# Ending index of frame
frameIndex = startSample(frame):endSample(frame); %# Indices of frame samples
energy(frame) = sum(y(frameIndex).^2); %# Calculate frame energy
end
关于matlab - MATLAB中音频所需的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3406361/