MATLAB变声程序代码
男声变女声matlab程序

exc = zeros(L,1);%激励信号(预测误差)
zi_pre = zeros(P,1);%预测滤波器的状态
s_rec = zeros(L,1);%重建语音
zi_rec = zeros(P,1);
%合成滤波器
exc_syn = zeros(L,1);%合成的激励信号(脉冲串)
PT = findpitch(s_Pitch);%计算基音周期PT
G = sqrt(E*PT);%计算合成激励的能量G
tempn_syn = [1:n*FL-last_syn]';
exc_syn1 = zeros(length(tempn_syn),1);
exc_syn1(mod(tempn_syn,PT)==0) = G;%某一段算出的脉冲
[s_syn1_t,zi_syn_t] = filter(1,A1,exc_syn1_t,zi_syn_t);
exc_syn_t((n-1)*FL+1:n*FL) = exc_syn1_t;%计算得到的合成激励
s_syn_t((n-1)*FL+1:n*FL) = s_syn1_t;%计算得到的合成语音
%载入声音
[s,fs,nbits] = wavread('222');
%播放原始声音
sound(s,fs,nbits);
FL = 80;%帧长
WL = 240;%窗长
P = 10;%预测系数个数
s = s/max(s);%归一化
L = length(s);%读入语音长度
FN = floor(L/FL)-2;%计算帧数
exc_syn1 = exc_syn1((n-1)*FL-last_syn+1:n*FL-last_syn);
男声变女声matlab程序

% 载入声音[s,fs,nbits] = wavread('222');%播放原始声音sound(s,fs,nbits);FL = 80; % 帧长WL = 240; % 窗长P = 10; % 预测系数个数s = s/max(s); % 归一化L = length(s); % 读入语音长度FN = floor(L/FL)-2; % 计算帧数% 预测和重建滤波器exc = zeros(L,1); % 激励信号(预测误差)zi_pre = zeros(P,1); % 预测滤波器的状态s_rec = zeros(L,1); % 重建语音zi_rec = zeros(P,1);% 合成滤波器exc_syn = zeros(L,1); % 合成的激励信号(脉冲串)s_syn = zeros(L,1); % 合成语音last_syn = 0; % 存储上一个(或多个)段的最后一个脉冲的下标zi_syn = zeros(P,1); % 合成滤波器的状态% 变调不变速滤波器exc_syn_t = zeros(L,1); % 合成的激励信号(脉冲串)s_syn_t = zeros(L,1); % 合成语音last_syn_t = 0; % 存储上一个(或多个)段的最后一个脉冲的下标zi_syn_t = zeros(P,1); % 合成滤波器的状态% 变速不变调滤波器(假设速度减慢一倍)hw = hamming(WL); % 汉明窗% 依次处理每帧语音for n = 3:FN% 计算预测系数s_w = s(n*FL-WL+1:n*FL).*hw; % 汉明窗加权后的语音[A E] = lpc(s_w, P); % 用线性预测法计算P个预测系数% A是预测系数,E会被用来计算合成激励的能量s_f = s((n-1)*FL+1:n*FL); % 本帧语音,下面就要对它做处理%用filter函数s_f计算激励[exc1,zi_pre] = filter(A,1,s_f,zi_pre);exc((n-1)*FL+1:n*FL) = exc1; %计算得到的激励%用filter函数和exc重建语音[s_rec1,zi_rec] = filter(1,A,exc1,zi_rec);s_rec((n-1)*FL+1:n*FL) = s_rec1; %计算得到的重建语音s_Pitch = exc(n*FL-222:n*FL);PT = findpitch(s_Pitch); % 计算基音周期PTG = sqrt(E*PT); % 计算合成激励的能量Gtempn_syn = [1:n*FL-last_syn]';exc_syn1 = zeros(length(tempn_syn),1);exc_syn1(mod(tempn_syn,PT)==0) = G; %某一段算出的脉冲exc_syn1 = exc_syn1((n-1)*FL-last_syn+1:n*FL-last_syn);[s_syn1,zi_syn] = filter(1,A,exc_syn1,zi_syn);exc_syn((n-1)*FL+1:n*FL) = exc_syn1; %计算得到的合成激励s_syn((n-1)*FL+1:n*FL) = s_syn1; %计算得到的合成语音last_syn = last_syn+PT*floor((n*FL-last_syn)/PT);%男声变女声PT1 =floor(PT/2); %减小基音周期poles = roots(A);deltaOMG =150*2*pi/fs;for p=1:10 %增加共振峰频率,实轴上方的极点逆时针转,下方顺时针转if imag(poles(p))>0 poles(p) = poles(p)*exp(j*deltaOMG);elseif imag(poles(p))<0 poles(p) = poles(p)*exp(-j*deltaOMG);endendA1=poly(poles);tempn_syn_t = [1:n*FL-last_syn_t]';exc_syn1_t = zeros(length(tempn_syn_t),1);exc_syn1_t(mod(tempn_syn_t,PT1)==0) = G; %某一段算出的脉冲exc_syn1_t = exc_syn1_t((n-1)*FL-last_syn_t+1:n*FL-last_syn_t);[s_syn1_t,zi_syn_t] = filter(1,A1,exc_syn1_t,zi_syn_t);exc_syn_t((n-1)*FL+1:n*FL) = exc_syn1_t; %计算得到的合成激励s_syn_t((n-1)*FL+1:n*FL) = s_syn1_t; %计算得到的合成语音last_syn_t = last_syn_t+PT1*floor((n*FL-last_syn_t)/PT1);end%绘图plot(s_syn_t),xlabel('n (samples)'), ...ylabel('Amplitude'), title('变换后语音信号'),...xlim([0,length(s_syn_t)]);%播放改变后的声音sound(2*s_syn_t);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% findpitch函数% 计算一段语音的基音周期function PT = findpitch(s)[B, A] = butter(5, 700/4000);s = filter(B,A,s);R = zeros(143,1);for k=1:143R(k) = s(144:223)'*s(144-k:223-k);end[R1,T1] = max(R(80:143));T1 = T1 + 79;R1 = R1/(norm(s(144-T1:223-T1))+1);[R2,T2] = max(R(40:79));T2 = T2 + 39;R2 = R2/(norm(s(144-T2:223-T2))+1);[R3,T3] = max(R(20:39));T3 = T3 + 19;R3 = R3/(norm(s(144-T3:223-T3))+1);Top = T1;Rop = R1;if R2 >= 0.85*RopRop = R2;Top = T2; endif R3 > 0.85*RopRop = R3;Top = T3; endPT = Top;return。
有趣的MATLAB 5.声音程序

