In this class, we learn how to use MATLAB to calculate the formulas and plot some figure. And then, we make the music and analyze the music for the midterm project.
The name of the movie is called "you are the apple of my eye." It means that you are the precious person as far as I am concerned. I watched this movie again recently. The plot of the movie touched me. As a result, I decided to use the theme song.
The Introduction of the Main Code
Read the Music and Plot the Sound Wave
% --- 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 MATLA
% handles structure with handles and user data (see GUIDATA)
[y, fs] = audioread('C:\Users\Johnny\Music\胡夏 那些年 歌詞.mp3'); % y會拿到音訊檔的所有點,fs為此音訊檔的sample rate
b = get(handles.slider2, 'value');
y = y * b;
sound(y,fs);
time = (1:length(y))/fs;
plot(handles.axes1,time,y,'r');
Do the Fourier Transform and Plot the result
% --- 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)
y = audioread('C:\Users\Johnny\Music\胡夏 那些年 歌詞.mp3'); % y會拿到音訊檔的所有點,fs為此音訊檔的sample rate
s = abs(fft(y));
figure(1);
subplot(2,1,1),plot(s);
subplot(2,1,2),plot(y);
sound(y, 44100);
Clean the Sound
% --- 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)
clear sound;
Remake the Theme Song by MATLAB
% --- 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)
%% Sampling Frequency
Fs = 44100; %Sampling Frequency
T = 1/Fs; %Sampling Period
%% Define Tempo 單位為16分音符
tempo = 95;
S = 1*(60/tempo);
time = [0:T:S];
[x k] = size(time);
t = @(L) linspace(0,L*S,L*k);
%% Define Notes
fA = 440.00; % Master Tuned to A 440
fGS = fA*2^(-1/12);
fG = fGS*2^(-1/12);
fFS = fG*2^(-1/12);
fF = fFS*2^(-1/12);
fE = fF*2^(-1/12);
fDS = fE*2^(-1/12);
fD = fDS*2^(-1/12);
fCS = fD*2^(-1/12);
fC = fCS*2^(-1/12);
fAS = fA*2^(1/12);
fB = fAS*2^(1/12);
A = @(m,L) sin(2*pi*fA*t(L)*2^m);
GS = @(m,L) sin(2*pi*fGS*t(L)*2^m);
G = @(m,L) sin(2*pi*fG*t(L)*2^m);
FS = @(m,L) sin(2*pi*fFS*t(L)*2^m);
F = @(m,L) sin(2*pi*fF*t(L)*2^m);
E = @(m,L) sin(2*pi*fE*t(L)*2^m);
DS = @(m,L) sin(2*pi*fDS*t(L)*2^m);
D = @(m,L) sin(2*pi*fD*t(L)*2^m);
CS = @(m,L) sin(2*pi*fCS*t(L)*2^m);
C = @(m,L) sin(2*pi*fC*t(L)*2^m);
B = @(m,L) sin(2*pi*fB*t(L)*2^m);
AS = @(m,L) sin(2*pi*fAS*t(L)*2^m);
%% Define Rest
si = 0*A(0,1/4); % sixteenth-quarter rest
er = 0*A(0,1/2); % eigth rest
qr = 0*A(0,1); % quarter rest
hr = 0*A(0,2); % half rest
wr = 0*A(0,4); % whole rest
%% Test 那些年
BH1 = [A(1,1) qr G(1,1) AS(1,1) A(1,1) F(1,1) DS(1,1/2) F(1,1/2) G(1,1/2) A(1,1/2) D(1,2) E(1,1) F(1,1) D(0,4)+F(0,4)+AS(0,4) C(0,4)+F(0,4)+A(0,4) AS(-1,3)+D(0,3)+G(0,3) si F(0,1/4) F(0,1/4) G(0,1/4) A(0,1/2) A(0,1/2) A(0,1/2) AS(0,1/4) A(0,3/2) A(0,1/4) G(0,1/4) F(0,1/4) G(0,1/2) G(0,1/2) G(0,1/2) AS(0,1/4) A(0,1/4) A(0,3/2)+E(0,3/2) G(0,1/2) F(0,1/2) E(0,1/4) F(0,1) E(0,1/4) F(0,1/2) E(0,1/2) D(0,1/2) C(0,1/4) C(0,13/4) si F(0,1/4) G(0,1/4) A(0,1/4) AS(0,1) D(0,1/2) F(0,1/2) E(0,7/4) E(0,1/4) CS(0,1/2)+A(0,1/2) AS(0,1/2) A(0,1/2) G(0,1/4) F(0,3/4) F(0,1/2) E(0,1/2) F(0,1/2) AS(0,1/2) A(0,1/2) G(0,1/2) F(0,1/4) AS(0,3/4) A(0,1/2) G(0,1/2) F(0,1/2) C(0,3)+F(0,3)+G(0,3) si F(0,1/4) F(0,1/4) G(0,1/4) A(0,1/2) A(0,1/2) A(0,1/2) AS(0,1/4) A(0,3/2) A(0,1/4) G(0,1/4) F(0,1/4)];
BH2 = [G(0,1/2) G(0,1/2) G(0,1/2) AS(0,1/4) A(0,1/4) E(0,3/2)+A(0,3/2) G(0,1/2) F(0,1/2) E(0,1/4) F(0,1) E(0,1/4) F(0,1/2) E(0,1/2) F(0,1/2) D(1,1/4) C(1,1/4) E(0,2)+G(0,2)+C(1,2) er AS(0,1/2) A(0,1/2) C(1,1/2) C(1,1/2) D(1,1/2) AS(0,1/2) A(0,1/4) G(0,1/4) E(0,1)+G(0,1) G(0,1/4) G(0,1/4) A(0,1/4) AS(0,1/4) CS(0,1/2)+AS(0,1/2) C(1,1/2) A(0,1/2) G(0,1/4) F(0,3/4) F(0,1/2) E(0,1/2) F(0,1/2) AS(0,1/2) A(0,1/2) AS(0,1/2) D(0,1/2) F(0,1) E(0,3/4) F(0,1/4) A(-1,2)+F(0,2) AS(-1,1)+DS(0,1)+G(0,1) C(0,1)+F(0,1)+A(0,1) AS(0,1/2) AS(0,1/2) D(0,1/2)+AS(0,1/2) AS(0,1/4) A(0,1/4) CS(0,1/2)+A(0,1/2) E(0,1/2) C(0,1/2)+A(0,1/2) G(0,1/2) C(0,1/2)+G(0,1/2) F(0,1/4) F(0,1) F(0,1/4) AS(0,1/2) A(0,1/2) G(0,1/2) A(0,1/2) AS(0,1/2) A(0,1/4) AS(0,3/4) D(1,1/2) D(1,1/2) E(1,1/2) C(1,1/2) AS(0,1/2) AS(0,1/2) C(1,1/4) A(0,5/4) C(0,1)+A(0,1) CS(0,1)+A(0,1) F(1,1/2)+A(1,1/2)+D(2,1/2) D(2,1/4) D(2,1/2) D(2,3/4) CS(2,1/2) D(2,1/4) E(2,3/4) A(1,1/2) E(2,1/2) F(2,1/4) E(2,1/2) C(2,3/4) F(1,3/4) si qr AS(0,1/2) A(0,1/2) G(0,1/2) F(0,1/4) AS(0,1/2) A(0,3/4) AS(0,1/2) C(1,1/2) C(0,1) F(0,1)+G(0,1)+C(1,1) E(0,1)+G(0,1)+C(1,1) er];
BH3 = [hr qr si C(1,1/4) C(1,1/4) E(1,1/4) F(1,1/2) F(1,1/4) F(1,1/2) C(1,1/2) C(1,5/4) si C(1,1/4) C(1,1/4) F(1,1/4) G(1,1/2) G(1,1/4) G(1,1/2) C(1,3/4) A(0,1)+CS(1,1) G(1,1/2) A(1,1/2) G(1,1/2) F(1,1/4) F(1,1) F(1,1/4) E(1,1/2) F(1,1/2) E(1,1/2) C(1,1/2) D(1,3/4) G(1,3/4) G(0,3/2) si G(0,1/4) G(0,1/4) A(0,1/4) AS(0,1/2) A(0,1/2) AS(0,1/2) D(1,1/4) C(1,5/4) si AS(0,1/4) A(0,1/4) AS(0,1/4) C(1,1/2) C(1,1/2) G(1,1/2) E(1,1/4) F(1,3/4) F(1,1/2) E(1,1/2) C(1,1/2) AS(0,1/2) A(0,1/4) AS(0,1/2) D(1,1/2) D(1,3/4) A(1,1/2) G(1,1/2) F(1,1/2) G(1,3/4) AS(-1,1/4)+D(0,1/4)+F(0,1/4) qr AS(-1,1)+D(0,1)+G(0,1) si C(1,1/4) C(1,1/4) E(1,1/4) F(1,1/2) F(1,1/4) F(1,1/2) C(1,3/4) C(1,3/4) C(1,1/4) si C(1,1/4) C(1,1/4) F(1,1/4) G(1,1/2) G(1,1/4) G(1,1/2) C(1,3/4) A(0,1)+CS(1,1) G(1,1/2) A(1,1/2) G(1,1/2) F(1,1/4) F(1,1) F(1,1/4) E(1,1/2) F(1,1/2) E(1,1/2) C(1,1/2)];
BH4 = [D(1,3/4) A(1,3/4) A(0,3/4) G(0,3/4) si AS(0,1/4) AS(0,1/4) C(1,1/4) D(1,1/2) C(1,1/4) D(1,1/2) F(1,1/2) CS(1,5/4)+E(1,5/4) si E(1,1/4) E(1,1/4) B(0,1/4)+D(1,1/4) CS(1,1/2) C(1,1/2) A(1,1/2) G(1,1/4) F(1,1/2) C(1,1/2)+F(1,1/2) F(1,1/2) E(1,1/2) C(1,1/2) AS(0,1/2) A(0,1/4) AS(0,1/2) C(1,1/2) D(1,3/4) A(1,1/2) G(1,1/2) F(1,1/2) G(1,3/4) AS(-1,1/4)+D(0,1/4)+G(0,1/4) er si F(1,1/4) F(1,3/4) E(1,3/4) F(1,1/2) F(1,3)];
M = [BH1 BH2 BH3 BH4];
%BL1 = [D(1,1/2) A(1,1/2) D(2,1) C(1,1/2) A(1,1/2) AS(1,1) AS(0,1/2) F(1,1/2) AS(1,1) A(0,1)+A(1,1) D(1,1)+D(2,1) G(0,1/2) D(1,1/2) AS(1,1) F(0,1/2) C(1,1/2) E(1,1) E(0,1/2) AS(0,1/2) D(1,1/2) F(1,1) G(1,1/2) A(1,1/4) AS(1,1/4) D(2,1/2) A(-1,1/2) A(0,1/2) D(1,1/2) G(1,1) AS(1,1/2) D(2,1) A(-1,4)+A(0,4) D(1,1/2) A(1,1/2) D(2,3) C(1,2)+A(1,2) AS(0,2)+F(1,2) AS(0,2)+G(1,2)+A(1,2) hr F(0,1/2)+E(1,1/2) C(1,1/2) E(1,1/2) A(1,5/2) G(0,1/2) D(1,1/2) G(1,1) A(0,2)+A(1,2) F(0,2)+C(1,2)+E(1,2) AS(0,2)+F(1,2)+A(1,2) G(0,1/2) D(1,1/2) G(1,1/2) A(1,5/2) A(0,1/2) E(1,1/2) G(1,1/2) D(2,1/2) E(1,2)+A(1,2)+C(2,2) D(0,4)+D(2,4)];
%Base = [BL1];
%M = [High + Base];
%% Listen to Notes
soundsc(M, Fs);
Close the GUI
% --- 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)
close(gcbf);
Clean the Axes in GUI
% --- 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)
cla reset;
Get the Value of the Slider and Give the Number to the String of the Text
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
a = get(handles.slider2, 'value');
set(handles.text4, 'string', a);
Result
MATLAB & GUI
Sound Wave & Fourier Transform
Control Button & Volume
Demo Video
ps. Recommend 720p or higher quality to watch the video