如何使用matlab演奏音乐


效果展示:

理论基础

声音是一种波,只要能绘制出波的曲线就可以生成其相对应的声音。

sound 函数

利用 sound 函数播放正弦函数对应的声音

FS=8192;
x=linspace(0,2*pi,FS);
y=sin(x);
sound(y,FS)

FS 为采样率,如此播放是人耳无法听到声音的,因为利用此函数播放时一秒钟中内完成一个波的 8192 个点,周期过小,频率过低为次声波人耳无法识别。

调节频率


FS=8192;
x=linspace(0,2*pi,FS);
y=sin(440*x);
sound(y,FS)

使用此段程序即可播放出滴的声音,440 的频率是音乐标准 A 频率。

影响声音的因素

** 采样率**

FS1=8192;
FS2=20000;
FS3=1000;
x1=linspace(0,2*pi,FS1);
y1=sin(440*x1);
x2=linspace(0,2*pi,FS2);
y2=sin(440*x2);
x3=linspace(0,2*pi,FS3);
y3=sin(440*x3);
sound(y1,FS1)
sound(y2,FS2)
sound(y3,FS3)

播放对比后可以发现采样率对声音影响不大,对声音影响最大的还是频率。

频率/周期

FS=8192;
x=linspace(0,2*pi,FS);
y1=sin(440*x);
y2=sin(500*x);
y3=sin(650*x);
sound(y1,FS)sound(y2,FS)sound(y3,FS)

依次播放 y1,y2,y3 可以发现在改变频率后播放声音出现明显差异。

十二平均律

那么我们如何确定合适的频率来播放声音呢?下面将引入十二平均律的概念(引用自百度)
十二平均律是指八度的音程按波长比例平均分成十二等份,每一等份称为一个半音(小二度)。一个大二度则是两等份,称为全音。将一个八度分成12等份有着惊人的一些巧合。这是因为它的纯五度音程的两个音的波长比(即1/2的7/12次方)约为0.6674,与2/3,约为0.6667,非常接近。具体见下表

音程名称 频率
完全一度 440
小二度 466
大二度 493
小三度 523
大三度 554
完全四度 587
增五度 622
完全五度 659
小六度 698
大六度 739
小七度 783
大七度 830
完全八度 880

重要的频率为 523,587,659,698,783,880,988 即为 C 调哆来咪发唆啦西。

FS=8192;
x=linspace(0,2*pi,FS);
freqs=[523,587,659,698,783,880,988];
y1=sin(freqs(1)*x);
y2=sin(freqs(2)*x);
y3=sin(freqs(3)*x);
y4=sin(freqs(4)*x);
y5=sin(freqs(5)*x);
y6=sin(freqs(6)*x);
y7=sin(freqs(7)*x);
y=[y1 y2 y3 y4 y5 y6 y7];
sound(y,FS)

如此即可播放出从哆到西,但是可以很明显发现如此播放声音没有起伏波动仅有频率变化,因此十分生硬。声音在传播过程中是逐渐减弱的因此波的起伏应当逐渐减小。

优化波形

如何实现振幅的逐渐震荡减小呢,只需在 sin(freps(1)*x)后点乘一个 1-x/(2*pi),即可实现震荡减小。
需要延音的如何处理,只需要将采样率和 1-x/(2*pi)改为两倍和 1-x/(4*pi)即可。
示例如下:

FS=8192;
x=linspace(0,2*pi,FS);
x2=linspace(0,2*pi*2,FS*2);
freqs=[523,587,659,698,783,880,988];
y1=sin(freqs(1)*x).*(1-x/(2*pi));
y2=sin(freqs(2)*x).*(1-x/(2*pi));
y3=sin(freqs(3)*x).*(1-x/(2*pi));
y4=sin(freqs(4)*x).*(1-x/(2*pi));
y5=sin(freqs(5)*x).*(1-x/(2*pi));
y6=sin(freqs(6)*x).*(1-x/(2*pi));
y7=sin(freqs(7)*x2).*(1-x2/(4*pi));
y=[y1 y2 y3 y4 y5 y6 y7];
plot(y)
sound(y,FS)

使用函数优化程序

定义波形函数 gen_wave.m

function y= gen_wave(tone,rythm)
%% rythm为节奏,拍数,tone为音高
FS=8192;
freqs=[523,587,659,698,783,880,988];
x=linspace(0,2*pi*rythm,floor(FS*rythm));
y=sin(freqs(tone)*x).*(1-x/(2*pi*rythm));
end

