本文主要是介绍matlab 实现为电子琴添加泛音,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
现在为之前的 matlab 实现简易电子琴 添加泛音
其实就是在之前Button回调函数生成的正弦波的基础上,再给它点乘一个元素数量一样多的数组,这个数组的波形是一个包络线,就是下面这个样子,达到的效果就是使得原来的正弦波先上升,再下降,然后保持,最后再下降到0.
修改Button1的回调函数如下
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 247 *t);%生成频率为247hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);
就是把原来
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, Fs);
这三行代码,替换为下面的代码
A = linspace(0,0.9,8800);
D = linspace(0.9,0.8,2200);
S = linspace(0.8,0.8,18000);
R = linspace(0.8,0,15100);
adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100
f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*
plot(t, f1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(f1, Fs);
其他Button的回调更改是一模一样的
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 247 *t);%生成频率为247hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 262 *t);%生成频率为262hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 294 *t);%生成频率为294hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 330 *t);%生成频率为330hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 349 *t);%生成频率为349hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 392 *t);%生成频率为392hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 440 *t);%生成频率为440hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 494 *t);%生成频率为494hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)Fs = 44100;%采样率为44100hzT = 1;%时间为1秒dt = 1.0 / Fs;%step为 1.0/44100N = T / dt;%数组元素的个数为 T/dtt = linspace(0, T, N);%生成一个数组[0, 1.0/44100, 2.0/44100,...,44099.0/44100, 1.0]y1 = 0.3*sin(2*pi* 523 *t);%生成频率为523hz,幅度为0.3,时间为1秒的正弦波A = linspace(0,0.9,8800);D = linspace(0.9,0.8,2200);S = linspace(0.8,0.8,18000);R = linspace(0.8,0,15100);adsr = [A,D,S,R];% A,D,S,R这四个数组的元素个数加起来等于44100f1 = y1.*adsr;% 当两个一维数组相乘要使用 .*plot(t, f1);axis([0, 0.01, -0.5, 0.5]);wavplay(f1, Fs);
运行结果如下,声音听起来明显比原来柔和了许多,很像真正的电子琴的声音。
这篇关于matlab 实现为电子琴添加泛音的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!