matlab - MATLAB中音频所需的帮助

标签 matlab audio waveform wave energy

我正在尝试编写一个.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/

相关文章:

javascript - 在 P5.js 中仍然使用 Lissajou 曲线的同时在新框架上绘制图像

windows - matlab低优先级系统调用

c++程序,用于将具有恒定(但未知)列数的未知大小的csv文件(仅填充 float )读取到数组中

java版的matlab的linspace

Libsndfile库可以在iPhone iOS上使用吗?

node.js - 使用 Node js 进行 MP3 文件直播

iphone - 在 iPhone 上使用 HTML5 音频播放声音片段时出现问题

Java:从 8 位 wav 文件中检索字节数组并将其规范化为 -1.0 到 1.0

matlab - 通过点 matlab 聚类着色的等高线图

iOS - 过滤后的声音停止后可听到爆音