⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mlp.m

📁 用MATLAB编写的LPC编码器及解码器源代码
💻 M
字号:
clc; 
clear all;

InputFilename = 'aa.wav'; %change it according to your wave files 

[inspeech, Fs, bits] = wavread(InputFilename); % read the wavefile

Order = 10; % order of the model used by LPC
L = 10;
fr=22.5;
fs=25;
 preemp = .9378;
 
 data=inspeech;
 sr=Fs;
 
 [row col] = size(data);
if col==1 data=data'; end


nframe = 0; 
msfr = round(sr/1000*fr); % Convert ms to samples
msfs = round(sr/1000*fs); % Convert ms to samples
duration = length(data);
speech = filter([1 -preemp], 1, data)'; % Preemphasize speech
msoverlap = msfs - msfr;
ramp = [0:1/(msoverlap-1):1]'; % Compute part of window


for frameIndex=1:msfr:duration-msfs+1 % frame rate=20ms
frameData = speech(frameIndex:(frameIndex+msfs-1)); % frame size=30ms
nframe = nframe+1;

A=lpc(frameData,10);
A=A';
% Levinson's method

aCoeff(:,nframe) =A;

% Calculate the filter 
% response
% by evaluating the 
% z-transform
if 0
gain=0;
cft=0:(1/255):1;
for index=1:L
gain = gain + aCoeff(index,nframe)*exp(-i*2*pi*cft).^index;
end
gain = abs(1./gain);
spec(:,nframe) = 20*log10(gain(1:128))';
plot(20*log10(gain));
title(nframe);
drawnow;
end


if 0
impulseResponse = filter(1, aCoeff(:,nframe), [1 zeros(1,255)]);
freqResp = 20*log10(abs(fft(impulseResponse)));
plot(freqResp);
end

errSig = filter([1 A'],1,frameData); % find excitation noise

      % gain%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
autoCorErr = xcorr(errSig); % calculate pitch & voicing information
[B,I] = sort(autoCorErr);
num = length(I);
if B(num-1) > .01*B(num)
pitch(nframe) = abs(I(num) - I(num-1));
else
pitch(nframe) = 0;
end

% calculate additional info to improve the compressed sound quality
resid(:,nframe) = errSig;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
if(frameIndex==1) % add residual frames using a trapezoidal window
stream = resid(1:msfr,nframe);
else
stream = [stream; 
overlap+resid(1:msoverlap,nframe).*ramp; 
resid(msoverlap+1:msfr,nframe)];
end
if(frameIndex+msfr+msfs-1 > duration)
stream = [stream; resid(msfr+1:msfs,nframe)];
else
overlap = resid(msfr+1:msfs,nframe).*flipud(ramp); 
end 
end
stream = filter(1, [1 -preemp], stream)';

% perform a discrete cosine transform on the residual
resid = dct(resid);
[a,b] = size(resid);
% only use the first 50 DCT-coefficients this can be done
% because most of the energy of the signal is conserved in these coeffs
resid = [ resid(1:50,:); zeros(430,b) ];

% quantize the data
resid = uencode(resid,4);
resid = udecode(resid,4);

% perform an inverse DCT
resid = idct(resid);

% add some noise to the signal to make it sound better
noise = [ zeros(50,b); 0.01*randn(430,b) ];
resid = resid + noise;

% decode/synthesize speech using LPC and the compressed residual as excitation


%
% 
source=resid;


[L1 nframe] = size(aCoeff); % L1 = 1+number of LPC coeffs 

[row col] = size(source);
if(row==1 | col==1) % continous stream; must be windowed
postFilter = 0; duration = length(source); frameIndex = 1;
for sampleIndex=1:msfr:duration-msfs+1
resid(:,frameIndex) = source(sampleIndex:(sampleIndex+msfs-1))';
frameIndex = frameIndex+1; 
end
else
postFilter = 1; resid = source;
end

[row col] = size(resid);
if col<nframe 
nframe=col;
end

for frameIndex=1:nframe
A = aCoeff(:,frameIndex);
residFrame = resid(:,frameIndex)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*G(frameIndex);
synFrame = filter(1, A', residFrame); % synthesize speech from LPC coeffs

if(frameIndex==1) % add synthesized frames using a trapezoidal window
synWave = synFrame(1:msfr); 
else
synWave = [synWave; overlap+synFrame(1:msoverlap).*ramp; ...
synFrame(msoverlap+1:msfr)];
end
if(frameIndex==nframe)
synWave = [synWave; synFrame(msfr+1:msfs)];
else
overlap = synFrame(msfr+1:msfs).*flipud(ramp); 
end 
end;

if(postFilter)
synWave = filter(1, [1 -preemp], synWave);
end
outspeech2=synWave;

% display the results
figure(1);
subplot(2,1,1);
plot(inspeech);
grid;

subplot(2,1,2);
plot(outspeech2);
grid;

disp('Press a key to play the original sound!');
pause;
sound(inspeech, Fs);


disp('Press a key to play the voice-excited LPC compressed sound!');
pause;
sound(outspeech2, Fs); 

⌨️ 快捷键说明

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