调用函数播放小星星

y1=gen_wave(1,1);
y2=gen_wave(1,1);
y3=gen_wave(5,1);
y4=gen_wave(5,1);
y5=gen_wave(6,1);
y6=gen_wave(6,1);
y7=gen_wave(5,2);
y=[y1 ,y2 ,y3, y4 ,y5 ,y6 ,y7];
plot(y)
sound(y,FS)

包络函数 1-x/(2*pi)换为 exp(-x)效果更优
成品:
脚本文件:

FS=8192;
y1_1=gen_wave_low(6,0.75);
y1_2=gen_wave(1,0.25);
y1_3=gen_wave(1,0.25);
y1_4=gen_wave_low(6,0.75);
y1_5=gen_wave_low(6,0.5);
y1_6=gen_wave_low(6,0.5);
y1_7=gen_wave(1,1);
y1=[y1_1 y1_2 y1_3 y1_4 y1_5 y1_6 y1_7];

y2_1=gen_wave(0,0.5);
y2_2=gen_wave(3,0.25);
y2_3=gen_wave_low(6,0.25);
y2_4=gen_wave(3,0.25);
y2_5=gen_wave(3,0.25);
y2_6=gen_wave(1,0.25);
y2_7=gen_wave(3,0.25);
y2_8=gen_wave(2,1);
y2_9=gen_wave(3,0.5);
y2_10=gen_wave(2,0.5);
y2=[y2_1 y2_2 y2_3 y2_4 y2_5 y2_6 y2_7 y2_8 y2_9 y2_10];

y3_1=gen_wave(1,0.5);
y3_2=gen_wave_low(6,1.5);
y3_3=gen_wave(0,1);
y3_4=gen_wave(0,0.5);
y3_5=gen_wave(5,0.25);
y3_6=gen_wave(5,0.25);
y3=[y3_1 y3_2 y3_3 y3_4 y3_5 y3_6];

y4_1=gen_wave(6,0.5);
y4_2=gen_wave(5,0.25);
y4_3=gen_wave(6,0.75);
y4_4=gen_wave(5,0.5);
y4_5=gen_wave(0,0.5);
y4_6=gen_wave(2,0.5);
y4_7=gen_wave(2,0.5);
y4_8=gen_wave(5,1);
y4=[y4_1 y4_2 y4_3 y4_4 y4_5 y4_6 y4_7 y4_8 ];

y5_1=gen_wave(5,0.5);
y5_2=gen_wave(3,1);
y5_3=gen_wave(3,1);
y5_4=gen_wave(0,1);
y5=[y5_1 y5_2 y5_3 y5_4];

y6_1=gen_wave(0,0.5);
y6_2=gen_wave(3,0.25);
y6_3=gen_wave(3,0.25);
y6_4=gen_wave(3,0.5);
y6_5=gen_wave(1,0.25);
y6_6=gen_wave(3,0.25);
y6_7=gen_wave(2,0.75);
y6_8=gen_wave(3,0.75);
y6_9=gen_wave(2,0.25);
y6_10=gen_wave(1,0.5);
y6=[y6_1 y6_2 y6_3 y6_4 y6_5 y6_6 y6_7 y6_8 y6_9 y6_10];

y7_1=gen_wave_low(6,1.75);
y7_2=gen_wave(0,1);
y7_3=gen_wave(0,0.5);
y7_4=gen_wave(2,0.25);
y7_5=gen_wave(2,0.25);
y7=[y7_1 y7_2 y7_3 y7_4 y7_5];

y8_1=gen_wave(3,0.5);
y8_2=gen_wave(2,0.25);
y8_3=gen_wave(1,0.25);
y8_4=gen_wave(2,0.5);
y8_5=gen_wave(1,0.5);
y8_6=gen_wave(0,1);
y8_7=gen_wave(2,0.5);
y8_8=gen_wave(1,0.25);
y8_9=gen_wave(1,0.25);
y8=[y8_1 y8_2 y8_3 y8_4 y8_5 y8_6 y8_7 y8_8 y8_9];

y9_1=gen_wave(2,0.25);
y9_2=gen_wave(1,1.75);
y9_3=gen_wave(0,1);
y9_4=gen_wave(0,0.5);
y9_5=gen_wave(5,0.25);
y9_6=gen_wave(5,0.25);
y9=[y9_1 y9_2 y9_3 y9_4 y9_5 y9_6];