MATLAB声音程序1.最炫民族风音频fs = 44100; % sample ratedt = 1/fs;T16 = 0.125;t16 = [0:dt:T16];[temp k] = size(t16);t4 = linspace(0,4*T16,4*k);t8 = linspace(0,2*T16,2*k);[temp i] = size(t4);[temp j] = size(t8);% Modification functionsmod4=(t4.^4).*exp(-30*(t4.^0.5));mod4=mod4*(1/max(mod4));mod8=(t8.^4).*exp(-50*(t8.^0.5));mod8=mod8*(1/max(mod8));mod16=(t16.^4).*exp(-90*(t16.^0.5));mod16=mod16*(1/max(mod16));f0 = 2*146.8; % reference frequencyScaleTable = [2/3 3/4 5/6 15/16 ...1 9/8 5/4 4/3 3/2 5/3 9/5 15/8 ...2 9/4 5/2 8/3 3 10/3 15/4 4 ...1/2 9/16 5/8];% 1/4 notesdo0f = mod4.*cos(2*pi*ScaleTable(21)*f0*t4);re0f = mod4.*cos(2*pi*ScaleTable(22)*f0*t4);mi0f = mod4.*cos(2*pi*ScaleTable(23)*f0*t4);fa0f = mod4.*cos(2*pi*ScaleTable(1)*f0*t4);so0f = mod4.*cos(2*pi*ScaleTable(2)*f0*t4);la0f = mod4.*cos(2*pi*ScaleTable(3)*f0*t4);ti0f = mod4.*cos(2*pi*ScaleTable(4)*f0*t4);do1f = mod4.*cos(2*pi*ScaleTable(5)*f0*t4);re1f = mod4.*cos(2*pi*ScaleTable(6)*f0*t4);mi1f = mod4.*cos(2*pi*ScaleTable(7)*f0*t4);fa1f = mod4.*cos(2*pi*ScaleTable(8)*f0*t4);so1f = mod4.*cos(2*pi*ScaleTable(9)*f0*t4);la1f = mod4.*cos(2*pi*ScaleTable(10)*f0*t4);tb1f = mod4.*cos(2*pi*ScaleTable(11)*f0*t4);ti1f = mod4.*cos(2*pi*ScaleTable(12)*f0*t4);re2f = mod4.*cos(2*pi*ScaleTable(14)*f0*t4); mi2f = mod4.*cos(2*pi*ScaleTable(15)*f0*t4); fa2f = mod4.*cos(2*pi*ScaleTable(16)*f0*t4); so2f = mod4.*cos(2*pi*ScaleTable(17)*f0*t4);la2f = mod4.*cos(2*pi*ScaleTable(18)*f0*t4);ti2f = mod4.*cos(2*pi*ScaleTable(19)*f0*t4);do3f = mod4.*cos(2*pi*ScaleTable(20)*f0*t4); blkf = zeros(1,i);% 1/8 notesdo0e = mod8.*cos(2*pi*ScaleTable(21)*f0*t8); re0e = mod8.*cos(2*pi*ScaleTable(22)*f0*t8); mi0e = mod8.*cos(2*pi*ScaleTable(23)*f0*t8); fa0e = mod8.*cos(2*pi*ScaleTable(1)*f0*t8);so0e = mod8.*cos(2*pi*ScaleTable(2)*f0*t8);la0e = mod8.*cos(2*pi*ScaleTable(3)*f0*t8);ti0e = mod8.*cos(2*pi*ScaleTable(4)*f0*t8);do1e = mod8.*cos(2*pi*ScaleTable(5)*f0*t8);re1e = mod8.*cos(2*pi*ScaleTable(6)*f0*t8);mi1e = mod8.*cos(2*pi*ScaleTable(7)*f0*t8);fa1e = mod8.*cos(2*pi*ScaleTable(8)*f0*t8);so1e = mod8.*cos(2*pi*ScaleTable(9)*f0*t8);la1e = mod8.*cos(2*pi*ScaleTable(10)*f0*t8);tb1e = mod8.*cos(2*pi*ScaleTable(11)*f0*t8); ti1e = mod8.*cos(2*pi*ScaleTable(12)*f0*t8); do2e = mod8.*cos(2*pi*ScaleTable(13)*f0*t8); re2e = mod8.*cos(2*pi*ScaleTable(14)*f0*t8); mi2e = mod8.*cos(2*pi*ScaleTable(15)*f0*t8); fa2e = mod8.*cos(2*pi*ScaleTable(16)*f0*t8); so2e = mod8.*cos(2*pi*ScaleTable(17)*f0*t8); la2e = mod8.*cos(2*pi*ScaleTable(18)*f0*t8);ti2e = mod8.*cos(2*pi*ScaleTable(19)*f0*t8); do3e = mod8.*cos(2*pi*ScaleTable(20)*f0*t8); blke = zeros(1,j);% 1/16 notesdo0s = mod16.*cos(2*pi*ScaleTable(21)*f0*t16); re0s = mod16.*cos(2*pi*ScaleTable(22)*f0*t16); mi0s = mod16.*cos(2*pi*ScaleTable(23)*f0*t16); fa0s = mod16.*cos(2*pi*ScaleTable(1)*f0*t16); so0s = mod16.*cos(2*pi*ScaleTable(2)*f0*t16); la0s = mod16.*cos(2*pi*ScaleTable(3)*f0*t16); ti0s = mod16.*cos(2*pi*ScaleTable(4)*f0*t16); do1s = mod16.*cos(2*pi*ScaleTable(5)*f0*t16); re1s = mod16.*cos(2*pi*ScaleTable(6)*f0*t16);fa1s = mod16.*cos(2*pi*ScaleTable(8)*f0*t16);so1s = mod16.*cos(2*pi*ScaleTable(9)*f0*t16);la1s = mod16.*cos(2*pi*ScaleTable(10)*f0*t16);tb1s = mod16.*cos(2*pi*ScaleTable(11)*f0*t16);ti1s = mod16.*cos(2*pi*ScaleTable(12)*f0*t16);do2s = mod16.*cos(2*pi*ScaleTable(13)*f0*t16);re2s = mod16.*cos(2*pi*ScaleTable(14)*f0*t16);mi2s = mod16.*cos(2*pi*ScaleTable(15)*f0*t16);fa2s = mod16.*cos(2*pi*ScaleTable(16)*f0*t16);so2s = mod16.*cos(2*pi*ScaleTable(17)*f0*t16);la2s = mod16.*cos(2*pi*ScaleTable(18)*f0*t16);ti2s = mod16.*cos(2*pi*ScaleTable(19)*f0*t16);do3s = mod16.*cos(2*pi*ScaleTable(20)*f0*t16); blks = zeros(1,k);% Melody by Schau_malpart0 = [mi1f la0e la0e do1f mi1f ...re1e re1s mi1s re1e do1e re1e do1e la0f ...mi1f la0e la0e do1f mi1f ...so1e re1s mi1s re1e do1e re1e do1e ti0e so0e ...mi1f la0e la0e do1f mi1f ...re1e re1s mi1s re1e do1e re1e do1e la0e so0e ...mi1f la0e la0e do1f mi1f ...so1e mi1e blkf blkf blkf ...];part1 = [la0f la0e so0e la0f la0e do1e ...do1f re1e do1e la0f la0f ...do1f do1e so0e do1e re1e mi1e so1e ...so1e mi1e re1f mi1f mi1f ...la1e la1e la1e so1e mi1e mi1f do1e ...la0e la0e la0e mi1e re1s mi1s re1e re1f ...mi1e mi1e so1e mi1e re1e mi1e re1e do1e ...la0f so0f la0f la0f ...];part2 = [mi1e mi1e so1e mi1e mi1e so1e so1e la1e ... do2e la1e so1f la1s do2s la1e la1f ...la0f la0e so0e la0f do1f ...re1e mi1s re1s do1e re1e mi1f mi1f ...la0e la1e la1e so1e re1e mi1s re1s do1e re1e ...mi1f mi1f blke blke blkf ...do1e la0e la0e do1e re1f so0e so0e ...mi1e so1e mi1e re1e do1f do1f ...la0e do1e re1e mi1e re1e do1e so0e mi0e ...la0f la0f blke blke blkf ...];part3 = [la0f la0e so0e la0f do1f ...re1e mi1s re1s do1e re1e mi1f mi1f ...la0e la1e la1e so1e re1e mi1s re1s do1e re1e ...mi1f mi1f blke blke blkf ...do1e la0e la0e do1e re1f so0e so0e ...mi1e so1e mi1e re1e do1f do1e do1e ...la0e do1e re1e mi1e so1e mi1e mi1e so1e ...la1f la1f la1f la1f ...];part4 = [la1e la1s la1s la1e la1e la1e la1s so1s mi1e re1e ...re1e re1s re1s mi1e mi1s so1s mi1e mi1s re1s do1e do1s la0s ...la0f la0e so0e la0f la0e do1e ...re1e mi1s re1s do1e re1e mi1f mi1f ...la1e so1e mi1e re1e so1e mi1e re1e do1e ...do1f do1f la0s do1s re1s mi1s re1s do1s la0s do1s];part5 = [do2e do2s do2s la1e la1s la1s so1e so1s so1s mi1e mi1s mi1s ...re1e mi1s re1s do1e la0s so0s la0s so0s do1s re1s mi1s so1s la1s re2s ...do2f do2f blks blks blks blks do1e re1e ...mi1f mi1f mi1f so1e mi1e ...la1f la1f la1e do1e so1e mi1e ...re1f re1e re1s re1s re1e re1e do1e re1e ...mi1f mi1e mi1s mi1s mi1e re1s do1s ti0e do1s re1s ...mi1f mi1f mi1f so1e mi1e ...do2f la1f la1f la1e do1e ...re1f so1f so1f la1f ...ti1f ti1f ti1f ti1f ...];part6 = [blkf blkf mi1e so1e mi1e so1e ...mi1f la0e la0s la0s do1f la0e mi1s la0s ...do1e do1s do1s re1e do1s re1s mi1f mi1f ...mi1f la0e la0s la0s so1f re1e re1s re1s ...mi1f mi1f mi1s re1s do1s la0s mi0s re0s mi0s so0s ...do1f la0e la0s la0s re1f so0e so0s so0s ...mi0f so0e so0s so0s do1f do1f ...la0f do1e do1s la0s mi1e mi1s mi1s re1e re1s mi1s ...];% Combination, v1 is complete version, v2 is simple version.v1 = [part0 part1 part1 part2 part3 part4 part0 part1 part1 part2 part3 part5 part3 part6 part3]; v2 = [part0 part1 part1 part2 part3 part5 part3 part6 part3];% Let's rock ^_^s = v1;s = s/max(s);sound(s,fs);ii以上内容来自知乎<电气小混混>。
男声变女声matlab程序

