function dekoder(inname, outname); % inname je jmeno souboru se vstupem - napr. out.kod % outname je jmeno souboru s vystupem - napr. synt.wav lseg = 200; rseg = 120; % shift je tedy 80 => 100 vektoru za sekundu shift = lseg-rseg; P = 10; %%% read the matrix ff = fopen (inname,'r'); inp = fread (ff,[P+2 inf],'float'); fclose (ff); % get its size Nfr = size (inp,2); % and separate it into LPC, energy, LAGS A = [ones(1,Nfr); inp(1:P,:)]; E = inp(P+1,:); LAGS = inp(P+2,:); %%% generate excitation % first part of the first frame will be simply set to zeros. exc = zeros(1,lseg); % define last - position of peak last related to the beginning of NEXT % VOICED FRAME. last = 1; for ii=1:Nfr; l=floor(LAGS(ii)); % floor to be sure ... % in case of unvoiced frame, generate noise. if (l==0) e = randn(1,shift); % voiced - generate pulses. else e = zeros(1,shift); pulses = last:l:shift; % pulses can be only in 1:80. Limit them ! pulses = pulses(find(pulses >= 1)); % where is the last pulse ? In case it exists, remember for the next frame % and generate the pulses. if ~isempty(pulses) lastpulse = max(pulses); last = -shift -1 + lastpulse; e(pulses) = 1; % if there's no pulse, just re-compute the position of the last % generate nothing, let zeros. else last = last - shift; end end % add just generated excitation to the global one: exc = [exc e]; end %%%%%%%%%%% divide excitation again into frames ... er = frame (exc, lseg, rseg); synt = zeros(1,lseg); for ii=1:Nfr, e = er(:,ii); a = A (:,ii); ss = filter(1,a,e); % has 200 samples but I want just 80 ! ss = ss(rseg+1:lseg); % now must correct the energy Ewanted = E(ii); Eactual = 1/shift * sum(ss.^2); ratio = Ewanted / Eactual; ss = ss * sqrt(ratio); synt = [synt ss']; end plot(synt); title('synt'); soundsc(synt); wavwrite(synt,8000,16,outname);