y10_1=gen_wave_high(1,1);
y10_2=gen_wave(0,0.5);
y10_3=gen_wave_high(1,0.25);
y10_4=gen_wave_high(1,0.25);
y10_5=gen_wave_high(2,0.5);
y10_6=gen_wave(6,0.5);
y10_7=gen_wave(0,0.5);
y10_8=gen_wave(6,0.25);
y10_9=gen_wave(6,0.25);
y10=[y10_1 y10_2 y10_3 y10_4 y10_5 y10_6 y10_7 y10_8 y10_9 ];

y11_1=gen_wave_high(3,0.5);
y11_2=gen_wave(7,0.5);
y11_3=gen_wave(7,0.5);
y11_4=gen_wave(6,0.5);
y11_5=gen_wave(6,0.25);
y11_6=gen_wave(5,0.25);
y11_7=gen_wave(6,1);
y11_8=gen_wave(6,0.25);
y11_9=gen_wave(5,0.25);
y11=[y11_1 y11_2 y11_3 y11_4 y11_5 y11_6 y11_7 y11_8 y11_9];

y12_1=gen_wave(6,0.5);
y12_2=gen_wave(5,0.25);
y12_3=gen_wave_high(1,1);
y12_4=gen_wave_high(1,0.25);
y12_5=gen_wave_high(1,0.25);
y12_6=gen_wave_high(2,0.25);
y12_7=gen_wave_high(2,0.5);
y12_8=gen_wave_high(2,0.25);
y12_9=gen_wave_high(3,0.75);
y12=[y12_1 y12_2 y12_3 y12_4 y12_5 y12_6 y12_7 y12_8 y12_9];

y13_1=gen_wave(5,0.25);
y13_2=gen_wave(3,1.75);
y13_3=gen_wave(0,1);
y13_4=gen_wave(0,0.5);
y13_5=gen_wave(5,0.25);
y13_6=gen_wave(5,0.25);
y13=[y13_1 y13_2 y13_3 y13_4 y13_5 y13_6];

y14_1=gen_wave_high(1,1);
y14_2=gen_wave_high(1,0.5);
y14_3=gen_wave(6,0.25);
y14_4=gen_wave(6,0.25);
y14_5=gen_wave_high(2,0.5);
y14_6=gen_wave_high(2,0.5);
y14_7=gen_wave(6,0.5);
y14_8=gen_wave(6,0.25);
y14_9=gen_wave(6,0.25);
y14=[y14_1 y14_2 y14_3 y14_4 y14_5 y14_6 y14_7 y14_8 y14_9];

y15_1=gen_wave_high(3,0.5);
y15_2=gen_wave(7,0.5);
y15_3=gen_wave(7,0.5);
y15_4=gen_wave(6,0.25);
y15_5=gen_wave(5,0.75);
y15_6=gen_wave(6,0.5);
y15_7=gen_wave(0,0.5);
y15_8=gen_wave(6,0.25);
y15_9=gen_wave(5,0.25);
y15=[y15_1 y15_2 y15_3 y15_4 y15_5 y15_6 y15_7 y15_8 y15_9];

y16_1=gen_wave(6,0.5);
y16_2=gen_wave(5,0.5);
y16_3=gen_wave_high(1,0.5);
y16_4=gen_wave(6,0.25);
y16_5=gen_wave(6,0.25);
y16_6=gen_wave(0,1);
y16_7=gen_wave(2,0.5);
y16_8=gen_wave(2,0.25);
y16_9=gen_wave(1,1.25);
y16=[y16_1 y16_2 y16_3 y16_4 y16_5 y16_6 y16_7 y16_8 y16_9];

y17_1=gen_wave(1,1);
y17_2=gen_wave(0,1);
y17_3=gen_wave(0,1);
y17=[y17_1 y17_2 y17_3];

y18_1=gen_wave(0,0.5);
y18_2=gen_wave(3,0.25);
y18_3=gen_wave(3,0.25);
y18_4=gen_wave(3,0.5);
y18_5=gen_wave(3,0.25);
y18_6=gen_wave(3,0.75);
y18_7=gen_wave(2,0.5);
y18_8=gen_wave(3,0.5);
y18_9=gen_wave(2,0.5);
y18=[y18_1 y18_2 y18_3 y18_4 y18_5 y18_6 y18_7 y18_8 y18_9];

