matlab 实现简易电子琴
Posted 胡刚2021
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab 实现简易电子琴相关的知识,希望对你有一定的参考价值。
1.创建项目
2.布局控件
布局如下
3.添加各个Button的回调函数
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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, Fs);
运行结果,按下1会先播放1秒的声音,然后显示出波形
同理,Button2-9的回调函数中只需要分别改变频率为
262,294,330,349,392,440,494,523
各个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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, 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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, 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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, 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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, 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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, 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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, 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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, 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秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, Fs);
% --- Executes on button press in pushbutton9.
function pushbutton9_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* 523 *t);%生成频率为523hz,幅度为0.3,时间为1秒的正弦波
plot(t, y1);
axis([0, 0.01, -0.5, 0.5]);
wavplay(y1, Fs);
以上是关于matlab 实现简易电子琴的主要内容,如果未能解决你的问题,请参考以下文章
DSP视频教程DSP视频教程第7期:Matlab的wifi通信实现,大大方便波形数据远程分析(2022-04-22)