1. 程式人生 > 其它 >用MATLAB和Python製作拜年祝福

用MATLAB和Python製作拜年祝福

技術標籤:隨便玩玩matlabpython

用MATLAB生成音樂。程式碼如下。

% guitar.m
function [u,fs]=guitar(keynum,t)
L=0.5;
f=440*2^((keynum-49)/12);
c=2*L*f;
fs=11025;
u=0;
for k = 0:50
    u=u+(-1)^k.*(8/((2*k+1)^2*pi^2)).*sin(((2*k+1)*pi/L)*L/2).*cos(((2*k+1)*pi*c/L)*t);
end
% make_vector.m
function treble_vector=make_vector(treble,rhythm)
fs=11025;
speed_factor=71/60;
%生成向量
treble_vector=zeros(1,ceil(sum(rhythm)*fs)+1);
n1=1;
for kk=1:length(treble)
    keynum=treble(kk);
    if keynum==0
        A=0.0;freq=440;
    else
        A=0.5;freq=440*2^((keynum-49)/12);
    end
    tt=0:1/fs:(rhythm(kk)/speed_factor);
    tone=A*guitar(keynum,tt);
    win=hann(length(tone));
    tone=tone.*win';
    n2=n1+length(tone)-1;
    treble_vector(n1:n2)=treble_vector(n1:n2)+tone;
    n1=n2;
end
% music.m
function music(tone,rhythm,weaken,A,Fs)

y=[];freqs=[523,587,659,698,783,880,988,0];
len=length(tone);
for i = 1:len
    x=linspace(0,2*pi*rhythm(i),floor(Fs*rhythm(i)));
    y=[y,A(i)*sin(x*freqs(tone(i))).*(1-x/(2*rhythm(i)*pi)).^weaken(i)];    
end
sound(y,Fs);
pause(len-2.5)
end
% good_luck_come.m
v=[49,51,0,52,54,56,57,59,61,63,64,66,68];
dur=0.45;
Fs=11025;
%prelude1
p1_tone=[6 6 8 8 6 0 ...
    6 5 3 5 8 6 ...
    0 ...
    6 8 8 8 8 6 5 ...
    6 5 2 5 3 3 ...
    3 2 1 3 2 3 ...
    6 5 3 6 5 ...
    0 ...
    6 8 8 6 9 9 9 8 ...
    6 5 8 6];
p1_rhy=[1 0.5 1 1 2 1 ...
    1 1 1 1 1 2 ...
    1 ...
    1 1 1 0.5 1 1 1 ...
    1 1 1 1 2 2 ...
    1 1 1 1 2 1 ...
    1 1 1 1 2 ...
    1 ...
    1 1 1 0.5 1 1 1 1 ...
    1 1 1 3];
pre1_tone=p1_tone;
pre1_rhy=dur*p1_rhy;
clear p1_tone; 
clear p1_rhy; 
clear p2_tone; 
clear p2_rhy;
%hightide1
p3_tone=[6 10 9 8 6 ... % 好運來祝你好運來
    5 8 6 ...
    6 9 8 6 5 ...
    2 5 3 0];
p3_rhy=[2 1 1.5 1 1 ...
    1 1 2 ...
    1 1 1 1 1 ...
    1 0.5 2 1];
p4_tone=[3 6 5 6 6 5 ...  % 好運來我們好運來
    6 9 8 9 ...
    8 8 8 9 10 10 9 8 ...
    5 0 8 6];
p4_rhy=[1 1 1 1 1 1 ...
        1 0.5 0.5 2 ...
    1 0.5 1 1 1 1 1 1 ...
    1 1 1 3];

hightide1_tone=[p3_tone,p4_tone];
hightide1_rhy=dur*[p3_rhy,p4_rhy];
clear p3_tone; 
clear p3_rhy ;
clear p4_tone; 
clear p4_rhy; 

%combinition
allparts_tone=[pre1_tone,hightide1_tone];
allparts_rhy=[pre1_rhy hightide1_rhy];
clear pre1_tone; 
clear hightide1_tone ;
clear pre1_rhy;
clear hightide1_rhy; 
Tones=v(allparts_tone+3);
speed_factor=71/60;
%生成向量
vector=zeros(1,ceil(sum(allparts_rhy)*Fs)+1);
n1=1;
for kk=1:length(Tones)
    keynum=Tones(kk);
    if keynum==0
        A=0.0;freq=440;
    else
        A=0.5;freq=440*2^((keynum-49)/12);
    end
    tt=0:1/Fs:(allparts_rhy(kk)/speed_factor);
    L=0.5;
    f=440*2^((keynum-49)/12);
    c=2*L*f;
    fs=11025;
    u=0;
    for k = 0:50
        u=u+(-1)^k.*(8/((2*k+1)^2*pi^2)).* ...
            sin(((2*k+1)*pi/L)*L/2).*cos(((2*k+1)*pi*c/L)*tt);
    end
    tone=A*u;
    win=hann(length(tone));
    tone=tone.*win';
    n2=n1+length(tone)-1;
    vector(n1:n2)=vector(n1:n2)+tone;
    n1=n2;
end
sound(vector,Fs);

用Python製作牛年祝福詞雲

import pandas as pd
from pyecharts.charts import WordCloud
from pyecharts import options as opts
import re
import numpy as np

data = pd.read_table('C:\\Users\\13372\\Desktop\\congratulations.txt',encoding='gbk')
data = data.columns.to_list()[0]
cong = re.split(r'[,。!;]', data)
size =
np.random.randint(1,10,len(cong)).tolist() wd_input = list(zip(cong,size)) c = ( WordCloud() .add("", wd_input, word_size_range=[12, 16], mask_image='C:\\Users\\13372\\Desktop\\Temp Files\\niu.jpg') .set_global_opts(title_opts=opts.TitleOpts(title='CONGRATULATIONS')) .render("C:\\Users\\13372\\Desktop\\congratulations.html") )

congratulations.txt是在網上翻的新年祝福,直接複製貼上。。。