用MATLAB和Python制作拜年祝福

用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是在网上翻的新年祝福,直接复制粘贴。。。

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值