MATLAB 中的音频需要帮助

发布于 2024-09-12 11:02:00 字数 609 浏览 0 评论 0原文

我正在尝试编写一个 .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 中运行该文件并收到以下错误:

???尝试访问x(2);索引越界,因为 numel(x)=1。 错误==> 12 点 能量(帧)=能量(帧)+x(i)^2;

任何帮助将不胜感激。

I'm trying to write an .m file to extract energy features from an audio track but I seem to be having trouble in its implementation:

% 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

I run that file in MATLAB and get the following error:

??? 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;

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潜移默化 2024-09-19 11:02:00

您应该使用 f 而不是 x,因为 f 是从 .wav 文件加载的实际信号。变量 x 可能只是您工作区中的其他标量,这就是您收到所看到的错误的原因。

您的代码还需要进行一些其他更正/改进。首先,正如 Paul R 指出的,您需要更正如何计算numFrames。其次,energy 应初始化为零向量。第三,您可以将内部 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

You should be using f instead of x, since f is the actual signal loaded from your .wav file. The variable x was probably just some other scalar in your workspace, which is why you were getting the error you saw.

There are a few other corrections/improvements that should be made to your code. First, as Paul R pointed out, you need to correct how you compute numFrames. Second, energy should be initialized as a vector of zeroes. Third, you can reduce the inner for loop to a one-line vectorized operation.

Here's how I would rewrite your code (EDIT: Based on comments, I have updated the code to save a few extra variables computed in the loop):

[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
阳光的暖冬 2024-09-19 11:02:00

这行: 不应该

numFrames=(numSamples/1);

是这样的:

numFrames=(numSamples / frameWidth);

或可能:

numFrames=((numSamples + frameWidth - 1) / frameWidth);

Shouldn't this line:

numFrames=(numSamples/1);

be something like:

numFrames=(numSamples / frameWidth);

or possibly:

numFrames=((numSamples + frameWidth - 1) / frameWidth);

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文