% 载入声音[s,fs,nbits] = wavread('222');%播放原始声音sound(s,fs,nbits);FL = 80; % 帧长WL = 240; % 窗长P = 10; % 预测系数个数s = s/max(s); % 归一化L = length(s); % 读入语音长度FN = floor(L/FL)-2; % 计算帧数% 预测和重建滤波器exc = zeros(L,1); % 激励信号(预测误差)zi_pre = zeros(P,1); % 预测滤波器的状态s_rec = zeros(L,1); % 重建语音zi_rec = zeros(P,1);% 合成滤波器exc_syn = zeros(L,1); % 合成的激励信号(脉冲串)s_syn = zeros(L,1); % 合成语音last_syn = 0; % 存储上一个(或多个)段的最后一个脉冲的下标zi_syn = zeros(P,1); % 合成滤波器的状态% 变调不变速滤波器exc_syn_t = zeros(L,1); % 合成的激励信号(脉冲串)s_syn_t = zeros(L,1); % 合成语音last_syn_t = 0; % 存储上一个(或多个)段的最后一个脉冲的下标zi_syn_t = zeros(P,1); % 合成滤波器的状态% 变速不变调滤波器(假设速度减慢一倍)hw = hamming(WL); % 汉明窗% 依次处理每帧语音for n = 3:FN% 计算预测系数s_w = s(n*FL-WL+1:n*FL).*hw; % 汉明窗加权后的语音[A E] = lpc(s_w, P); % 用线性预测法计算P个预测系数% A是预测系数,E会被用来计算合成激励的能量s_f = s((n-1)*FL+1:n*FL); % 本帧语音,下面就要对它做处理%用filter函数s_f计算激励[exc1,zi_pre] = filter(A,1,s_f,zi_pre);exc((n-1)*FL+1:n*FL) = exc1; %计算得到的激励%用filter函数和exc重建语音[s_rec1,zi_rec] = filter(1,A,exc1,zi_rec);s_rec((n-1)*FL+1:n*FL) = s_rec1; %计算得到的重建语音s_Pitch = exc(n*FL-222:n*FL);PT = findpitch(s_Pitch); % 计算基音周期PTG = sqrt(E*PT); % 计算合成激励的能量Gtempn_syn = [1:n*FL-last_syn]';exc_syn1 = zeros(length(tempn_syn),1);exc_syn1(mod(tempn_syn,PT)==0) = G; %某一段算出的脉冲exc_syn1 = exc_syn1((n-1)*FL-last_syn+1:n*FL-last_syn);[s_syn1,zi_syn] = filter(1,A,exc_syn1,zi_syn);exc_syn((n-1)*FL+1:n*FL) = exc_syn1; %计算得到的合成激励s_syn((n-1)*FL+1:n*FL) = s_syn1; %计算得到的合成语音last_syn = last_syn+PT*floor((n*FL-last_syn)/PT);%男声变女声PT1 =floor(PT/2); %减小基音周期poles = roots(A);deltaOMG =150*2*pi/fs;for p=1:10 %增加共振峰频率,实轴上方的极点逆时针转,下方顺时针转if imag(poles(p))>0 poles(p) = poles(p)*exp(j*deltaOMG);elseif imag(poles(p))<0 poles(p) = poles(p)*exp(-j*deltaOMG);endendA1=poly(poles);tempn_syn_t = [1:n*FL-last_syn_t]';exc_syn1_t = zeros(length(tempn_syn_t),1);exc_syn1_t(mod(tempn_syn_t,PT1)==0) = G; %某一段算出的脉冲exc_syn1_t = exc_syn1_t((n-1)*FL-last_syn_t+1:n*FL-last_syn_t);[s_syn1_t,zi_syn_t] = filter(1,A1,exc_syn1_t,zi_syn_t);exc_syn_t((n-1)*FL+1:n*FL) = exc_syn1_t; %计算得到的合成激励s_syn_t((n-1)*FL+1:n*FL) = s_syn1_t; %计算得到的合成语音last_syn_t = last_syn_t+PT1*floor((n*FL-last_syn_t)/PT1);end%绘图plot(s_syn_t),xlabel('n (samples)'), ...ylabel('Amplitude'), title('变换后语音信号'),...xlim([0,length(s_syn_t)]);%播放改变后的声音sound(2*s_syn_t);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% findpitch函数% 计算一段语音的基音周期function PT = findpitch(s)[B, A] = butter(5, 700/4000);s = filter(B,A,s);R = zeros(143,1);for k=1:143R(k) = s(144:223)'*s(144-k:223-k);end[R1,T1] = max(R(80:143));T1 = T1 + 79;R1 = R1/(norm(s(144-T1:223-T1))+1);[R2,T2] = max(R(40:79));T2 = T2 + 39;R2 = R2/(norm(s(144-T2:223-T2))+1);[R3,T3] = max(R(20:39));T3 = T3 + 19;R3 = R3/(norm(s(144-T3:223-T3))+1);Top = T1;Rop = R1;if R2 >= 0.85*RopRop = R2;Top = T2; endif R3 > 0.85*RopRop = R3;Top = T3; endPT = Top;return。
MATLAB变声器

