% Matlab code for speech synthesis of the phonemes
% /s/, /i/, and /a/

% Import recorded sounds into Matlab
s = wavread('s.wav');
i = wavread('i.wav');
a = wavread('a.wav');

% Create voiced source -- a square pulse train
Fs=44100;                       % Sampling frequency
t=0:1/44100:1;
d=0:1/190:1;
type='rectpuls';
p=1/Fs;
voiced_src=pulstran(t,d,type,p);
plot(t,voiced_src)              % Look at pulse train
soundsc(voiced_src,Fs)          % Listen to pulse train

% Create voiceless source -- white noise
noise = randn(size(s));
soundsc(s,Fs)

% Synthesize /s/
[as,gs] = lpc(s,18)             % Find the LPC coefficients

as =

  Columns 1 through 7 

    1.0000   -1.2625    2.1864   -1.6780    1.8325   -1.0632    0.7653

  Columns 8 through 14 

   -0.6455    0.2085   -0.6054    0.2189   -0.7660    0.3697   -0.5771

  Columns 15 through 19 

    0.2845   -0.2900    0.2019   -0.1492    0.0857


gs =

   10.7022

s_synth = filter(gs,as,noise);  % Run source through the filter
soundsc(s,Fs)                   % Listen to the results
soundsc(s_synth,Fs)
specgram(s,512,Fs)              % Plot spectrograms
specgram(s_synth,512,Fs)

% Synthesize /i/
[ai,gi] = lpc(i,18)

ai =

  Columns 1 through 7 

    1.0000   -1.6154    0.5057    0.0183    0.1062    0.2071   -0.0028

  Columns 8 through 14 

   -0.2050   -0.0743   -0.0299   -0.0276    0.0258    0.1219    0.0175

  Columns 15 through 19 

   -0.0453    0.0016   -0.0016   -0.1988    0.2089


gi =

    0.3566

i_synth = filter(gi,ai,voiced_src);
soundsc(i,Fs)                   % Listen to the results
soundsc(i_synth,Fs)
freqz(gi, ai, 65536, Fs)        % Look at LPC spectral envelope

% Synthesize /a/
[aa,ga] = lpc(a,18)

aa =

  Columns 1 through 7 

    1.0000   -1.7946    0.4853    0.1820    0.1881    0.1806   -0.1151

  Columns 8 through 14 

   -0.1662    0.0552   -0.0859   -0.0989    0.1248    0.1796   -0.0306

  Columns 15 through 19 

   -0.0035   -0.0543   -0.1178    0.0031    0.0738


ga =

    0.2622

a_synth = filter(ga,aa,voiced_src);
soundsc(a,Fs)                   % Listen to the results
soundsc(a_synth,Fs)
freqz(ga, aa, 65536, Fs)        % Look at LPC spectral envelope

% Concatenate sounds in order to form words
sasi = [s_synth;a_synth';s_synth;i_synth'];
sasi = [s_synth(1:4000);a_synth(1:10000)';s_synth(1:6000);i_synth(1:7000)'];
sound(sasi,Fs)

sisa = [s_synth;i_synth';s_synth;a_synth'];
sisa = [s_synth(1:4000);i_synth(1:10000)';s_synth(1:6000);a_synth(1:9000)'];
sound(sisa, Fs)


