二、案例简介

.m是MATLAB源代码文件,.fig是MATLAB的GUI文件,.jpg和.wav分别是一张图和一段语音,是项目所需的资源文件。

在程序最开始,会默认打开目录中的.wav文件,播放一段语音,内容就是“welcome to my piano”。后面还有一个label,它显示的内容也是这句话,label的标志是一个笑脸,就是目录中的.jpg文件。之后就进入了程序的正式界面。

上方标题之下是2个坐标面板,左右分别显示当前琴音的时域、频域波形。右侧框内显示了琴音的频率数值,当前按下的是小字一组的g(so音),其频率为392Hz。下面的单选框是选择坐标值有无虚线网格。下方的按键是对应计算机键盘作出来的,因为当时的初步想法是按下某一个键盘就响起对应的琴音,但是后来没能实现,所以只好老老实实地用鼠标点击。我一共是作了5组音键:大字组、小字组、小字一组、小字二组、小字三组,频率由低变高。

三、部分源代码

%---------Designer:Tang Men-------%
%-----------Time:02.05.2021--------------%


function varargout = MyPiano(varargin)
% MYPIANO MATLAB code for MyPiano.fig
% MYPIANO, by itself, creates a new MYPIANO or raises the existing
% singleton*.
%
% H = MYPIANO returns the handle to a new MYPIANO or the handle to
% the existing singleton*.
%
% MYPIANO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MYPIANO.M with the given input arguments.
%
% MYPIANO('Property','Value',...) creates a new MYPIANO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MyPiano_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MyPiano_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help MyPiano

% Last Modified by GUIDE v2.5 15-Jul-2017 13:18:02

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MyPiano_OpeningFcn, ...
'gui_OutputFcn', @MyPiano_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before MyPiano is made visible.
function MyPiano_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to MyPiano (see VARARGIN)

% Choose default command line output for MyPiano
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes MyPiano wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = MyPiano_OutputFcn(hObject, eventdata, handles)
smile1 = imread('smile1.jpg');
s = 'Welcome To MyPiano!';
hs = msgbox(s,'Welcome!','custom',smile1);
ht = findobj(hs, 'Type', 'text'); %change the size of words
set(ht, 'FontSize', 12, 'Unit', 'normal','FontName','Times New Roman');
%position [a b c d]determine the position of the msgbox, [a b] represent the coordinate of the point
% in the lower left quarter,c and d respectively express the width and height of the msgbox.
%It will show the former msgbox,setting proper coordinate will make the latter msgbox cover the former one.
set(hs,'Position',[480 380 280 80]);
fn = uigetfile('WelcomeToMyPiano.wav','Open File');
[y,fs] = audioread(fn);
sound(y,fs);
varargout{1} = handles.output;


%function dawy(y,t)
function drawy(y,t)
%Vectors must be the same length.but the time frame showed in the axes can be adjusted,like number'100'can be changed
plot(t(1:100),y(1:100));
xlabel('time (s)');ylabel('amplitude');
zoom on;


%function drawfft(y,fs)
function drawfft(y,fs)
N=1024; % make a self-defined drawfft(y,N,fs)
fy=fft(y,N); fy=abs(fy);
f=(0:N/2-1)*fs/N;
plot(f,fy(1:N/2));
xlabel('Frequency (Hz)');ylabel('amplitude');
zoom on;


function pushbutton1_Callback(hObject, eventdata, handles)
global fs t
%To control time frame to 3/8 s,make fs as 4000 in order to limite the frame of frequency to 1-2000
%and this range is suitable for the biggest frequency 1975.5 Hz
fs = 4000;t = (0:1500)*(1/fs);
f=65.4;
y = cos(2*pi*f*t);
sound(y);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f); %pay attention to the function set


function pushbutton2_Callback(hObject, eventdata, handles)
global fs t
f=73.4;
y = cos(2*pi*f*t);
sound(y);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y);
set(findobj('Tag','edit1'),'String',f);


function pushbutton3_Callback(hObject, eventdata, handles)
global fs t
f=82.4;
y = cos(2*pi*f*t);
sound(y);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton4_Callback(hObject, eventdata, handles)
global fs t
f=87.3;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton5_Callback(hObject, eventdata, handles)
global fs t
f=98.0;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton6_Callback(hObject, eventdata, handles)
global fs t
f=110.0;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton7_Callback(hObject, eventdata, handles)
global fs t
f=123.5;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton11_Callback(hObject, eventdata, handles)
global fs t
f=130.8;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton12_Callback(hObject, eventdata, handles)
global fs t
f=146.8;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton13_Callback(hObject, eventdata, handles)
global fs t
f=164.8;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton14_Callback(hObject, eventdata, handles)
global fs t
f=174.6;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);


function pushbutton15_Callback(hObject, eventdata, handles)
global fs t
f=196.0;
y = cos(2*pi*f*t);
sound(y,fs);
axes(handles.axes1);
drawy(y,t);
axes(handles.axes2);
drawfft(y,fs);
set(findobj('Tag','edit1'),'String',f);

四、运行结果

【数字信号】基于matlab GUI简易电子琴(英文版)【含Matlab源码 873期】_参考文献

五、matlab版本及参考文献

1 matlab版本

2014a

2 参考文献

[1] 沈再阳.精通MATLAB信号处理[M].清华大学出版社,2015.

[2]高宝建,彭进业,王琳,潘建寿.信号与系统——使用MATLAB分析与实现[M].清华大学出版社,2020.

[3]王文光,魏少明,任欣.信号处理与系统分析的MATLAB实现[M].电子工业出版社,2018.