matlab 实现为电子琴添加泛音
Posted 胡刚2021
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab 实现为电子琴添加泛音相关的知识,希望对你有一定的参考价值。
现在为之前的 matlab 实现简易电子琴 添加泛音
其实就是在之前Button回调函数生成的正弦波的基础上,再给它点乘一个元素数量一样多的数组,这个数组的波形是一个包络线,就是下面这个样子,达到的效果就是使得原来的正弦波先上升,再下降,然后保持,最后再下降到0.
修改Button1的回调函数如下
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
Fs = 44100;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 = 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;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 = 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;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 = 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;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 = 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;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 = 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;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 = 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;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 = 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;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 = 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;%采样率为44100hz
T = 1;%时间为1秒
dt = 1.0 / Fs;%step为 1.0/44100
N = T / dt;%数组元素的个数为 T/dt
t = 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这四个数组的元素个数加起来等于44100
f1 Matlab程序学习(散点边界线/包络线)