原理很简单,确定好歌谱每个音节频率和持续时间,理论上有歌谱可以编写所有的歌曲,这里编写最简单的一首《世上只有妈妈好》,供大家参考。我对编写出的歌曲进行了DWT压缩处理,,显示压缩后的音频和原来音频的比较。
clear;
clc;
fs=8000;
%抽样频率?
f=[425.37,392,329.63,392,510,425.37,392,425.37,329.63,...
392,425.37,392,329.63,293.66,262.63,221.13,392,...
329.63,293.66,293.66,329.63,392,392,425.37,329.63,...
293.66,262.63,392,329.63,293.66,262.63,221.13,262.63,223.11];%各个乐音对应的频率?
time=fs*[0.6,0.4,1/2,1/2,1/2,1/4,1/4,1,1/2,...
1/4,1/4,1/2,1/4,1/4,1/4,1/4,1/4,1/4,1,1/2,1/2,...
1/2,1/4,1/4,1/2,1/2,1,0.6,0.4,1/4,1/4,1/4,1/4,1];%各个乐音的抽样点数?
N=length(time);%这段音乐的总抽样点数?
p=zeros(1,N);%用p向量来储存抽样点?
n=1;
for num=1:N
%利用循环产生抽样数据,num表示乐音编号?
t=1/fs:1/fs:time(num)/fs;%产生第num个乐音的抽样点?
G=zeros(1,time(num));%G为存储包络数据的向量
%G(1:time(num))=exp(1:(-1/time(num)):1/fs);%产生包络
p(n:n+time(num)-1)=sin(2*pi*f(num)*t);%.*G(1:time(num));%%%f(num)为第num个抽样频率
%抽样点对应的幅值?????
n=n+time(num);
end
%播放音乐?
plot(p);
[c,s]=wavedec(p,3,'db1');%%%三次小波变换
a1=appcoef(c,s,'db1',1);%%%提取第一层低频分量
k1=wcodemat(a1,500,'r',0);%%对低频分量进行编码
a2=appcoef(c,s,'db1',2);
k2=wcodemat(a2,500,'r',0);
a3=appcoef(c,s,'db1',3);
k3=wcodemat(a3,500,'r',0);
%%%%显示结果%%%%%%%%%%%%%%
subplot(221)
plot(p)
title('原始音频信号')
subplot(222)
plot(k1)
title('第一次压缩后的音频信号')
subplot(223)
plot(k2)
title('第二次压缩后的音频信号')
subplot(224)
plot(k3)
title('第三次压缩后的音频信号')
%%%%播放各分解后的低频语音信号,播放时间越短,说明信号压缩越多%%%%%%%%
wavplay(p);
wavplay(a1)
% wavplay(a2)
% wavplay(a3)
%%%%%%%显示压缩后各比特大小%%
whos('p')
whos('k1')
whos('k2')
whos('k3')