MATLAB变声器电子工程学院摘要语音信号处理中的变声处理已经有了比较成熟的算法,本文阐述了变声算法的基础原理,利用数字滤波器,自相关法,LPC,LPC系数求根法等方法在MATLAB上改变语音信号的基频和共振峰以实现变声,并总结了现有变声算法的缺陷,对用不同的变换域能否改进变声算法做了粗略分析。
关键词:变声算法,LPC,变换域目录研究背景 (3)变声原理 (3)语音基本概念 (3)变声原理 (4)变声过程 (5)分帧处理 (5)计算LPC系数 (5)计算原始激励 (6)计算基音周期 (6)计算激励能量 (7)合成脉冲序列 (7)更改声道参数 (7)合成变声语音 (8)程序设计 (8)传统变声算法缺陷 (9)合成激励与原始激励差别较大 (9)不能实现定向变声 (10)实现定向变声的猜想 (11)小波域是否存在恒定音色参数粗略分析 (11)统计上的变换是否利于寻找恒定音色参数猜想 (11)下一步研究计划 (12)研究背景语音信号是人们日常生活中十分常见的信号,语音也是人与人之间传递信息的一种十分重要的方式。
随着智能终端以及互联网的普及,语音信号大量地以数字形式出现,语音信号处理变得越来越重要,变声处理是语音信号处理的基础之一,所以在这样的大背景下,研究变声算法并改进变声算法是很有意义而且有必要的。
另一方面,传统的变声算法是对发声过程的简单模拟进行语音合成,在模拟过程中改变参数以实现变声,而传统的变声算法存在一些缺陷,若要改进变声算法使其更灵活有效,那么细致的研究传统的变声算法是很有必要的。
变声原理语音基本概念1. 声道:声道是很多动物及人类都有的一个腔室,从声源产生的声音经由此处滤出。
人的声道包括声道则包括喉腔、咽头、口腔和鼻腔。
2. 基音:一般的声音都是由发音体发出的一系列频率、振幅各不相同的振动复合而成的。
这些振动中有一个频率最低的振动,由它发出的音就是基音,其余为泛音。
发音体整体振动产生的音,叫做基音,决定音高;发音体部分振动产生的音,叫做泛音,决定音色;基音和泛音结合一起而形成的音,叫做复合音,日常我们所听到的声音多为复合音。
hrtf旋转音效matlab实现

hrtf旋转⾳效matlab实现原理参考:将⾳频分段,各个段分别使⽤hrtf在Ls, L, R, Ls, Rrs, Lrs位置处的filter系数。
是声⾳听起来来⾃Ls, L, R, Rs, Rrs, Lrs,就有旋转效果。
hrtf = load("E:\CIPIC_hrtf_database\standard_hrir_database\subject_21\hrir_final.mat");Ls = [1 9];L = [8 9];R= [17 9];Rs = [25 9];Rrs = [17 41];Lrs = [8 41];%source from 5.1 to L chh_l_l = squeeze(hrtf.hrir_l(L(1), L(2), :));h_r_l = squeeze(hrtf.hrir_l(R(1), R(2), :));h_ls_l = squeeze(hrtf.hrir_l(Ls(1), Ls(2), :));h_rs_l = squeeze(hrtf.hrir_l(Rs(1), Rs(2), :));h_lrs_l = squeeze(hrtf.hrir_l(Lrs(1), Lrs(2), :));h_rrs_l = squeeze(hrtf.hrir_l(Rrs(1), Rrs(2), :));%source from 5.1 to R chh_l_r = squeeze(hrtf.hrir_r(L(1), L(2), :));h_r_r = squeeze(hrtf.hrir_r(R(1), R(2), :));h_ls_r = squeeze(hrtf.hrir_r(Ls(1), Ls(2), :));h_rs_r = squeeze(hrtf.hrir_r(Rs(1), Rs(2), :));h_lrs_r = squeeze(hrtf.hrir_r(Lrs(1), Lrs(2), :));h_rrs_r = squeeze(hrtf.hrir_r(Rrs(1), Rrs(2), :));[input, fs] = wavread('input.wav');h_l = zeros(length(h_l_l), 6);h_l(:, 1) = h_ls_l;h_l(:, 2) = h_l_l;h_l(:, 3) = h_r_l;h_l(:, 4) = h_rs_l;h_l(:, 5) = h_rrs_l;h_l(:, 6) = h_lrs_l;h_r = zeros(length(h_l_r), 6);h_r(:, 1) = h_ls_r;h_r(:, 2) = h_l_r;h_r(:, 3) = h_r_r;h_r(:, 4) = h_rs_r;h_r(:, 5) = h_rrs_r;h_r(:, 6) = h_lrs_r;idx = 0;%change to another direction every 2 second (every N samples)N = fs * 2;%divide audio to loop segment, each segment has N samplesloop = int32(length(input(:, 1)) / N);fadeSize = 1024;win = hamming(fadeSize * 2, 'symmetric');fadeIn = win(1: fadeSize, 1);fadeOut = win(fadeSize + 1 : fadeSize * 2, 1);y = zeros(length(input(:, 1)), 2);fadeInData = zeros(fadeSize, loop-1, 2);fadeOutData = zeros(fadeSize, loop-1, 2);fadeOutStartIdx = N - fadeSize;outputStartIdx = 1;for i = 1:1:loop-1 idx = mod(i, 6); if idx == 0 idx = 6; end %current segment is inputStartIdx ~inputEndIdx inputStartIdx = int32((i-1) * N + 1); inputEndIdx = int32(i * N); pInput = pOutput = zeros(N, 2); pInput(:, 1) = input(inputStartIdx : inputEndIdx, 1); pInput(:, 2) = input(inputStartIdx : inputEndIdx, 2); if i * N < length(input(:, 1)) %current segment filter with HRTF, which make the sound comes from direction idx. pOutput(:, 1) = filter(h_l(:, idx), 1, pInput(:, 1)); pOutput(:, 2) = filter(h_l(:, idx), 1, pInput(:, 2)); end %fadeIn fadeOut at the begine & end of current segment to avoid pop noise fadeInData(: ,i, 1) = pOutput(1: fadeSize, 1) .* fadeIn; fadeInData(: ,i, 2) = pOutput(1: fadeSize, 2) .* fadeIn; fadeOutData(: ,i, 1) = pOutput(fadeOutStartIdx + 1: N, 1) .* fadeOut; fadeOutData(: ,i, 2) = pOutput(fadeOutStartIdx + 1: N, 2) .* fadeOut; if i > 1 %add the cross fade data at the begin & end of segment y(outputStartIdx:outputStartIdx + fadeSize-1, 1) = fadeInData(:, i, 1) + fadeOutData(:, i-1, 1); y(outputStartIdx:outputStartIdx + fadeSize-1, 2) = fadeInData(:, i, 2) + fadeOutData(:, i-1, 2); else %the first segment y(outputStartIdx:outputStartIdx + fadeSize-1, 1) = fadeInData(:, i, 1) ; y(outputStartIdx:outputStartIdx + fadeSize-1, 2) = fadeInData(:, i, 2) ; end y(outputStartIdx + fadeSize + 1: outputStartIdx + fadeOutStartIdx , 1) = pOutput(fadeSize+1: fadeOutStartIdx, 1); y(outputStartIdx + fadeSize + 1: outputStartIdx + fadeOutStartIdx , 2) = pOutput(fadeSize+1: fadeOutStartIdx, 2); outputStartIdx = outputStartIdx + fadeOutStartIdx;end% low pass filter to avoid pop noise at the begin & end of segmentwn = 10000 / fs;[B, A] = butter(8, wn, 'low');y = filter[B, A, y]wavwrite(y, fs, 'output.wav');。
基于matlab的变声器设计与实现

