【信号处理】数字电子琴设计与实现matlab源码

一、简介

1 背景
电子琴是随着电子技术的广泛应用而产生。1959年,世界上第一台全晶体管双排键电子琴在日本诞生。从此,电子琴技术不断发展成熟,电子琴的普及也越来越广。
电子琴在中国的推广始于20世纪八十年代,至今在音色、音质、演奏的便利性等方面都达到了相当成熟的地步,而且随着集成电路技术的发展,其升级换代可以在保持原有结构不变的情况下,通过简单的芯片更换实现。但是,电子琴也有明显的不足之处:与非电子乐器,如钢琴、管弦乐器等的协奏问题,限制了电子琴在重要音乐场所的使用,这极大的影响了电子琴的应用和推广。协调电子琴与非电子乐器的协奏,是当前音乐界人士和电子琴开发商的当务之急。

2 电子琴发音原理
物体振动时会发出声音,振动的频率不同,声音的音调就不同。在电子琴里,虽然没有振动的弦、簧、管等物体,却有许多特殊的电装置,每个电装置一工作,就会使喇叭发出一定频率的声音。当按动某个琴键时,就会使与它对应的电装置工作,从而使喇叭发出某种音调的声音。
电子琴的音量控制器,实质上是一个可调电阻器。当转动音量控制器旋扭时,可调电阻器的电阻就随着变化。电阻大小的变化,又会引起喇叭声音强弱的变化。所以转动音量控制旋扭时,电子琴发声的响度就随之变化。
当乐器发声时,除了发出某一频率的声音──基音以外,还会发出响度较小、频率加倍的辅助音──谐音。我们听到的乐器的声音是它发出的基音和谐音混合而成的。不同的乐器发出同一基音时,不仅谐音的数目不同,而且各谐音的响度也不同。因而使不同的乐器具有不同的音品。在电子琴里,除了有与基音对应的电装置外,还有与许多谐音对应的电装置,适当地选择不同的谐音电装置,就可以模仿出不同乐器的声音来。

3 基于 Matlab的数字电子琴实现原理
振动频率不同,音调就不同。电子琴的每一按键对应一个频率不同的正弦波,按下按键,产生正弦波,播放波形即可听到对应按键的基音。

二、源代码

 function varargout = digtai_piano(varargin)
% DIGTAI_PIANO M-file for digtai_piano.fig
%      DIGTAI_PIANO, by itself, creates a new DIGTAI_PIANO or raises the existing
%      singleton*.
%
%      H = DIGTAI_PIANO returns the handle to a new DIGTAI_PIANO or the handle to
%      the existing singleton*.
%
%      DIGTAI_PIANO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DIGTAI_PIANO.M with the given input arguments.
%
%      DIGTAI_PIANO('Property','Value',...) creates a new DIGTAI_PIANO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before digtai_piano_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to digtai_piano_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

% Copyright 2002-2003 The MathWorks, Inc.

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

% Last Modified by GUIDE v2.5 28-Sep-2007 10:39:23

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @digtai_piano_OpeningFcn, ...
                   'gui_OutputFcn',  @digtai_piano_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 digtai_piano is made visible.
function digtai_piano_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 digtai_piano (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = digtai_piano_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A=1;
f=131;

Fs=11025;
P=60;

T=1.0/f;
dt=T/Fs;
N=T/dt;
t=linspace(0,1,N);
y=A*sin(2*pi*f*t+P);

Phandel=findobj('Tag','edit1')
set(Phandel,'String',f,'FontSize',15.0)

plot(t,y,'r');
grid ;
axis([0,0.1,-1.5,1.5]);
wavplay(y,11025);
pause(1);
cla;





% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A=1;
f=147;

Fs=11025;
P=60;

T=1.0/f;
dt=T/Fs;
N=T/dt;
t=linspace(0,1,N);
y=A*sin(2*pi*f*t+P);

Phandel=findobj('Tag','edit1')
set(Phandel,'String',f,'FontSize',15.0)

plot(t,y,'r');
grid ;
axis([0,0.1,-1.5,1.5]);
wavplay(y,11025);
pause(1);
cla;


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A=1;
f=165;

Fs=11025;
P=60;

T=1.0/f;
dt=T/Fs;
N=T/dt;
t=linspace(0,1,N);
y=A*sin(2*pi*f*t+P);

Phandel=findobj('Tag','edit1')
set(Phandel,'String',f,'FontSize',15.0)

plot(t,y,'r');
grid ;
axis([0,0.1,-1.5,1.5]);
wavplay(y,11025);
pause(1);
cla;


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A=1;
f=175;

Fs=11025;
P=60;

T=1.0/f;
dt=T/Fs;
N=T/dt;
t=linspace(0,1,N);
y=A*sin(2*pi*f*t+P);

Phandel=findobj('Tag','edit1')
set(Phandel,'String',f,'FontSize',15.0)

plot(t,y,'r');
grid ;
axis([0,0.1,-1.5,1.5]);
wavplay(y,11025);
pause(1);
cla;


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A=1;
f=196;

Fs=11025;
P=60;

T=1.0/f;
dt=T/Fs;
N=T/dt;
t=linspace(0,1,N);
y=A*sin(2*pi*f*t+P);

Phandel=findobj('Tag','edit1')
set(Phandel,'String',f,'FontSize',15.0)

plot(t,y,'r');
grid ;
axis([0,0.1,-1.5,1.5]);
wavplay(y,11025);
pause(1);
cla;


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A=1;
f=220;

Fs=11025;
P=60;

T=1.0/f;
dt=T/Fs;
N=T/dt;
t=linspace(0,1,N);
y=A*sin(2*pi*f*t+P);

Phandel=findobj('Tag','edit1')
set(Phandel,'String',f,'FontSize',15.0)

plot(t,y,'r');
grid ;
axis([0,0.1,-1.5,1.5]);
wavplay(y,11025);
pause(1);
cla;

三、运行结果

在这里插入图片描述

四、备注

完整代码或者代写添加QQ1575304183

往期回顾>>>>>>

【信号处理】基于HMM的睡眠状态检测matlab源码

【信号处理】基于小波变换的音频水印嵌入提取matlab源码

【信号处理】基于遗传算法的VST混响matlab源码

【信号处理】脉搏信号之脉率存档matlab源码含GUI

猜你喜欢

转载自blog.csdn.net/qq_34763204/article/details/113796290