vtapp.m

来自「这是一个用于语音信号处理的工具箱」· M 代码 · 共 201 行

M
201
字号
%Script: Apply the voicetype modification, and change the other acoustic 
%          parameters to agree with the voicetype change
% Input: vctyp1 
% Output: vctyp3, gci, gm and nidx
%      vtapp.m is a call function by "app_pb" on figure(Vtset_f).

%--------------------------------%
%  App=0 ==> undo the operation  %
%--------------------------------%

if App==0  %%% cancel the operation
   
   if exist('vctyp3')==0
      vctyp1=vctyp;
   else
      vctyp1=vctyp3;
   end
   clear sf_flag;
   vt_plt;
   return;
end
clear App;

%-----------------------------%
% 0. set the initial value    %
%-----------------------------%
vctyp3=vctyp1;
gci3=gci;
gm3=gm;
nidx3=nidx;

nidxpool=nidx(nidx>0);
nidxpool=nidxpool(:)';
idx=find(vctyp>0);

%--------------------------------------------------------------------%
% 1. correct the errend setting, such as one voiced frame surrend by %
%    unvoiced frames, vice versa.                                    %
%--------------------------------------------------------------------%

vctyp3(1)=0;  % the first frame must be unvoiced!!
vctyp3(nfram)=0;  % the last frame must be unvoiced!!
for kf=2:nfram-1
    if (vctyp3(kf-1)+vctyp3(kf+1))==0
           vctyp3(kf)=0;
    elseif ( vctyp3(kf-1)+vctyp3(kf+1) )==2
           vctyp3(kf)=1;
    end
end

%-------------------------------%
% 2. construct the gain contour %
%-------------------------------%
 
   m_len=basic(5)-basic(6);
   gm_x=1;
   gm_y=0;

   for kf=1:nfram

       startp=m_len*(kf-1)+Order+1;
       tmp_x=m_len/4*[0 1 2 3]+m_len/8+startp;
       gm_x=[gm_x tmp_x];
       gm_y=[gm_y ngm(kf,1:4)];
   end
   gm_x=[gm_x startp+m_len-1];
   gm_y=[gm_y 0];

%----------------------------------------------------------------------%
% 3. find the frames whose voicetypes have been changed and make other %
%    acoustic parameter changes accordingly                            %
%----------------------------------------------------------------------%

indx1=find((vctyp-vctyp3)==1);  % voiced => unvoiced
indx2=find((vctyp-vctyp3)==-1); % unvoiced => voiced

%%% === a voiced frame is altered to an unvoiced frame ===
for nk=1:length(indx1)

    nf=indx1(nk);
    startp=m_len*(nf-1)+Order+1;
    endp=startp+m_len+Order;

    % delete the glottal closure instants
    idx=find(gci3>=startp & gci3<=endp);
    gci3(idx)=[];

    % set the codebook index for unvoiced excitation
    seed=randperm( length(nidxpool) );
    nidx3(nf,1:4)=nidxpool( seed(1:4) );

end

%%% === an unvoiced frame is altered to a voiced frame ===

where_seg=find(diff(indx2)>1);

if ~isempty(indx2)
   clear gpcf3;
   sframe=indx2(1); % the starting frame

   for kk=1:(length(where_seg)+1)

       if kk==(length(where_seg)+1)
          eframe=max(indx2); % the ending frame
       else
          eframe=indx2(where_seg(kk));
       end

       startp=m_len*(sframe-1)+Order+1; % the atarting point
       endp=m_len*eframe+Order+1; % the ending point

       %*** set the new glottal closure instants ***
       if ( vctyp3(sframe-1)+vctyp3(eframe+1) )==2
          % both preceding and following frames are voiced
          gci_src=gci3( gci3>(startp-2*m_len) & gci3<startp );
          p00=round(mean( diff(gci_src) ));
          gci_src=gci3( gci3>endp & gci3<(endp+2*m_len) );
          p01=round(mean( diff(gci_src) ));
          gci_s=max( gci3(gci3<startp) );
          gci_e=min( gci3(gci3>endp) );
          gci_insert=gci_dis(p00,p01,gci_s,gci_e);

       elseif vctyp3(sframe-1)==1
          % the preceding frame is voiced
          gci_src=gci3( gci3>(startp-2*m_len) & gci3<startp );
          p00=round(mean( diff(gci_src) ));
          p01=0;
          gci_s=max( gci3(gci3<startp) );
          gci_e=endp-Order;
          gci_insert=gci_dis(p00,p01,gci_s,gci_e);

       elseif vctyp3(eframe+1)==1
          % followed by a voiced frame
          p00=0;
          gci_src=gci3( gci3>endp & gci3<(endp+2*m_len) );
          p01=round(mean( diff(gci_src) ));
          gci_s=startp;
          gci_e=min( gci3( gci3>endp ) );
          gci_insert=gci_dis(p00,p01,gci_s,gci_e);

       else
          % both preceding and following frames are unvoiced
          p00=pctour(gci);
          p01=0;
          gci_s=startp;
          gci_e=endp-Order;
          gci_insert=gci_dis(p00,p01,gci_s,gci_e);

       end

       idx=max( find( gci3<startp) );
       if isempty(idx)
          idx=0;
       end
       gci3=[gci3(1:idx) gci_insert gci3(idx+1:length(gci3))];

       %*** find the shape coefficients for glottal flow ***
       idx=find( gpcf(:,1)~=0 );
       pf=max(idx(idx<sframe));   % the preceding voiced frame
       if isempty(pf)
           pf=max(idx);
       end
       ff=max(idx(idx>eframe));  % the following voiced frame
       if isempty(ff)
           ff=min(idx);
       end
       dev=( gpcf(ff,1:7)-gpcf(pf,1:7) )/(eframe-sframe);
       for kf=sframe:1:eframe
          gpcf(kf,1:7)=gpcf(pf,1:7)+dev;
       end

       if kk~=(length(where_seg)+1)
          sframe=indx2(where_seg(kk)+1);
       end

   end  %for kk=1:(length(where_seg)+1)
end  %% if ~isempty(index2)

%-------------------------------%
% 3. construct new gain contour %
%-------------------------------%
 
 gm3=interp1( gm_x,gm_y,gci3 );
 gm3=gm3(:)';
 ave=mean(gm3);
 gm3=filter(0.4,[1 -0.4 -0.2],gm3); %% smooth
 gm3=gm3+(ave-mean(gm3));
 gm3=abs(gm3);

 gci=gci3;
 gm=gm3;
 vctyp=vctyp3;
 %Plot the new voice type
 clear sf_flag;
 vt_plt;

 clear nidxpool seed idx insert_gci gci3 gm3 startp endp dev vctyp3; %vctyp1;
 clear seg_eng tmp sframe eframe where_seg indx1 indx2 gci_src p00 p01;
 clear gm_x gm_y tmp_x;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?