文章主题:基于MATLAB的变声器设计与实现在音乐和语音处理领域,变声器是一种常见的设备,它可以改变声音的音调和音色,为音乐创作和语音处理带来了更多的可能性。
在本文中,我们将探讨基于MATLAB的变声器设计与实现,从原理到具体实现,为读者带来深入了解和实践指导。
一、变声器的原理和应用变声器是一种可以改变声音频率和谐波结构的设备,它在音乐创作、电子音乐制作以及语音处理中有着广泛的应用。
通过改变声音的音调和音色,变声器可以为音频增加特殊效果,为音乐创作和语音处理带来更多可能性。
二、MATLAB在音频处理中的应用MATLAB作为一种强大的科学计算软件,拥有丰富的音频处理工具和函数库,可以方便地进行音频数据处理和分析。
在音频处理中,MATLAB可以实现音频的读取、录制、分析、处理和合成,为音频处理带来了极大的便利。
三、基于MATLAB的变声器设计1. 我们需要了解音频信号的基本特性,包括音频信号的采样和量化过程,以及MATLAB中的音频数据表示方式。
2. 我们需要设计变声器的算法,包括音频信号的频率变换、时域变换和谐波结构的改变。
3. 接下来,我们可以使用MATLAB编写变声器的代码,包括音频信号的读取、处理和输出,以及界面设计和交互操作。
4. 我们还可以对设计好的变声器进行优化和测试,以确保其性能和效果达到预期的要求。
四、实现和应用通过上述步骤,我们可以在MATLAB中成功设计并实现一个变声器,可以对音频进行实时处理和效果展示。
在使用过程中,我们还可以根据具体音频的特点和需求,调整变声器的参数和效果,以获得更加理想的音频处理效果。
五、个人观点和总结基于MATLAB的变声器设计与实现,不仅可以帮助我们更好地理解音频处理的原理和方法,还可以为音乐创作和语音处理带来更多的可能性。
通过学习和实践,我们可以更深入地了解音频处理的相关知识,并掌握MATLAB在音频处理领域的应用技巧,为自己的学习和工作增添新的动力和乐趣。
区分男女声matlab代码

