matlab - 如何使用基本音乐信号的 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)
解决方案
推荐阅读
- python-3.x - 从ini中读取特定部分
- python - Python shlex 没有右引号错误——如何处理?
- python - 使用类标签和数据集创建 numpy 数组
- php - 如果指针对象不存在或指针未定义,总是获取指针值?
- javascript - Rails 5.2.3 使用 JSON 发送 AJAX 请求但不接收具有 + 符号作为值的键值对
- nginx - Nginx 重定向到错误的 URL
- flutter - 如何使用 Flutter Route Navigation 动态更改屏幕中的元素(部分)
- windows - 我的 Visual Studio 解决方案中的某些内容导致 Windows 繁忙光标不断旋转并且永不停止,我如何找到导致此问题的原因?
- uml - 单个序列图中的多个用例
- java - 如何从 html 代码中获取图像标签到 Glide 作为 Java 中的图像源?