当前位置:网站首页>How to write simple music program with MATLAB
How to write simple music program with MATLAB
2022-06-12 08:07:00 【HNU_ Liu Yuan】
Some people say :MATLAB Besides having children , The others will .
Can it be used to create music ?
The answer is Tolerable
First we need to know what we hear DO RE MI, How did it come about ? What is the difference between ?
Music as “ wave ” A kind of , Naturally, the superposition of sine waves can be used to simulate , Different frequencies can produce notes of different frequencies , So we just use MATLAB Generate a series of sine functions , Plus MATLAB Self contained **sound()** Function .
Music is mainly composed of tones and beats , So the music function needs two inputs , I.e. tone (tone) And beat (rythm)
Now the function module is given :
Fs Is the sampling frequency , General default 8192Hz, That is, the number of samples per second ;
linspace(a,b,c) The function is used to uniformly generate b individual a To c Array between ;
freqs The array of is the frequency corresponding to different timbres , For the sake of simplicity, only commonly used ;
Be careful : The playback frequency should be consistent with the sampling frequency !!!
function y = gen_wave( tone, rythm )
%UNTITLED2 Music function Corresponding music2
% tone pat
Fs = 8192;
freqs = [523, 587, 659, 698, 783, 880, 988];
x = linspace(0, 2 * pi * rythm, floor(Fs * rythm));
y = sin(freqs(tone) * x);
end
Create a new main function and execute the following code to issue DO The sound .
Fs = 8192;
y=[];
y = gen_wave(1,1);
sound(y,Fs);
After execution, use **plot()** Function to draw a sound image , Here's the picture 
You can see very dense , After the local magnification is :
First, you can see that after zooming in , Values are discontinuous , Then the sound is constant , This is not quite consistent with our reality , The piano should be at its maximum when it is just pressed , It will change with time , The sound fades away , So we are y Is multiplied by a function , Attenuate it , The code is as follows :
function y = gen_wave( tone, rythm )
% tone pat
Fs = 8192;
freqs = [523, 587, 659, 698, 783, 880, 988];
x = linspace(0, 2 * pi * rythm, floor(Fs * rythm));
y = sin(freqs(tone) * x) .*(1- x/(rythm * 2 *pi));
end
Rerun DO The sound , And draw an image :
Zoom in 
It can be seen that this is oscillation attenuation , Of course, it can also be multiplied by a function of exponential decay , The sound will be more real .
Then we can find a complete music score , such as 《 Little star 》
According to the simplified music, we can generate an array of music representatives of this song , The code is as follows :
Fs = 8192;
y=[];
music = [1,1,5,5,6,6,5,...
4,4,3,3,2,2,1,...
5,5,4,4,3,3,2,...
5,5,4,4,3,3,2,...
1,1,5,5,6,6,5,...
4,4,3,3,2,2,1];
music_length = length(music(:));
y1 = gen_wave(1,1);
y2 = gen_wave(2,1);
y3 = gen_wave(3,1);
y4 = gen_wave(4,1);
y5 = gen_wave(5,1);
y6 = gen_wave(6,1);
y7 = gen_wave(7,1);
for i = 1:1:music_length
if music(i) == 1
y=[y,y1];
elseif music(i) == 2
y=[y,y2];
elseif music(i) == 3
y=[y,y3];
elseif music(i) == 4
y=[y,y4];
elseif music(i) == 5
y=[y,y5];
elseif music(i) == 6
y=[y,y6];
elseif music(i) == 7
y=[y,y7];
end
end
sound(y, Fs);
By traversing the music array , Executing subroutines , We can generate an array of scales , Reuse sound() Just play it .
If it feels useful , Give me some advice. ,O(∩_∩)O ha-ha ~
边栏推荐
- MATLAB image processing - Otsu threshold segmentation (with code)
- Topic 1 Single_ Cell_ analysis(3)
- Vision Transformer | CVPR 2022 - Vision Transformer with Deformable Attention
- Vision Transformer | Arxiv 2205 - LiTv2: Fast Vision Transformers with HiLo Attention
- Strvec class mobile copy
- Quaternion Hanmilton and JPL conventions
- (P25-P26)基于非范围的for循环、基于范围的for循环需要注意的3个细节
- Conda創建虛擬環境報錯,問題解决
- Solve mapper duplication problem in reverse engineering
- 802.11 protocol: wireless LAN protocol
猜你喜欢

visual studio2019的asp.net项目添加日志功能

Vins technical route and code explanation

Topic 1 Single_ Cell_ analysis(4)

Some summaries of mathematical modeling competition in 2022

Vision Transformer | CVPR 2022 - Vision Transformer with Deformable Attention

MATLAB image processing - Otsu threshold segmentation (with code)

Process terminated

电脑连接上WiFi但是上不了网

Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano

FPGA to flip video up and down (SRAM is61wv102416bll)
随机推荐
Ceres optimizer usage (self use)
qt. qpa. plugin: Could not load the Qt platform plugin “xcb“ in “***“
Topic 1 Single_ Cell_ analysis(1)
FPGA to flip video up and down (SRAM is61wv102416bll)
Vins technical route and code explanation
The R language uses the sample The split function divides the machine learning data set into training set and test set
S-msckf/msckf-vio technical route and code details online blog summary
Final review of Discrete Mathematics (predicate logic, set, relation, function, graph, Euler graph and Hamiltonian graph)
Conda crée un environnement virtuel pour signaler les erreurs et résoudre les problèmes
Vision transformer | arXiv 2205 - TRT vit vision transformer for tensorrt
Vision Transformer | Arxiv 2205 - LiTv2: Fast Vision Transformers with HiLo Attention
Numpy generates row vectors and column vectors (1024 water medal text)
Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano
2、 Eight, ten and hexadecimal conversion
vm虚拟机中使用NAT模式特别说明
Leetcode notes: biweekly contest 70
Compiling principle on computer -- function drawing language (III): parser
2.2 linked list - Design linked list (leetcode 707)
AJ project: online bank project summary
JSP technology