在MATLAB中计算光谱质心
我希望检查我的WAV文件中的光谱质心(SC) 我使用以下MATLAB代码来实现此目的:在MATLAB中计算光谱质心,matlab,fft,centroid,spectral,Matlab,Fft,Centroid,Spectral,我希望检查我的WAV文件中的光谱质心(SC) 我使用以下MATLAB代码来实现此目的: function C = SpectralCentroid2(signal,windowLength, step, fs) % function C = SpectralCentroid(signal,windowLength, step, fs) % % This function computes the spectral centroid feature of an audio signal % A
function C = SpectralCentroid2(signal,windowLength, step, fs)
% function C = SpectralCentroid(signal,windowLength, step, fs)
%
% This function computes the spectral centroid feature of an audio signal
% ARGUMENTS:
% - signal: the audio samples
% - windowLength: the length of the window analysis (in number of samples)
% - step: the step of the window analysis (in number of samples)
% - fs: the sampling frequency
%
% RETURNS:
% - C: the sequence of the spectral centroid feature
%
signal = signal / max(abs(signal));
curPos = 1;
L = length(signal);
numOfFrames = floor((L-windowLength)/step) + 1;
H = hamming(windowLength);
m = ((fs/(2*windowLength))*[1:windowLength])';
C = zeros(numOfFrames,1);
for (i=1:numOfFrames)
window = H.*(signal(curPos:curPos+windowLength-1));
FFT = (abs(fft(window,2*windowLength)));
FFT = FFT(1:windowLength);
FFT = FFT / max(FFT);
C(i) = sum(m.*FFT)/sum(FFT);
if (sum(window.^2)<0.010)
C(i) = 0.0;
end
curPos = curPos + step;
end
C = C / (fs/2);
我不知道为什么会出现这个问题。汉明
窗口H
是一个列向量,而信号是一个行向量(在注释中声明)。特别是在这一行:
window = H.*(signal(curPos:curPos+windowLength-1));
。。。这是代码中的第23行,您正在尝试逐点将列向量乘以行向量,这就是维度不兼容的原因
要解决此问题,请在运行代码之前确保信号是列向量。因此,将其添加为函数声明后代码的第一行
signal = signal(:);
这确保了您的信号是列向量,因此您可以声明行向量或列向量的1D信号,并且您的代码仍然可以工作
总之,得到空矩阵的原因是因为信号仅由一个点组成。你需要一个长信号。。。至少和窗口大小一样长,这样才能工作。似乎信号应该是一个数字数组,而不仅仅是一个数字,请尝试1:366383
就像测试>SpectralCentroid2(1:366383,1024,128,44100)
使用时出错。*矩阵维度必须一致。SpectralCentroid2(第24行)窗口中的错误=H.*(信号(curPos:curPos+windowLength-1));Matlab上面说了错误消息。。
signal = signal(:);