首页 > 解决方案 > 如何使用基本音乐信号的 FFT 获取持续时间和顺序?

问题描述

我使用不同的频率窦创建了 6 个音符。我将它们组合成一个向量来创建一个基本旋律。每个音符都有自己的时间,没有音符重叠和音符之间的空白时间。我想要做的是获得音符的持续时间和顺序。下面的代码是我可以进步的程度:

clear;
fs=5000;                %sampling frequency
t=   0:1/fs:3-1/fs;     %time of song (3sec)
ta=  0:1/fs:0.5-1/fs;   %time of note a    (0.5sec)
tb=0.5:1/fs:0.9-1/fs;   %time of note b    (0.4sec)
tc=  0.9:1/fs:1.2-1/fs; %time of note c    (0.3sec)
td=1.2:1/fs:2.2-1/fs;   %time of note d    (1.0sec)
te=  2.2:1/fs:2.4-1/fs; %time of note e    (0.2sec)
tf=2.4:1/fs:3-1/fs;     %time of note f    (0.6sec)
a=sin(2*pi*350*ta);     %note a (350Hz)
b=sin(2*pi*450*tb);     %note b (450Hz)
c=sin(2*pi*550*tc);     %note c (550Hz)
d=sin(2*pi*650*td);     %note d (650Hz)
e=sin(2*pi*750*te);     %note e (750Hz)
f=sin(2*pi*850*tf);     %note f (850Hz)
song=[a,d,f,c,b,e];   %combination of notes that creates 3 sec song
%sound(song);
song_fft=fft(song);
len=length(song);
P2 = abs(song_fft/len);
P1 = P2(1:len/2+1);   
P1(2:end-1) = 2*P1(2:end-1);
f = fs*(0:(len/2))/len;
%plot(t,song)
plot(f,P1) ;          %single sided spectrum of song
title('Single-Sided Amplitude Spectrum of song');
xlabel('f (Hz)');
ylabel('|P1(f)|');
[pks,locs]=findpeaks(P1,'minpeakheight',0.02,'minpeakdistance',250);
fprintf("*********************peak_frequencies**********************")
f(locs(1))
f(locs(2))
f(locs(3))
f(locs(4))
f(locs(5))
f(locs(6))
fprintf("*******************peak_heigths****************************")
pks(1)
pks(2)
pks(3)
pks(4)
pks(5)
pks(6)

标签: matlabsignal-processingfftvoice

解决方案


推荐阅读