MATLAB脚本确定具有最大功率的频率

时间:2013-08-28 03:51:29

标签: matlab spectrum spectrogram

我正在使用移动音频源和静止观察者进行线性运动测量。这里描述:http://www.animations.physics.unsw.edu.au/labs/Doppler/doppler_lab.html

如何编写MATLAB脚本来获取音频文件中功率最大的频率的样本编号?

1 个答案:

答案 0 :(得分:3)

您需要的是时间 - 频率本地化信息。这可以使用Short-time Fourier transform获得。还有许多其他时频分析技术,STFT是最简单的,因此是一个很好的起点。这是一个简单的代码,可以帮助您理解这个概念:

% Parameters
Fs = 44100; % Sample rate (44100 Hz)
t = 0:1/Fs:5; % Time instances
t1 = 5; % End time of signal, 5 secs
f0 = 440; % frequency swiped from 440 Hz
f1 = 880; % to 880 Hz

% Signal generation
audio = chirp(t,f0,t1,f1);
% wavplay(audio,Fs) % to play the audio


% Signal analysis
window = 2050; % Should be minimum twice the maximum frequency we want to analyze
noverlap = 1025; % 50% overlap
nfft = 44100;

[S,F,T,P] = spectrogram(audio,window,noverlap,nfft,Fs); % Spectrogram takes the STFT of the signal
% P matrix contains the power spectral density of each segment of the STFT

% Plotting results
imagesc(real(S)) % frequency-time Plot of the signal
ylim([0 1000])
xlabel('Time (secs)')
ylabel('Frequency (Hz)')
title('Time-Frequency plot of a Audio signal')

要获取样本编号,您只需找到感兴趣的频率出现的时间实例,并使用采样频率计算样本编号。

P是功率谱密度矩阵。沿y轴是频率,x轴是时间,每个瞬间由每个频率贡献的功率存储在该矩阵中。您需要整个矩阵中具有最高值的元素。下面的代码应该有效:

[maxElement, maxElementTimeIndex] = max(max(P, [], 1)); % max(P, [], 1) finds maximum power for each time instance, max(max(P,[],1)) finds maximum for the entire 2D matrix.
maxPoweredSampleInAudioSignal = (maxElementTimeIndex-1) * Fs; % This calculation is made within the limitations of STFT, so approximately here the maximum power for any frequency is present