Vous êtes sur la page 1sur 7

Submitted to:

Dr. Shoab A. Khan

Assignment # 3 ADSP
Signal Modeling (Levinson-Durban Recursion)

Submitted by:
Maj Muhammad Sajid
NS Asad Rehman
MS (Computer Engineering) - 81

Assignment # 3 Statement
Write the code in C or in Matlab and show its working in the lab or on your laptops .
Take a speech file. For every 256 samples of the file repeat the following:
Generate a 10th order all pole model.
Solve the model using Livenson Durbun recursion.
Write the difference equation using the coefficients obtained in (b)
Generate 256 samples from the difference equation in (c) save these samples
in a file
5. Keep the intermediate samples in the difference equation for the next
iteration
6. Repeat (a) to (e) for next 256 samples until you reach end of the file, append
zeroes in the last iteration if required.
1.
2.
3.
4.

Signal Modeling (Levinson-Durban Recursion)


This program generates 10th order all pole model of a given speech file, using
Levinson-Durban Recursion. Takes an impulse as

an input and generates output

speech file. Code given below fulfills all the requirements given above.

Main Code
%% Assignment3 Maj Muhammad Sajid & NS Asad Rehman
%% Run file MajSajid_NSAsad.m
%% Output Wave File will be generated at 'D:\outputfile.wav'
%% Wave File selected is of length 32768 and has 128 complete Frames of 256 samples, no
requirement of %%appending Zeros.
%% Sampling Frequency is 8KHz and 32 ms frame size gives frame size of 256 Samples
clear all;
%% Reading the input file
[WaveFile, FS, nbits] = wavread('cleanSpeech.wav');
wavplay(WaveFile, FS);

%% Setting the Samples Per Frame and Filter Order


SecondPerFrame = 32/1000; % setting frame length to 32ms
SamplesPerFrame = floor(FS * SecondPerFrame);
TotalFrames = floor(length(WaveFile) / SamplesPerFrame);
FilterOrder = 10;
%% Initializing
FilterCoefficients = zeros(TotalFrames, FilterOrder); % Filter coefficients for each frame
a = zeros(1,FilterOrder);
b = ones(1,1)
x = zeros(1,length(b));
y = zeros(1,length(a));
x_in = [1,zeros(1,SamplesPerFrame-1)];
outputDE = zeros(1,length(x_in));
Total_output=[];
Y=0;
flag=0;
for iFrame=1:TotalFrames
FrameBuffer = WaveFile( ((iFrame-1) * SamplesPerFrame + 1) : iFrame *SamplesPerFrame
);
%% Calculating Autocorrelation
r= zeros(1,FilterOrder + 1); % Initialzing to zero
for k = 1:FilterOrder + 1
for n = 1:SamplesPerFrame
d = n+k-1;
if (n+k-1 > SamplesPerFrame)
d= d-SamplesPerFrame;
end

r(k) = r(k) + FrameBuffer(n)*conj(FrameBuffer(d));


end
end
r = r./SamplesPerFrame; % Auto correlation vector

%% Calculating Filter Coefficients


FilterCoefficients(iFrame ,:) = calculate_levinson_durbin(r, FilterOrder);
%% Implementing Difference Equation
a = FilterCoefficients(iFrame ,:);
Y = filter(b,a,x_in);
outputDE = Y;
Total_output=[Total_output outputDE];
%%

Difference Equation can also be implemented as follows


%
flag=0;
%
for i = 1:length(x_in)
%%
Y1= GetFilterdValue(x_in(i),a,flag);
%
[Y1, x, y ] = IIR_filter ( a, b, x, y, x_in(i));
%
outputDE(i) = Y1;
%
flag=1;
%
end

end
FilterCoefficients
% %Playing the output audio
wavplay(Total_output,FS);
% % Plotting the input and output
figure1 = figure;
axes('Parent',figure1,'FontWeight','bold');
box('on');
hold('all');
plot(Total_output - WaveFile');
xlabel('Samples','FontWeight','bold');
ylabel('Error','FontWeight','bold');
title(['Sample by Sample Error b/w Original vs Modeled Signal - Levinson Durbin Algorithm
with M=',num2str(FilterOrder)],'FontWeight','bold');
%% Writing the output audio to a seperate file
wavwrite(Total_output, FS, 'D:\outputfile.wav');

Function Code to Calculate Levinson-Durban Recursion


% Writing a func to calculate Levinson Durbin Filter Coefficients
function [FilterCoefficients]=calculate_levinson_durbin(r,FilterOrder)
gamma = zeros(1,FilterOrder); % Initialization of parameters
tau = zeros(1,FilterOrder);
epsilon = zeros(1,FilterOrder);
a = zeros(FilterOrder, FilterOrder);
epsilon(1) = r(1);
gamma(1) = r(2);

a(1,1) = 1;
tau(1) = - r(2) / epsilon(1) ;
for m = 2:FilterOrder
for l = 1: m-1
gamma(m) = gamma(m) + r(m - l + 1)* a(m-1,l);
end
tau(m) = - gamma(m) / epsilon(m-1);
epsilon(m) = epsilon(m-1)* (1-abs(tau(m))^2);
for k = 1:m
if(k==1)
a(m,k)=1;
else
a(m,k) = a(m-1,k) + tau(m)*a(m-1,m-k+1);
end
end
end
FilterCoefficients = a(FilterOrder, :);

Function Code to Implement Difference Equation


function [ Y, x, y ] = IIR_filter ( a, b, x, y, x_in )
%
%
%
%
%

b = numerator coefficients
a = denominator coefficients
x_in = input sample
Y = output sample
x,y = input & output buffers

N_b = length(b);
N_a = length(a);
% shift right input buffer
for n = N_b-1:-1:1
x(n+1) = x(n);
end
% new sample
x(1) = x_in;
% convolution of b(n) and x(n)
y1 = 0;
for k=1:1:N_b
y1 = y1 + b(k)*x(k);
end
% convolution of a(n) and y(n)
y2 = 0;
for k=1:1:N_a
y2 = y2 + a(k)*y(k);
end
% shift right output buffer
for n = N_a-1:-1:1

y(n+1) = y(n);
end
y(1) = y1+y2;
Y = y(1);
end

Output

10 Order All Pole model for


128 Frames
a(1)

Frames

a(2)

a(3)

a(4)

a(5)

a(6)

a(7)

a(8)

a(9)

a(10)

To summarize screenshot of Workspace is given below

Comparison b/w Original and Modeled Signal


Comparison plot is given as under, maximum error is 2.4:-

Max Error

Vous aimerez peut-être aussi