y19_1=gen_wave(1,0.25);
y19_2=gen_wave_low(6,1.75);
y19_3=gen_wave(0,1);
y19_4=gen_wave(0,0.5);
y19_5=gen_wave(5,0.25);
y19_6=gen_wave(6,0.75);
y19=[y19_1 y19_2 y19_3 y19_4 y19_5 y19_6];

y20_1=gen_wave(5,0.25);
y20_2=gen_wave(6,0.75);
y20_3=gen_wave(6,0.5);
y20_4=gen_wave(5,0.5);
y20_5=gen_wave(0,0.5);
y20_6=gen_wave(5,0.5);
y20_7=gen_wave(5,0.5);
y20=[y20_1 y20_2 y20_3 y20_4 y20_5 y20_6 y20_7];

y21_1=gen_wave(6,0.5);
y21_2=gen_wave(3,1.5);
y21_3=gen_wave(3,1);
y21_4=gen_wave(0,1);
y21=[y21_1 y21_2 y21_3 y21_4];

y22_1=gen_wave(0,0.5);
y22_2=gen_wave(3,0.25);
y22_3=gen_wave(3,0.25);
y22_4=gen_wave(3,0.5);
y22_5=gen_wave(3,0.25);
y22_6=gen_wave(3,0.75);
y22_7=gen_wave(2,0.5);
y22_8=gen_wave(3,0.5);
y22_9=gen_wave(2,0.25);
y22_10=gen_wave(1,0.25);
y22=[y22_1 y22_2 y22_3 y22_4 y22_5 y22_6 y22_7 y22_8 y22_9 y22_10];

y23_1=gen_wave(1,0.25);
y23_2=gen_wave_low(6,0.25);
y23_3=gen_wave(1,0.25);
y23_4=gen_wave_low(6,1.25);
y23_5=gen_wave(0,1);
y23_6=gen_wave(0,0.5);
y23_7=gen_wave(1,0.25);
y23_8=gen_wave(1,0.25);
y23=[y23_1 y23_2 y23_3 y23_4 y23_5 y23_6 y23_7 y23_8];

y24_1=gen_wave(2,0.25);
y24_2=gen_wave(1,1.75);
y24_3=gen_wave(0,0.5);
y24_4=gen_wave(2,0.25);
y24_5=gen_wave(2,0.25);
y24_6=gen_wave(2,0.5);
y24_7=gen_wave_low(5,0.25);
y24_8=gen_wave(1,2.25);
y24=[y24_1 y24_2 y24_3 y24_4 y24_5 y24_6 y24_7 y24_8];

y=[y1 ,y2 ,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13,y14,y15,y16,y17,y18,y19,y20,y21,y22,y23,y24];
plot(y);
sound(y,FS)

函数文件

function y= gen_wave(tone,rythm)
%% rythm为节奏,拍数,tone为音高
FS=8192;
if tone==0
    y=0;
else
freqs=[523,587,659,698,783,880,988];
x=linspace(0,2*pi*rythm,floor(FS*rythm));
y=sin(freqs(tone)*x).*exp(-x);
end
end
function y = gen_wave_low(tone,rythm)
Fs = 8192;
if tone==0
    y=0;
else
freqs = [261,293,329,349,392,440,494];
x = linspace(0,2 * pi * rythm,floor(Fs * rythm));
y = sin(freqs(tone) * x).*exp(-x);
end
end
function y = gen_wave_high(tone,rythm)
Fs = 8192;
if tone==0
    y=0;
else
freqs = [1047,1175,1319,1397,1568,1760,1976];
x = linspace(0,2 * pi * rythm,floor(Fs * rythm));
y = sin(freqs(tone) * x).*exp(-x);
end
end

文章作者: 不染轻裳
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 不染轻裳 !
评论
 上一篇

阅读全文

公告
公告 公告
最近一直没工夫维护站点,因为lc国际版切割导致评论和友链朋友圈还有图床也失效了,近期可能会维护但是大概要一段时间才有功夫来维护了,毕竟破的洞挺多的,但是肯定是不会跑路的发个贴证明我还活着。
2022-06-28
下一篇 

阅读全文

壬寅清明·春景当思
壬寅清明·春景当思 壬寅清明·春景当思
壬寅清明·春景当思 看了看文章日历,上次发文已经是一月份的时候,恭喜自己又成功鸽了两个月,这篇依旧是自己碎碎念的生活记录。 疫情老规矩还是先说说这一个月学习生活吧,从2月19号以来,到今天已经过了一个多月了,期间其实还是发生了很多事的,
2022-04-04
  目录