Matlab模擬鋼琴識別琴譜GUI

音符頻率對應表

音符 頻率/HZ 半週期/us 音符 頻率/HZ 半週期/us
低1DO 262 1908 #4FA# 740 0676
#1DO# 277 1805 中5SO 784 0638
低2RE 294 1700 #5SO# 831 0602
#2RE# 311 1608 中6LA 880 0568
低3M 330 1516 #6LA# 932 0536
低4FA 349 1433 中7SI 988 0506
#4FA# 370 1350 高1DO 1046 0478
低5SO 392 1276 #DO# 1109 0451
#5SO# 415 1205 高2RE 1175 0426
低6LA 440 1136 #2RE# 1245 0402
#6LA# 466 1072 高3M 1318 0372
低7SI 494 1012 高4FA 1397 0358
中1DO 523 0956 #4FA# 1480 0338
#1DO# 554 0903 高5SO 1568 0319
中2RE 578 0842 #5S0# 1661 0292
#2RE# 622 0804 高6LA 1760 0284
中3M 659 0759 #6LA# 1865 0268
中4FA 698 0716 高7SI 1976

("#"表示半音,用於上升或下降半個音)

我們知道用Matlab的sound函數可以發聲,但是那種聲音是很難聽的“電話鈴聲”,那該怎麼模擬成鋼琴音色呢?

每個音符有固定的基音頻率,表格如上。

matlab實測

接下來,我們打開matlab

f=440;
fs=8000;
t=1/fs:1/fs:1;
x=sin(2*pi*f*t);
sound(x,fs);

這樣點擊運行的話,是一聲電話音,但是知道頻率的話,我們就可讓它發出音樂的聲音了

fs = 4000;t = (0:1500)*(1/fs);  
f=294;
y = cos(2*pi*f*t);                                     
sound(y,fs);

上面的程序是dao音

y=1;
fs = 4000;t = (0:1500)*(1/fs);  
f1=262;
y1 = cos(2*pi*f1*t);                                     
%sound(y1,fs);
f2=294;
y2 = cos(2*pi*f2*t);                                     
%sound(y2,fs);
f3=330;
y3 = cos(2*pi*f3*t);                                     
%sound(y3,fs);
f4=349;
y4 = cos(2*pi*f4*t);                                     
%sound(y4,fs);
f5=392;
y5 = cos(2*pi*f5*t);                                     
%sound(y5,fs);
f6=440;
y6 = cos(2*pi*f6*t);                                     
%sound(y6,fs);
f7=494;
y7 = cos(2*pi*f7*t);   
%%%%%%%%%y=[y1,y2,y3,y4,y5,y6,y7];
%%%%%%%%%sound(y,fs);
k=0;
A=input('請輸入"[]",中間帶空格');
for i=1:length(A) %MATLAB下標從1開始
    switch(A(i))
        case 1
            %y=[y1];
            sound(y1,fs);
            pause(0.5);
        case 2
            %y=[y2];
             sound(y2,fs);
             pause(0.5);
        case 3
            %y=[y3];
             sound(y3,fs);
             pause(0.5);
        case 4
            %y=[y4];
             sound(y4,fs);
             pause(0.5);
        case 5
            %y=[y5];
             sound(y5,fs);
             pause(0.5);
        case 6
            %y=[y6];
             sound(y6,fs);
             pause(0.5);
        case 7
            %y=[y7];
             sound(y7,fs);
             pause(0.5);
    end
   % sound(y,fs);
end

在這裏插入圖片描述
運行即可發聲,而且輸入“1234567789”等任意音符對應的數字也可以發出“dao ruai mi fa。。。”進行***音符識別***
下面展示gui版本

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

% Last Modified by GUIDE v2.5 17-Mar-2020 21:31:13

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
global h;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @duolaimi2_OpeningFcn, ...
                   'gui_OutputFcn',  @duolaimi2_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
end


% --- Executes just before duolaimi2 is made visible.
function duolaimi2_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 duolaimi2 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = duolaimi2_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;
end

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fs = 4000;t = (0:1500)*(1/fs); 
f=262;
n=1;
global h;
y = cos(2*pi*f*t);                                     
sound(y,fs);
set(handles.edit1,'String',n);
h(n,:)=y;
%i=i+1;
end

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fs = 4000;t = (0:1500)*(1/fs);  
f=294;
n=2;
global h;
y = cos(2*pi*f*t);                                     
sound(y,fs);
set(handles.edit1,'String',n);
h(n,:)=y;
%i=i+1;
end

% --- 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)
fs = 4000;t = (0:1500)*(1/fs);  
f=330;
n=3;
global h;
y = cos(2*pi*f*t);                                     
sound(y,fs);
set(handles.edit1,'String',n);
h(n,:)=y;
%i=i+1;
end

% --- 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)
fs = 4000;t = (0:1500)*(1/fs);  
f=349;
n=4;
global h;
y = cos(2*pi*f*t);                                     
sound(y,fs);
set(handles.edit1,'String',n);
h(n,:)=y;
%i=i+1;
end

% --- 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)
fs = 4000;t = (0:1500)*(1/fs);  
f=392;
n=5;
global h;
y = cos(2*pi*f*t);                                     
sound(y,fs);
set(handles.edit1,'String',n);
h(n,:)=y;
%i=i+1;
end

% --- 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)
fs = 4000;t = (0:1500)*(1/fs);  
f=440;
y = cos(2*pi*f*t);                                     
sound(y,fs);
set(handles.edit1,'String',n);
%i=i+1;
end

% --- 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)
fs = 4000;t = (0:1500)*(1/fs);  
f=494;
n=7;
global h;
y = cos(2*pi*f*t);                                     
sound(y,fs);
set(handles.edit1,'String',n);
h(n,:)=y;
%i=i+1;
end


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
%x=str2double(get(handles.edit1,'String'));
%x=num2str(x,'%04d')-'0';
x=str2num(get(handles.edit1,'String')');
Fs=44100;
t=0:1/Fs:0.5;
p1=cos(2*pi*261*t);
p2=cos(2*pi*293.66*t);
p3=cos(2*pi*329.63*t);
p4=cos(2*pi*349.23*t);
p5=cos(2*pi*392.00*t);
p6=cos(2*pi*440.00*t);
p7=cos(2*pi*493.88*t);
p0=cos(2*pi*0*t);
for i=1:length(x)
    switch(x(i))
        case 1
            sound(p1,Fs);
            pause(0.5);
        case 2
            sound(p2,Fs);
            pause(0.5);
        case 3
            sound(p3,Fs);
            pause(0.5);
        case 4
            sound(p4,Fs);
            pause(0.5);
        case 5
            sound(p5,Fs);
            pause(0.5);
        case 6
            sound(p6,Fs);
            pause(0.5);
        case 7
            sound(p7,Fs);
            pause(0.5);
        case 0
            pause(0.5);
    end
end
end


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
end


% --- 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)
end


在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章