synthesis_go.m

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

M
222
字号
%To generate synthetic speech using the source and the formants specified
%Written by albert Hsiao
%Changes and interface by Karthik
%Modified by D. G. Childers 2/25/98

Ntotal = length(excitation);
speech1=zeros(1,Ntotal);
integral_excitation=filter(1,[1 -.97],excitation); %integrate the excitation to get the glottal flow
excitation=integral_excitation;
%use the glottal flow as the excitation
if file_flag == 0
   Lens_array = Pitch;
end

%--------------------------------------------------------------------%
%  process the excitation signal throught the formant filter %
%--------------------------------------------------------------------%

segment_no=1;
if exist ('Nframe1')
   Nframe=Nframe1;
end
Lens=Lens_array(segment_no);
%Lens=Pitch(1);
start_point=1;
end_point=Lens;

ff=Ff(segment_no,:);
fb=Fb(segment_no,:);
rts=ff2root(ff,fb);
pcofa=real( poly(rts) );
gp=excitation(start_point:end_point);
gm=Vgain(segment_no)+Ngain(segment_no)+Ncof(segment_no,1); % voiced gain + unvoiced gain
cofa1=pcofa;
cofa1(Lens)=0;
sscon=real(ifft(fft(gp)./fft(cofa1)));
amp=(1/2)*sqrt(gm*Lens/(sscon*sscon'));
speech1(start_point:end_point)=sscon*amp;

%Below is a fix for excitation amplitude.
temp_excitation(start_point:end_point)=excitation(start_point:end_point);
temp_amp=(1/2)*sqrt(gm*Lens/(temp_excitation*temp_excitation'));
excitation(start_point:end_point)=gp*temp_amp;
%end of fix

pwfsm=0;
for segment_no=2:Nframe
   ocofa=pcofa;
   Lens=Lens_array(segment_no);
   %Lens=Pitch(segment_no);
   start_point=end_point+1;
   end_point=end_point+Lens;
   if end_point > Ntotal
      end_point = Ntotal;
      Lens = end_point - start_point +1;
   end
   ff=Ff(segment_no,:);
   fb=Fb(segment_no,:);
   rts=ff2root(ff,fb);
   pcofa=real( poly(rts) );
   filt_len=length(pcofa)-1;
   Ziy=speech1( start_point:-1:start_point-filt_len+1);    
   gp=excitation(start_point:end_point);
   gm=Vgain(segment_no)+Ngain(segment_no)+Ncof(segment_no,1); % voiced gain + unvoiced gain
   Ziydc=mean(Ziy);
   fxac=filt(1,ocofa,zeros(size(gp)),Ziy-Ziydc);
   % 'fxac' = deviation signal.
   fxdc=filt(1,ocofa,zeros(size(gp)),ones(size(Ziy)));
   % 'fxdc' = mean value signal.
   fx=fxac+Ziydc*fxdc;	
   gx=filter(1,pcofa,gp);
   gxtemp=gx;
   ncomp=0;
   while ncomp<=5
      Ziy1=gxtemp(Lens:-1:Lens-filt_len+1);
      dcamp=mean(Ziy1);
      Ziy1=Ziy1-dcamp;
      tamp=sqrt((Ziy1*Ziy1')/(Ziy*Ziy'));
      gxtemp=gx+tamp*fxac+dcamp*fxdc;
      ncomp=ncomp+1;
   end;
   pwf=sqrt((gx*gx')/(gxtemp*gxtemp'));
   if pwfsm==0
      pwfsm=pwf;
   end;
   smf=.7;
   pwfsm=(1-smf)*pwf+smf*pwfsm;
   amp=sqrt(gm*Lens/(gx*gx'))*pwfsm;
   speech1(start_point:end_point)=amp*gx+fx;
   excitation(start_point:end_point)=gp*amp;
end

%Create display figure
PV = [179 55 611 464];
s2 = 'Speech Display window';

% Open analysis window
while exist('speech_display_fig')==1
   try1 = 'get(speech_display_fig,''position'');';
   eval(try1,catch2);
   if check ==0
      clear speech_display_fig;
      check = 1;
      break;
   end
   s1 = get(speech_display_fig,'Name');
   if ~strcmp(s1,s2)
      clear speech_display_fig;
      break;
   end
   figure(speech_display_fig);
   break;
end;

if exist('speech_display_fig')~=1;
   speech_display_fig= figure('Position',PV,...
      'Resize','on',...
      'Numbertitle','off',...
      'Color',[0.8 0.8 0.8],...
      'Name',s2);
end

clf;

ax1=axes('Position',[0.08 0.54 0.75 0.40]);
ax2=axes('Position',[0.08 0.04 0.75 0.40]);

uicontrol('Style','Pushbutton',...
   'Units','normalized',...
   'Position',[.85 .85 .12 .08], ...
   'String','play',...
   'Callback', 'soundsc(excitation,Srate);');

uicontrol('Style','Pushbutton',...
'Units','Normalized',...   
'Position',[.85 .70 .12 .08], ...
'String','spectrogram',...
'Callback', 'sgram2'); %sgram2--excitation for synthesized speech

uicontrol('Style','Pushbutton',...
   'Units','normalized',...
   'Position',[.85 .55 .12 .08], ...
   'String','waveform',...
   'Callback', 'axes(ax1);plot(excitation,''b'');title(''Excitation Signal'');');

uicontrol('Style','Pushbutton',...
   'Units','Normalized',...
   'Position',[.85 .35 .12 .08], ...
   'String','play',...
   'Callback', 'soundsc(speech1,Srate);');

uicontrol('Style','Pushbutton',...
   'Units','Normalized',...   
   'Position',[.85 .20 .12 .08], ...
   'String','Spectrogram',...
   'Units','normalized',...
   'Callback', 'sgram1'); %sgram1--speech1--synthesized speech
uicontrol('Style','Pushbutton',...
   'Units','normalized',...
   'Position',[.85 .05 .12 .08], ...
   'String','waveform',...
   'Callback', 'axes(ax2);plot(speech1,''b'');title(''Synthesized speech'');');

axes(ax1);
plot(excitation,'b'); 
title('Excitation Signal');
axes(ax2); 
plot(speech1,'b');
title('Synthesized Speech');


%Create a window to enable save and quit features

PV = [8 121 154 163];
s2 = 'Synthesis  Menu';

% Open analysis window
while exist('synth_fig_1')==1
   try1 = 'get(synth_fig_1,''position'');';
   eval(try1,catch2);
   if check ==0
      clear synth_fig_1;
      check = 1;
      break;
   end
   s1 = get(synth_fig_1,'Name');
   if ~strcmp(s1,s2)
      clear synth_fig_1;
      break;
   end
   figure(synth_fig_1);
   break;
end;

if exist('synth_fig_1')~=1;
   
   synth_fig_1=figure('Position',PV,...
      'Resize','off',...
      'Numbertitle','off',...
      'Color',[0.5 0.5 0.5],...
      'Name',s2);
   
   uicontrol('Style','frame',...
      'Units','Normalized',...
      'Position',[0.03 0.03 0.94 0.94],...
      'BackGroundColor',[0.9 0.9 0.9]);
   
   speech_save_popup = uicontrol('Style','Pushbutton',...
      'Units','Normalized',...
      'Position',[0.1 0.65 0.8 .2],...
      'String','Save(Ascii)',...
      'Callback','save_speech');
         
   uicontrol('Style','Pushbutton',...
      'Units','Normalized',...
      'Position',[0.1  0.2 0.8 .2],...
      'String','Cancel',...
      'Callback','speech_quit');
end


⌨️ 快捷键说明

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