function varargout = boy_girl(varargin)% BOY_GIRL M-file for boy_girl.fig% BOY_GIRL, by itself, creates a new BOY_GIRL or raises the existing% singleton*.%% H = BOY_GIRL returns the handle to a new BOY_GIRL or the handle to% the existing singleton*.%% BOY_GIRL('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in BOY_GIRL.M with the given input arguments. %% BOY_GIRL('Property','Value',...) creates a new BOY_GIRL or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before boy_girl_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application % stop. All inputs are passed to boy_girl_OpeningFcn via varargin.%% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help boy_girl% Last Modified by GUIDE v2.5 07-May-2011 12:11:56% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @boy_girl_OpeningFcn, ...'gui_OutputFcn', @boy_girl_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before boy_girl is made visible.function boy_girl_OpeningFcn(hObject, ~, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to boy_girl (see VARARGIN)% Choose default command line output for boy_girlhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes boy_girl wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = boy_girl_OutputFcn(~, ~, handles)% varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.time_edit,'string','10');set(handles.freq_edit,'string','8000');% Get default command line output from handles structureoriginalvarargout{1} = handles.output;function time_edit_Callback(~, ~, ~)% hObject handle to time_edit (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,'String') returns contents of time_edit as text% str2double(get(hObject,'String')) returns contents of time_edit as a double% --- Executes during object creation, after setting all properties.function time_edit_CreateFcn(hObject, ~, ~)% hObject handle to time_edit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');endfunction freq_edit_Callback(~, eventdata, handles)% hObject handle to freq_edit (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,'String') returns contents of freq_edit as text% str2double(get(hObject,'String')) returns contents of freq_edit as a double% --- Executes during object creation, after setting all properties.function freq_edit_CreateFcn(hObject, ~, ~)% hObject handle to freq_edit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');end% --- Executes on button press in start_pushbutton.function start_pushbutton_Callback(hObject, ~, handles)% hObject handle to start_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)freq=str2num(get(handles.freq_edit,'String'));time=str2num(get(handles.time_edit,'string'));ai=analoginput('winsound',0);%初始化录音麦克chanel=addchannel(ai,1);%1表示单声道set(ai,'SampleRate',freq);T=time; %录音时间set(ai,'SamplesPerTrigger',T*freq);start(ai);y=0;time=0;[y,time]=getdata(ai);%读出相应的数据handles.y=y;handles.Fs=freq;handles.yoriginal=y;handles.foriginal=freq;guidata(hObject,handles);plot(handles.axes1,time,handles.y)%画出声音波形title(handles.axes1,'时域图');xlabel(handles.axes1,'时间');ylabel(handles.axes1,'频率');set(handles.axes1,'Xgrid','on');set(handles.axes1,'Ygrid','on');% --- Executes on selection change in xuanpin_popupmenu.function xuanpin_popupmenu_Callback(hObject, eventdata, handles)% hObject handle to xuanpin_popupmenu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)contents = cellstr(get(handles.xuanpin_popupmenu,'String')) ;%获取listbox1中的细胞矩阵item=contents{get(handles.xuanpin_popupmenu,'Value')};%获取listbox中的值if strcmp(item,'自定义')prompt={'频率编辑'};title='请输入频率:';lines=1;def={'8000'};answer=inputdlg(prompt,title,lines,def);handles.Fs=str2num(answer{1});guidata(hObject,handles);elseend% Hints: contents = cellstr(get(hObject,'String')) returns xuanpin_popupmenu contents as cell array% contents{get(hObject,'Value')} returns selected item from xuanpin_popupmenu% --- Executes during object creation, after setting all properties.function xuanpin_popupmenu_CreateFcn(hObject, eventdata, handles)% hObject handle to xuanpin_popupmenu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');end% --- Executes on button press in open_pushbutton.function open_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to open_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)[filename pathname]=uigetfile({'*.wav','ALL FILES(*.*)'},'选择声音文件');if isequal([filename pathname],[0,0])return;endstrname=[pathname filename];%选择的声音文件路径和文件名[data Fs]=wavread(strname);%temp表示声音数据Fs表示频率handles.y=data;handles.Fs=Fs;handles.yoriginal=data;handles.foriginal=Fs;guidata(hObject,handles);% --- Executes on button press in play_pushbutton.function play_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to play_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)c=handles.Fs;contents = cellstr(get(handles.xuanpin_popupmenu,'String')) ;%获取listbox1中的细胞矩阵item=contents{get(handles.xuanpin_popupmenu,'Value')};%获取listbox中的值if strcmp(item,'默认频率')Fs=c;elseif strcmp(item,'低频')Fs=0.7*c;elseif strcmp(item,'高频')Fs=1.3*c;elseFs=handles.Fs;end;wavplay(handles.y,Fs);N=length(handles.y);y1=fft(handles.y,N);f1=0:Fs/N:Fs*(N-1)/N;plot(handles.axes1,f1,handles.y);title(handles.axes1,'原始时域图');xlabel(handles.axes1,'时间');ylabel(handles.axes1,'频率');set(handles.axes1,'Xgrid','on');set(handles.axes1,'Ygrid','on');plot(handles.axes2,f1(1:N/2),y1(1:N/2));set(handles.axes2,'Xgrid','on');set(handles.axes2,'Ygrid','on');title(handles.axes2,'频谱图');xlabel( handles.axes2,'频率');ylabel( handles.axes2,'幅度');% --- Executes on button press in yuchuli2_radiobutton.function yuchuli2_radiobutton_Callback(hObject, eventdata, handles) % hObject handle to yuchuli2_radiobutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of yuchuli2_radiobutton% --- Executes on button press in yuchuli1_radiobutton.function yuchuli1_radiobutton_Callback(hObject, eventdata, handles) % hObject handle to yuchuli1_radiobutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of yuchuli1_radiobutton% --- Executes on selection change in yuchuli3_popupmenu.function yuchuli3_popupmenu_Callback(hObject, eventdata, handles)% hObject handle to yuchuli3_popupmenu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)contents = cellstr(get(handles.xuanpin_popupmenu,'String')) ;%获取listbox1中的细胞矩阵item=contents{get(handles.xuanpin_popupmenu,'Value')};%获取listbox中的值if strcmp(item,'低通滤波')prompt={'请输入滤波上限频率:'};title='请输入滤波上限频率:';lines=1;def={'2000'};answer=inputdlg(prompt,title,lines,def);handles.limit=str2num(answer{1});elseprompt={'请输入滤波下限频率:'};title='请输入滤波下限频率:';lines=1;def={'1000'};answer=inputdlg(prompt,title,lines,def);handles.limit=str2num(answer{1});endguidata(hObject,handles);% Hints: contents = cellstr(get(hObject,'String')) returns yuchuli3_popupmenu contents as cell array% contents{get(hObject,'Value')} returns selected item from yuchuli3_popupmenu% --- Executes during object creation, after setting all properties.function yuchuli3_popupmenu_CreateFcn(hObject, eventdata, handles)% hObject handle to yuchuli3_popupmenu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on button press in chuli_pushbutton.function chuli_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to chuli_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)contents = cellstr(get(handles.xuanpin_popupmenu,'String')) ;%获取listbox1中的细胞矩阵item=contents{get(handles.xuanpin_popupmenu,'Value')};%获取listbox中的值v1=get(handles.yuchuli1_radiobutton,'value');v2=get(handles.yuchuli2_radiobutton,'value');y=handles.y;fs=handles.Fs;if v1==1y=y-mean(y);elseendif v2==1y=detrend(y);elseendif strcmp(item,'低通滤波')fs=handles.Fs;limit=handles.limit-500;wp=pi*limit/fs; %归一化通带数字频率wpws=pi*(limit+200)/fs; %归一化阻带数字截止频率wsRp=0.5; %通带波纹系数RpRs=60;[N,Wn]=buttord(wp,ws,Rp,Rs); %求阶数N和滤波器截止频率Wn[num,den]=butter(N,Wn); %传输分子和分母的系数elsefs=handles.Fs;limit=handles.limit;wp=pi*limit/fs; %归一化通带数字频率wpws=pi*(limit-200)/fs;Rp=0.3; %通带波纹系数RpRs=80;[N,Wn]=buttord(wp,ws,Rp,Rs); %求阶数N和滤波器截止频率Wn[num,den]=butter(N,Wn,'high'); %传输分子和分母的系数endf=filter(num,den,y); %滤波M=max(size(f));s=length(f);zh=fft(f,s); %做FFT变换v=0:fs/s:fs*(s-1)/s; %计算频率plot(handles.axes3,v(1:M/2),zh(1:M/2))set(handles.axes3,'Xgrid','on');set(handles.axes3,'Ygrid','on');xlabel(handles.axes3,'频率');ylabel(handles.axes3,'振幅');title(handles.axes3,'滤波后的频谱图');plot(handles.axes1,v,handles.y);title(handles.axes1,'原始时域图');xlabel(handles.axes1,'时间');ylabel(handles.axes1,'频率');set(handles.axes1,'Xgrid','on');set(handles.axes1,'Ygrid','on');N=length(handles.y);y1=fft(handles.y,N);f1=0:fs/N:fs*(N-1)/N;plot(handles.axes2,f1(1:N/2),y1(1:N/2));set(handles.axes2,'Xgrid','on');set(handles.axes2,'Ygrid','on');title(handles.axes2,'频谱图');xlabel( handles.axes2,'频率');ylabel( handles.axes2,'幅度');handles.y=f;handles.Fs=fs;guidata(hObject,handles);% --- Executes on button press in close_pushbutton.function close_pushbutton_Callback(~, ~, ~)% hObject handle to close_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) close();% --- Executes on button press in fft_pushbutton.function fft_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to fft_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)data=handles.y;Fs=handles.Fs;len=max(size(data));%读出声音数据文件的长度[sel,ok]=listdlg('ListString',{'默认长度','128','256','512','1024','2048','4096','8192','16384','32768','65536'},...'name','选择FFT变换的点数', 'OKstring','确定', ...'Cancelstring','取消', 'SelectionMode','single','ListSize',[300 200]);data_frequency=[len,128,256,512,1024,2048,4096,8192,16384,32768,65536];if ok==1M=data_frequency(sel);y=data(1:M);%取前M个数据做变换N=length(y);zh=fft(y,N); %做FFT变换f=0:Fs/N:Fs*(N-1)/N; %计算频率plot(handles.axes2,f(1:M/2),zh(1:M/2))title(handles.axes2,'傅里叶变换图像');set(handles.axes2,'Xgrid','on');set(handles.axes2,'Ygrid','on');xlabel(handles.axes2,'频率');ylabel(handles.axes2,'振幅');elseendhandles.zh=zh;guidata(hObject,handles);% --- Executes on button press in fan_pushbutton.function fan_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to fan_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)zh=handles.zh;y=ifft(zh);plot(handles.axes3,y);title(handles.axes3,'反傅里叶变换图像')xlabel(handles.axes3,'数据序列');ylabel(handles.axes3,'频率');set(handles.axes3,'Xgrid','on');set(handles.axes3,'Ygrid','on');handles.y=y;guidata(hObject,handles);% --- Executes on button press in b2g_pushbutton.function b2g_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to b2g_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%读取音频信息(双声道,16位,频率44100Hz)% 定义常数FL = 80; % 帧长WL = 240; % 窗长P = 10; % 预测系数个数data=handles.y;fs=handles.Fs; % 载入语音数据data= data/max(data); %归一化L = length(data); % 读入语音长度FN = floor(L/FL)-2; % 计算帧数% 预测和重建滤波器exc = zeros(L,1); % 激励信号(预测误差)zi_pre = zeros(P,1); % 预测滤波器的状态s_rec = zeros(L,1); % 重建语音zi_rec = zeros(P,1);% 合成滤波器exc_syn = zeros(L,1); % 合成的激励信号(脉冲串)s_syn = zeros(L,1); % 合成语音last_syn = 0; %存储上一个(或多个)段的最后一个脉冲的下标zi_syn = zeros(P,1); % 合成滤波器的状态% 变调不变速滤波器exc_syn_t = zeros(L,1); % 合成的激励信号(脉冲串)s_syn_t = zeros(L,1); % 合成语音last_syn_t = 0; %存储上一个(或多个)段的最后一个脉冲的下标zi_syn_t = zeros(P,1); % 合成滤波器的状态% 变速不变调滤波器(假设速度减慢一倍)hw = hamming(WL); % 汉明窗% 依次处理每帧语音for n = 3:FN% 计算预测系数(不需要掌握)s_w = data(n*FL-WL+1:n*FL).*hw; %汉明窗加权后的语音[A E] = lpc(s_w, P); %用线性预测法计算P个预测系数% A是预测系数,E会被用来计算合成激励的能量s_f = data((n-1)*FL+1:n*FL); % 本帧语音,下面就要对它做处理% (4) 用filter函数s_f计算激励,注意保持滤波器状态[exc1,zi_pre] = filter(A,1,s_f,zi_pre);exc((n-1)*FL+1:n*FL) = exc1; %计算得到的激励% (5) 用filter函数和exc重建语音,注意保持滤波器状态[s_rec1,zi_rec] = filter(1,A,exc1,zi_rec);s_rec((n-1)*FL+1:n*FL) = s_rec1; %计算得到的重建语音% 注意下面只有在得到exc后才会计算正确s_Pitch = exc(n*FL-222:n*FL);PT = findpitch(s_Pitch); % 计算基音周期PT(不要求掌握)G = sqrt(E*PT); % 计算合成激励的能量G(不要求掌握)%方法3:本段激励只能修改本段长度tempn_syn = [1:n*FL-last_syn]';exc_syn1 = zeros(length(tempn_syn),1);exc_syn1(mod(tempn_syn,PT)==0) = G; %某一段算出的脉冲exc_syn1 = exc_syn1((n-1)*FL-last_syn+1:n*FL-last_syn);[s_syn1,zi_syn] = filter(1,A,exc_syn1,zi_syn);exc_syn((n-1)*FL+1:n*FL) = exc_syn1; %计算得到的合成激励s_syn((n-1)*FL+1:n*FL) = s_syn1; %计算得到的合成语音last_syn = last_syn+PT*floor((n*FL-last_syn)/PT);% (13) 将基音周期减小一半,将共振峰频率增加150Hz,重新合成语音,听听是啥感受~PT1 =floor(PT/2); %减小基音周期poles = roots(A);deltaOMG =150*2*pi/fs;for p=1:10 %增加共振峰频率,实轴上方的极点逆时针转,下方顺时针转if imag(poles(p))>0 poles(p) = poles(p)*exp(j*deltaOMG);elseif imag(poles(p))<0 poles(p) = poles(p)*exp(-j*deltaOMG);endendA1=poly(poles);tempn_syn_t = [1:n*FL-last_syn_t]';exc_syn1_t = zeros(length(tempn_syn_t),1);exc_syn1_t(mod(tempn_syn_t,PT1)==0) = G; %某一段算出的脉冲exc_syn1_t = exc_syn1_t((n-1)*FL-last_syn_t+1:n*FL-last_syn_t);[s_syn1_t,zi_syn_t] = filter(1,A1,exc_syn1_t,zi_syn_t);exc_syn_t((n-1)*FL+1:n*FL) = exc_syn1_t; %计算得到的合成激励s_syn_t((n-1)*FL+1:n*FL) = s_syn1_t; %计算得到的合成语音last_syn_t = last_syn_t+PT1*floor((n*FL-last_syn_t)/PT1);endplot(handles.axes3,s_syn_t),set(handles.axes3,'Xgrid','on');set(handles.axes3,'Ygrid','on');xlabel(handles.axes3,'数据序列');ylabel(handles.axes3,'频率');title(handles.axes3,'变音后的时域图'),XLim([0,length(s_syn_t)]);handles.y=s_syn_t;guidata(hObject,handles);Fs=handles.foriginal;N=length(handles.y);y1=fft(handles.y,N);f1=0:Fs/N:Fs*(N-1)/N;plot(handles.axes1,f1,handles.yoriginal);title(handles.axes1,'原始时域图');xlabel(handles.axes1,'时间');ylabel(handles.axes1,'频率');set(handles.axes1,'Xgrid','on');set(handles.axes1,'Ygrid','on');plot(handles.axes2,f1(1:N/2),y1(1:N/2));set(handles.axes2,'Xgrid','on');set(handles.axes2,'Ygrid','on');title(handles.axes2,'频谱图');xlabel( handles.axes2,'频率');ylabel( handles.axes2,'幅度');pause(3);sound(handles.yoriginal);sound(handles.y);% --- Executes on button press in g2b_pushbutton.function g2b_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to g2b_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) y=handles.y;fs=handles.Fs;%读取音频信息(双声道,16位,频率44100Hz)N=length(y)f=0:fs/N:fs*(N-1)/N;Y=fft(y,N); %进行傅立叶变换plot(handles.axes2,f(1:N/2),Y(1:N/2));title(handles.axes2,'声音信号的频谱');xlabel(handles.axes2,'频率');ylabel(handles.axes2,'振幅');f1=0:(fs*0.7)/N:(fs*0.7)*(N-1)/N;syms t;t=[0,9];R=y*exp(2*pi*300*t);P=fft(R,N);Z=ifft(P);z=real(Z);handles.y=y;plot(handles.axes3,f1(1:N/2),Z(1:N/2));title(handles.axes3,'变声后的时域图');xlabel(handles.axes3,'时间序列');ylabel(handles.axes3,'频率')set(handles.axes3,'Xgrid','on');set(handles.axes3,'Ygrid','on');Fs=handles.foriginal;N=length(handles.y);y1=fft(handles.y,N);f1=0:Fs/N:Fs*(N-1)/N;plot(handles.axes1,f1,handles.yoriginal);title(handles.axes1,'原始时域图');xlabel(handles.axes1,'时间');ylabel(handles.axes1,'频率');set(handles.axes1,'Xgrid','on');set(handles.axes1,'Ygrid','on');plot(handles.axes2,f1(1:N/2),y1(1:N/2));set(handles.axes2,'Xgrid','on');set(handles.axes2,'Ygrid','on');title(handles.axes2,'频谱图');xlabel( handles.axes2,'频率');ylabel( handles.axes2,'幅度');pause(3);guidata(hObject,handles);sound(handles.yoriginal);sound(handles.y,handles.foriginal*0.7);% --- Executes on button press in bijiao_pushbutton.function bijiao_pushbutton_Callback(hObject, eventdata, handles) % hObject handle to bijiao_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.y=handles.yoriginal;handles.Fs=handles.foriginal;guidata(hObject,handles);% --- Executes on button press in save_pushbutton.function save_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to save_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) Fs=handles.Fs;data=handles.y;[filename]=uiputfile({'*.wav'},'文件保存');wavwrite(data,Fs,filename);%data表示声音数据% 计算一段语音的基音周期function PT = findpitch(s)[B, A] = butter(5, 700/4000);s = filter(B,A,s);R = zeros(143,1);for k=1:143R(k) = s(144:223)'*s(144-k:223-k);end[R1,T1] = max(R(80:143));T1 = T1 + 79;R1 = R1/(norm(s(144-T1:223-T1))+1); [R2,T2] = max(R(40:79));T2 = T2 + 39;R2 = R2/(norm(s(144-T2:223-T2))+1); [R3,T3] = max(R(20:39));T3 = T3 + 19;R3 = R3/(norm(s(144-T3:223-T3))+1); Top = T1;Rop = R1;if R2 >= 0.85*RopRop = R2;Top = T2;endif R3 > 0.85*RopRop = R3;Top = T3;endPT = Top;return。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
% ct1clear all,close all, clc;% 定义常数FL = 80;% 帧长WL = 240;% 窗长P = 10;% 预测系数个数[s,fs] = wavread('sunday_2.wav');% 载入语音ss = s/max(s); %归一化L = length(s);% 读入语音长度FN = floor(L/FL)-2;% 计算帧数% 预测和重建滤波器exc = zeros(L,1);% 激励信号(预测误差)zi_pre = zeros(P,1);% 预测滤波器的状态s_rec = zeros(L,1);% 重建语音zi_rec = zeros(P,1);% 合成滤波器exc_syn = zeros(L,1);% 合成的激励信号(脉冲串)s_syn = zeros(L,1);% 合成语音last_syn = 0;%存储上一个(或多个)段的最后一个脉冲的下标zi_syn = zeros(P,1);% 合成滤波器的状态% 变调不变速滤波器exc_syn_t = zeros(L,1);% 合成的激励信号(脉冲串)s_syn_t = zeros(L,1);% 合成语音last_syn_t = 0;%存储上一个(或多个)段的最后一个脉冲的下标zi_syn_t = zeros(P,1);% 合成滤波器的状态% 变速不变调滤波器(假设速度减慢一倍)v=.5;exc_syn_v = zeros(v\L,1);% 合成的激励信号(脉冲串)s_syn_v = zeros(v\L,1);% 合成语音last_syn_v = 0;%存储上一个(或多个)段的最后一个脉冲的下标zi_syn_v = zeros(P,1);% 合成滤波器的状态hw = hamming(WL);% 汉明窗% 依次处理每帧语音for n = 3:FN% 计算预测系数(不需要掌握)s_w = s(n*FL-WL+1:n*FL).*hw;%汉明窗加权后的语音[A E] = lpc(s_w, P);%用线性预测法计算P个预测系数% A是预测系数,E会被用来计算合成激励的能量if n == 27% (3) 观察预测系统的零极点图zplane(1,A);ends_f = s((n-1)*FL+1:n*FL);% 本帧语音,下面就要对它做处理% (4) 用filter函数s_f计算激励,注意保持滤波器状态[exc1,zi_pre] = filter(A,1,s_f,zi_pre);exc((n-1)*FL+1:n*FL) = exc1; %计算得到的激励% (5) 用filter函数和exc重建语音,注意保持滤波器状态[s_rec1,zi_rec] = filter(1,A,exc1,zi_rec);s_rec((n-1)*FL+1:n*FL) = s_rec1; %计算得到的重建语音% 注意下面只有在得到exc后才会计算正确s_Pitch = exc(n*FL-222:n*FL);PT = findpitch(s_Pitch);% 计算基音周期PT(不要求掌握)G = sqrt(E*PT);% 计算合成激励的能量G(不要求掌握)%方法3:本段激励只能修改本段长度tempn_syn = [1:n*FL-last_syn]';exc_syn1 = zeros(length(tempn_syn),1);exc_syn1(mod(tempn_syn,PT)==0) = G; %某一段算出的脉冲exc_syn1 = exc_syn1((n-1)*FL-last_syn+1:n*FL-last_syn);[s_syn1,zi_syn] = filter(1,A,exc_syn1,zi_syn);exc_syn((n-1)*FL+1:n*FL) =exc_syn1;%计算得到的合成激励s_syn((n-1)*FL+1:n*FL) = s_syn1;%计算得到的合成语音last_syn = last_syn+PT*floor((n*FL-last_syn)/PT);% (11) 不改变基音周期和预测系数,将合成激励的长度增加一倍,再作为filter% 的输入得到新的合成语音,听一听是不是速度变慢了,但音调没有变。
FL_v = floor(FL/v);tempn_syn_v = [1:n*FL_v-last_syn_v]';exc_syn1_v = zeros(length(tempn_syn_v),1);exc_syn1_v(mod(tempn_syn_v,PT)==0) = G; %某一段算出的脉冲exc_syn1_v = exc_syn1_v((n-1)*FL_v-last_syn_v+1:n*FL_v-last_syn_v);[s_syn1_v,zi_syn_v] = filter(1,A,exc_syn1_v,zi_syn_v);last_syn_v = last_syn_v+PT*floor((n*FL_v-last_syn_v)/PT);exc_syn_v((n-1)*FL_v+1:n*FL_v) =exc_syn1_v;%计算得到的加长合成激励s_syn_v((n-1)*FL_v+1:n*FL_v) = s_syn1_v;%计算得到的加长合成语音% (13) 将基音周期减小一半,将共振峰频率增加150Hz,重新合成语音,听听是啥感受~PT1 =floor(PT/2);%减小基音周期poles = roots(A);deltaOMG = 150*2*pi/8000;for p=1:10%增加共振峰频率,实轴上方的极点逆时针转,下方顺时针转if imag(poles(p))>0 poles(p) = poles(p)*exp(j*deltaOMG);elseif imag(poles(p))<0 poles(p) = poles(p)*exp(-j*deltaOMG);endendA1=poly(poles);if n==27figure;zplane(1,A1);endtempn_syn_t = [1:n*FL-last_syn_t]';exc_syn1_t = zeros(length(tempn_syn_t),1);exc_syn1_t(mod(tempn_syn_t,PT1)==0) = G; %某一段算出的脉冲exc_syn1_t = exc_syn1_t((n-1)*FL-last_syn_t+1:n*FL-last_syn_t);[s_syn1_t,zi_syn_t] = filter(1,A1,exc_syn1_t,zi_syn_t);exc_syn_t((n-1)*FL+1:n*FL) =exc_syn1_t;%计算得到的合成激励s_syn_t((n-1)*FL+1:n*FL) = s_syn1_t;%计算得到的合成语音last_syn_t = last_syn_t+PT1*floor((n*FL-last_syn_t)/PT1);end% (6)s ,exc 和s_rec 的区别figure;subplot(3,1,1), plot(exc), xlabel('n (samples)'), ylabel('Amplitude'), title('激励信号');subplot(3,1,2), plot(s), xlabel('n (samples)'), ylabel('Amplitude'), title('原语音信号');subplot(3,1,3), plot(s_rec), xlabel('n (samples)'), ylabel('Amplitude'), title('重建语音信号'); figure;subplot(3,1,1), plot(exc), xlabel('n (samples)'), ylabel('Amplitude'), title('激励信号'), XLim([15*FL+1,16*FL]);subplot(3,1,2), plot(s), xlabel('n (samples)'), ylabel('Amplitude'), title('原语音信号'), XLim([15*FL+1,16*FL]);subplot(3,1,3), plot(s_rec), xlabel('n (samples)'), ylabel('Amplitude'), title('重建语音信号'), XLim([15*FL+1,16*FL]);sound(exc);pause(2);sound(s);pause(2);sound(s_rec);pause(2);%原始语音与合成语音的差别figure;subplot(3,1,1), plot(exc_syn), xlabel('n (samples)'), ylabel('Amplitude'), title('合成激励信号'); subplot(3,1,2), plot(s), xlabel('n (samples)'), ylabel('Amplitude'), title('原语音信号');subplot(3,1,3), plot(s_syn), xlabel('n (samples)'), ylabel('Amplitude'), title('合成语音信号');sound(s);pause(2);sound(s_syn);pause(2);%变速不变调figure;subplot(3,1,1), plot(exc_syn_v), xlabel('n (samples)'), ylabel('Amplitude'), title('合成慢速激励信号') ,XLim([0,length(exc_syn_v)]);subplot(3,1,2), plot(s), xlabel('n (samples)'), ylabel('Amplitude'), title('原语音信号'), XLim([0,length(s)]);subplot(3,1,3), plot(s_syn_v), xlabel('n (samples)'), ylabel('Amplitude'), title('合成慢速语音信号'), XLim([0,length(s_syn_v)]);sound(s);pause(2);sound(s_syn_v);pause(4);%变调不变速figure;subplot(3,1,1), plot(exc_syn_t), xlabel('n (samples)'), ylabel('Amplitude'), title('合成高调激励信号') ,XLim([0,length(exc_syn_t)]);subplot(3,1,2), plot(s), xlabel('n (samples)'), ylabel('Amplitude'), title('原语音信号'), XLim([0,length(s)]);subplot(3,1,3), plot(s_syn_t), xlabel('n (samples)'), ylabel('Amplitude'), title('合成高调语音信号'), XLim([0,length(s_syn_t)]);sound(s);pause(2);sound(s_syn_t);。