DFT, IDFT and Linear convolution using overlap add and save method
-
Upload
assini-hussain -
Category
Documents
-
view
1.085 -
download
11
Transcript of DFT, IDFT and Linear convolution using overlap add and save method
Expt. No.3 DFT and linear convolution by DFT using MATLABPrograms:
a) DFT without fft
%DFT without fftfunction [Xk]=dft(x,N)
%padding zeros or truncating x(n) points L=length(x);if N>L xn=[x,zeros(1,N-L)];else for i=1:N xn(i)=x(i); endend
%Twiddle factorn=0:1:N-1;k=0:1:N-1;nk=n'*k; %Transpose of n or k is mustWN=exp(-j*2*pi/N);WNnk=WN.^nk;
%DFTXk=xn*WNnk;
Results:Input>> x=[1 2 3 4 3 2 1]; >> N=8;Output>> Xk=dft(x,N)Xk = Columns 1 through 5 16.0000 -4.8284 - 4.8284i 0 0.8284 - 0.8284i 0 Columns 6 through 8 0.8284 + 0.8284i 0 -4.8284 + 4.8284i
b) To plot Spectrum
%Program to plot spectrumfunction []=my_spectrum(Xk)
% length of XkN=length(Xk);k=0:1:N;
%Magnitude spectrumXk(N+1)=Xk(1); %Periodic property to get N+1 points
MXk=abs(Xk);display(‘Press any key to plot the MAGNITUDE & PHASE SPECTRUM of Xk’);pause;subplot(2,1,1);stem(k,MXk);grid on;title('Magnitude Spectrum of X(k)');xlabel('k');ylabel('Mag. of X(k)');
%Phase SpectrumPXk=angle(Xk);subplot(2,1,2);grid on;stem(k,PXk);title('Phase Spectrum of X(k)');xlabel('k');ylabel('Phase of X(k)');
Results:Input to DFT>> x=[1 1 1 1]; N=4;>> Xk=dft(x,N);To plot spectrum>> my_spectrum(Xk)Press any key to plot the MAGNITUDE & PHASE SPECTRUM of Xk
0 0.5 1 1.5 2 2.5 3 3.5 40
1
2
3
4Magnitude Spectrum of X(k)
k
Mag
. of
X(k
)
0 0.5 1 1.5 2 2.5 3 3.5 4-3
-2
-1
0Phase Spectrum of X(k)
k
Pha
se o
f X
(k)
>> x=[1 1 1 1]; N=16;>> Xk=dft(x,N);>> my_spectrum(Xk)Press any key to plot the MAGNITUDE & PHASE SPECTRUM of Xk
0 2 4 6 8 10 12 14 160
1
2
3
4Magnitude Spectrum of X(k)
k
Mag
. of
X(k
)
0 2 4 6 8 10 12 14 16-2
0
2
4Phase Spectrum of X(k)
k
Pha
se o
f X
(k)
x=[1 1 1 1]; N=128;>> Xk=dft(x,N);>> my_spectrum(Xk)Press any key to plot the MAGNITUDE & PHASE SPECTRUM of Xk
0 20 40 60 80 100 120 1400
1
2
3
4Magnitude Spectrum of X(k)
k
Mag.
of
X(k
)
0 20 40 60 80 100 120 140-4
-2
0
2
4Phase Spectrum of X(k)
k
Phase o
f X
(k)
c) IDFT without ifft%DFT without fft
function [xn]=idft(X,N)%padding zeros or truncating x(n) points
L=length(X);if N>L Xk=[X,zeros(1,N-L)];else for i=1:N Xk(i)=X(i); endend
%Twiddle factorn=0:1:N-1;k=0:1:N-1;nk=n'*k; %Transpose of n or k is mustWN=exp(-j*2*pi/N);WNnk=WN.^-nk;
%DFTxn=(Xk*WNnk)/N;Results:
Input
>> Xk=[16 -4.8284-4.8284i 0 0.8284-0.8284i 0 0.8284+0.8284i 0-8284+4.8284i];
>> N=8;
Output
>> xn=idft(Xk,N)
xn =
Columns 1 through 5
1.0000 2.0000 3.0000 - 0.0000i 4.0000 - 0.0000i 3.0000 - 0.0000i
Columns 6 through 8
2.0000 1.0000 + 0.0000i 0.0000 + 0.0000i
Imaginary part of x(n) is not exact zero due to truncating of Twiddle factor. To avoid this use abs( )
>> xn=abs(idft(Xk,N))
xn =
1.0000 2.0000 3.0000 4.0000 3.0000 2.0000 1.0000 0.0000
d) DFT using fft%DFT using 'fft'
clc;clear all;close all;
%inputxn=input('Enter the input in matrix form: ');N=input('Enter the number of points: ');
%DFTXk=fft(xn,N);
%Display disp('Press any key to display x(n) and X(k)');pause;disp('x(n) = ');disp(xn);disp('X(k) ');disp(Xk);
%Plot disp('Press any key to plot input and output');pause;n=0:1:N;k=0:1:N;xn(N+1)=0;Xk(N+1)=Xk(1);subplot(3,1,1);stem(n,xn);title('Input x(n)');xlabel('n');ylabel('x(n)');RXk=real(Xk); %real part of X(k)IXk=imag(Xk); %imaginary part of X(k)subplot(3,1,2);stem(k,RXk);title('X(k)');xlabel('k');ylabel('Real part of X(k)');subplot(3,1,3);stem(k,IXk);title('X(k)');xlabel('k');ylabel('Imaginary part of X(k)');
Results
Inputs
Enter the input in matrix form: [1 1 1 1]
Enter the number of points: 4
Outputs
Press any key to display x(n) and X(k)
x(n) =
1 1 1 1
X(k)
4 0 0 0
Press any key to plot input and output
0 0.5 1 1.5 2 2.5 3 3.5 40
0.5
1Input x(n)
n
x(n
)
0 0.5 1 1.5 2 2.5 3 3.5 40
2
4X(k)
k
Real part
of
X(k
)
0 0.5 1 1.5 2 2.5 3 3.5 4-1
0
1X(k)
kImagin
ary
part
of
X(k
)
Enter the input in matrix form: [1 1 1 1]
Enter the number of points: 16
0 2 4 6 8 10 12 14 160
0.5
1Input x(n)
n
x(n
)
0 2 4 6 8 10 12 14 16-5
0
5X(k)
k
Real part
of
X(k
)
0 2 4 6 8 10 12 14 16-5
0
5X(k)
kImagin
ary
part
of
X(k
)
Enter the input in matrix form: [1 1 1 1]
Enter the number of points: 128
0 20 40 60 80 100 120 1400
0.5
1Input x(n)
n
x(n
)
0 20 40 60 80 100 120 140-5
0
5X(k)
k
Real part
of
X(k
)
0 20 40 60 80 100 120 140-5
0
5X(k)
kImagin
ary
part
of
X(k
)
e) IDFT using ifft%DFT using 'fft'
clc;clear all;close all;
%inputXk=input('Enter the input in matrix form: ');N=input('Enter the number of points: ');
%IDFTxn=ifft(Xk,N);
%Display disp('Press any key to display X(k) and x(n)');pause;disp('X(k) = ');disp(Xk);disp('x(n)');disp(xn);ResultsInputEnter the input in matrix form: [16 -4.8284-4.8284i 0 0.8284-0.8284i 0 0.8284+0.8284i 0 -4.8284+4.8284i]Enter the number of points: 8OutputPress any key to display X(k) and x(n)X(k) = Columns 1 through 5 16.0000 -4.8284 - 4.8284i 0 0.8284 - 0.8284i 0 Columns 6 through 8 0.8284 + 0.8284i 0 -4.8284 + 4.8284ix(n) 1.0000 2.0000 3.0000 4.0000 3.0000 2.0000 1.0000 0.0000
f) Linear convolution using DFT by overlap-add method%Linear convolution by DFT using Overlap-add method
function [y] = ovradd(x,h,N)M = length(h);%len_hNx = length(x);%len_xif(N<M) error('The memory size should at least be equal to or greater than the size of the filter response h(n)');endL = N - M + 1; %Length of Blockif(mod(Nx,L)~=0) x=[x zeros(1,(L-mod(Nx,L)))]; %Padding the input sequence with appropriate zerosend
% Splitting the input sequence into blocks of processing size
Nx =length(x);B = ceil(Nx/L); %No. of blocksindex=0;xsplit=zeros(B,L);for i=1:B for j=1:L index=index+1; xsplit(i,j)=x(index); endendhz=[h zeros(1,N-M)]; %Circular convolution needs both sequences to be of equal lengths
% Overlap-add Method%Blocks of x(n) padded with zeros in the last M-1 positions
xadd=zeros(B,N);for i=1:B xadd(i,:)=[xsplit(i,:) zeros(1,M-1)];end
%Circular convolution operationfor m=1:B ym(m,:)=cconv(xadd(m,:),hz,N);end
%Adding the first M-1 terms of next block to the last M-1 terms of previous blocky=zeros(B, N);if(B>1) for i=1:B-1 temp=ym(i+1,:); yi(i,:)=[ym(i,1:end-M+1) (ym(i,end-M+2:end)+temp(1,1:M-1))]; end yi(B,:)=[ym(B,:)];else yi=ym;end
%Output to one-dimensional arrayy=zeros(1,Nx+M-1);for i=1:B if(i==1) y=yi(i,:); if(B==1) y=[yi(i,1:end-M+1)]; end elseif(i==B) y=[y yi(i,M:end-M+3)]; else y=[y yi(i,M:end)]; endend
len_y=len_x+M-1;y=[y(1:len_y)];disp(y');ResultsInput>> x=[0:1:15 14:-1:0]x = Columns 1 through 170 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14Columns 18 through 3113 12 11 10 9 8 7 6 5 4 3 2 1 0>> h=[1 1 1 1];
>> N=10;
>> ovradd(x,h,N);
0 1.0000 3.0000 6.0000 10.0000 14.0000 18.0000 22.0000 26.0000 30.0000 34.0000 38.0000 42.0000 46.0000 50.0000 54.0000 56.0000 56.0000 54.0000 50.0000 46.0000 42.0000 38.0000 34.0000 30.0000 26.0000 22.0000 18.0000 14.0000 10.0000 6.0000 3.0000 1.0000 0
g) Linear convolution using DFT by overlap-save method%Linear convolution using DFT by Overlap-save method
function [y] = ovrsave(x,h,N)M = length(h);%len_hNx = length(x);%len_xlen_x=Nx;if(N<M) error('The memory size should at least be equal to or greater than the size of the filter response h(n)');end
%Padding the input sequence with appropriate zerosL = N - M + 1; %Blocksif(mod(Nx,L)~=0) x=[x zeros(1,(L-mod(Nx,L)))]; end% Splitting the input sequence into blocks of processing sizeNx =length(x);B = ceil(Nx/L); %No. of blocksindex=0;split=zeros(B,L);for i=1:B for j=1:L index=index+1; split(i,j)=x(index); end
endhz=[h zeros(1,N-M)]; %Circular convolution needs both sequences to be of equal lengths
%Overlap Save Method%Blocks of x(n) padded with appropriate members from the original array
xblock=zeros(B,N);for i=1:B if(i==1) temp= [zeros(1,M-1) split(i,:)]; xblock(i,:)= temp; else temp=xblock(i-1,:); xblock(i,:)=[temp(1,N-M+2:end) split(i,:)]; endend
% Discarding the first m-1 termsy=zeros(B,N-M+1);for i=1: B temp=ym(i,:); y(i,:)=temp(1,M:end);end
%Output to proper formy=y';y=[y(1:(len_x+M-1))];Results:InputsInput>> x=[0:1:15 14:-1:0]x = Columns 1 through 170 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14Columns 18 through 3113 12 11 10 9 8 7 6 5 4 3 2 1 0>> h=[1 1 1 1];
>> N=20;
>> ovrsave(x,h,N);
0 1.0000 3.0000 6.0000 10.0000 14.0000 18.0000 22.0000 26.0000 30.0000 34.0000 38.0000 42.0000 46.0000 50.0000 54.0000 56.0000 56.0000 54.0000 50.0000 46.0000 42.0000 38.0000 34.0000 30.0000 26.0000 22.0000 18.0000 14.0000 10.0000 6.0000 3.0000 1.0000 0