Tuesday, August 16, 2011

EC 6120 and EC 5102 DSP MATLAB PROGRAMS DAY 5

PROGRAMS FOR FILTERING LONG DATA SEQUENCES

%Program for MATLAB 7 users where circular convolution function is not in built
%if present the present program (a function) to b saved as ECEE.m can be replaced by 'cconv' command

function [c]=ECEE(a,b,N)
% a=[1 2 3 4];%input
% b=[1 2 -1 -2];%input
% N=6;%number of points
A=fft(a,N);
B=fft(b,N);
C=A.*B;
c=ifft(C,N);


OVERLAP SAVE METHOD
%Filtering long data sequence using OVERLAP SAVE METHOD
clc
clear all
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=[1 2 -1 2 3 -2 -3 -1 1 1 2 -1];%input
h=[1 2 1 1];%system
N=4;%length of each block
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yy=conv(x,h);
subplot 311, stem(yy)
title('Using LONG linear filtering')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (N
    error('N must be >=length(h)')
end
Nx=length(x);
M=length(h);
M1=M-1;
L=N-M1;
x=[zeros(1,M-1), x, zeros(1,N-1)];
h=[h zeros(1,N-M)];
K=floor((Nx+M1-1)/L);%no of blocks
y=zeros(K+1,N);
%dividing sequence in k blocks
for k=0:K
    xk=x(k*L+1:k*L+N);
    Y(k+1,:)=ECEE(xk,h,N);
end
Y=Y(:,M:N)';
y=(Y(:))';
subplot 312, stem(y)
title('Using Overlap SAVE method')
d=yy-y(1:length(yy));
subplot 313, stem(d)
title('Difference')

OVERLAP ADD METHOD
%Filtering long data sequence using OVERLAP ADD METHOD
clc
clear all
close all
x=[1 2 -1 2 3 -2 -3 -1 1 1 2 -1];%input
h=[1 2 1 1];%system
L=4;%length of each block
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yy=conv(x,h);
subplot 311, stem(yy)
title('Using LONG linear filtering')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Nx=length(x);
M=length(h);
M1=M-1;
R=rem(Nx,L);
N=L+M1;
x=[x zeros(1,L-R)];
h=[h zeros(1,N-M)];
K=floor(Nx/L); % number of blocks
y=zeros(K+1,N);
z=zeros(1,M1);
%dividing to K blocks
for k=0:K
    xp=x(L*k+1:L*k+L);
    xk=[xp z];
    y(k+1,:)=ECEE(xk,h,N);
end
yp=y';
[x,y]=size(yp);
for i=L+1:x;
    for j=1:y-1
        temp1=i-L;
        temp2=j+1;
        temp3=yp(temp1,temp2)+yp(i,j);
        yp(temp1,temp2)=temp3;
    end
end
z=1;
for j=1:y
    for i=1:x
        if ((i<=L & j<=y-1)|(j==y))
            ypnew(z)=yp(i,j);
            z=z+1;
        end
    end
end
y=ypnew;

subplot 312, stem(y)
title('Using Overlap Add method')
d=yy-y(1:length(yy));
subplot 313, stem(d)
title('Difference')






No comments: