我正在研究一个脚本,该脚本在循环中执行给定短音频文件的FFT。我也想存储峰值频率,但是我不知道该怎么做。
代码类似于以下内容:
n = ...
Frequencies = zeros(1,n); % Allocating memory for the peak frequencies
for k = 1:n
str(k)
textFileName = [num2str(k) '.m4a'];
[data,fs] = audioread(textFileName);
%...
% Fast Fourier transform and plotting part works ok
%...
[peaks,frequencies] = findpeaks(abs(cutP2),cutf,'MinPeakHeight',10e-3);
% Here starts the problem
maximum_Peak = max(peaks);
Frequencies(k) = ... % I need to store the frequency which is coupled
% with the maximum amplitude but I do not know how
end
close(figure(n)) %The loop opens one redundant blank plot, I could not
%find out any other way to close it
我不想存储峰值频率的幅度,但是要存储峰值幅度的频率。如果您能帮我解决这个多余的数字,我将很高兴。我试图实现一个if
语句,但是没有用。
答案 0 :(得分:1)
max
包含第二个输出,该输出返回最大值的索引。使用此第二个值来存储感兴趣的值。
[maximum_Peak,I] = max(peaks); %Note I Use 'I' for index - personal habit
Frequencies(k) = frequencies(I);
此外,如果您的目标只是找到最高点,那么findpeaks可能会过分杀伤您,并且可能使用:
[maximum_Peak,I] = max(abs(cutP2));
%Might want to check that max is high enough
Frequencies(k) = cutf(I);
请注意,尽管代码相似,但并不相同,取决于您要执行的操作。
最后,一些未经请求的建议,您使用frequencies
和Frequencies
有点危险。通常,基于大小写的差异不是一个好主意。考虑将后者重命名为freq_of_max_amp