Você está na página 1de 9

%Computation of N Point DFT a sequence 2.

8284
clc; Phase spectrum:
clear all; X_ang =
close all; 0
2.3562
x1=input('Enter the sequence x(n): '); -3.1416
N=input('Enter the length of DFT: '); -2.3562
x=[x1,zeros(1,N-length(x1))]
k=0:N-1
n=k
powers=n.'*k;
disp('DFT matrix for given is');
WNkn=(exp(-j*2*pi/N)).^powers
disp('The N point DFT of the given sequence is')
X=WNkn*x.' % x is sequence
after zero padding
disp('The N point DFT of the given sequence using
inbuilt function is')
X1=fft(x1,N) % for inbuilt use
original sequence
disp('Magnitude spectrum:')
X_mag=abs(X)
disp('Phase spectrum:')
X_ang=angle(X)

figure
subplot(3,1,1);
stem(0:length(x1)-1,x1); grid on;
xlabel('---->n');
ylabel('---->amplitude');
title('Input sequence x(n)')

subplot(3,1,2);
stem(0:length(X_mag)-1,X_mag); grid on;
xlabel('---->k');
ylabel('---->|X(k)|');
title('Magnitude spectrum')

subplot(3,1,3);
stem(0:length(X_ang)-1,X_ang); grid on;
xlabel('---->k');
ylabel('----> <X(k)');
title('Phase spectrum')

OUTPUT:

DFT matrix for given is


WNkn =
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 - 1.0000i -1.0000 - 0.0000i -0.0000 +
1.0000i
1.0000 + 0.0000i -1.0000 - 0.0000i 1.0000 + 0.0000i -1.0000 -
0.0000i
1.0000 + 0.0000i -0.0000 + 1.0000i -1.0000 - 0.0000i 0.0000 -
1.0000i

The N point DFT of the given sequence isX =


10.0000 + 0.0000i
-2.0000 + 2.0000i
-2.0000 - 0.0000i
-2.0000 - 2.0000i
The N point DFT of the given sequence using inbuilt function is
X1 =10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 -
2.0000i

Magnitude spectrum:
X_mag =
10.0000
2.8284
2.0000
%Computation of N Point IDFT a sequence The N point IDFT of the given sequence using inbuilt
clc; function is
clear all;
close all;
X1 =
x1=input('Enter the sequence X(n): ');
N=input('Enter the length of IDFT: '); -1.0000 + 0.0000i 1.0000 - 0.5000i 0.5000 + 0.5000i
x=[x1,zeros(1,N-length(x1))] 0.5000 + 0.0000i
k=0:N-1
n=k
powers=n.'*k;
disp('IDFT matrix for given is');
WNkn=(exp(j*2*pi/N)).^powers
disp('The N point IDFT of the given sequence
is')
X=1/N*WNkn*x.' % x is
sequence after zero padding
disp('The N point IDFT of the given sequence
using inbuilt function is')
X1=ifft(x1,N) % for
inbuilt use original sequence

figure
subplot(2,1,1);
stem(0:length(x1)-1,x1);
grid on;
xlabel('---->n');
ylabel('---->amplitude');
title('Input sequence x(n)')

subplot(2,1,2);
stem(0:length(X1)-1,X1);
grid on;
xlabel('---->n');
ylabel('---->amplitude');
title('IDFT sequence X(n)')

OUTPUT:

IDFT matrix for given is

WNkn =

1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i


1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 + 1.0000i -1.0000 + 0.0000i -
0.0000 - 1.0000i
1.0000 + 0.0000i -1.0000 + 0.0000i 1.0000 - 0.0000i -
1.0000 + 0.0000i
1.0000 + 0.0000i -0.0000 - 1.0000i -1.0000 + 0.0000i
0.0000 + 1.0000i

The N point IDFT of the given sequence is

X=

-1.0000 + 0.0000i
1.0000 - 0.5000i
0.5000 + 0.5000i
0.5000 - 0.0000i
//C Program To find N point DFT of a given
sequence XMag[k]=sqrt(pow(XReal[k],2)+pow(XImag[k]
#define CHIP_6713 1 ,2)); //magnitude
#include "dsk6713.h" XPhase[k]=atan2(XImag[k],XReal[k]);
#include "dsk6713_aic23.h" //phase
#include "stdio.h" }
#include "stdlib.h"
#include "math.h" //Printing phase and Magnitude values
#define pi 3.141592653589793 printf("The magnitude and phase response
float *x, *XReal, *XImag, *XMag, *XPhase; of DFT are\n");
void main() for(k=0;k<N;k++)
{
int i, n , k, N, lx; printf("XMag[%d]=%f\tXPhase[%d]=%f\n",k,X
Mag[k],k,XPhase[k]);
//Reading length of input sequence } //end of main
printf("\nEnter the length of input
sequence\n");
scanf("%d",&lx);

//Read the value of N


printf("\nEnter the N point DFT
desired\n");
scanf("%d",&N);
x=(float*)malloc(N*sizeof(float));

//Dynamic memory allocation


XReal=(float*)malloc(N*sizeof(float));
XImag=(float*)malloc(N*sizeof(float));
XMag=(float*)malloc(N*sizeof(float));
XPhase=(float*)malloc(N*sizeof(float));

//Reading input sequence


printf("\nEnter the input sequence \n");
for(i=0;i<lx;i++)
scanf("\n%f\n",&x[i]);

//padding of zeros to input sequence


for(i=lx;i<N;i++)
x[i]=0;

//printing input sequence after padding


zeros
for(i=0;i<N;i++)
printf("\n%f\n",x[i]);

//Computing DFT
for(k=0;k<N;k++)
{
XReal[k]=0;
XImag[k]=0;
for(n=0;n<N;n++)
{

XReal[k]+=x[n]*cos(2*pi/N*k*n);
//real part
XImag[k]-
=x[n]*sin(2*pi/N*k*n); //imaginary
part
}
}

//Printing output
printf("\nThe DFT computed is\n");
for(k=0;k<n;k++)

printf("(%f)+1j*(%f)\n",XReal[k],XImag[k]
);
for(k=0;k<n;k++)
{
convolution
PROPERTIES OF LINEAR CONVOLUTION:
MATLAB CODE: a1 = 5 16 34 60 95 100 94 76
45
clc
clear all a2 =5 16 34 60 95 100 94 76
close all 45
x=input('enter the frist sequence x(n)=')
h=input('enter the second sequence h(n)=') a3 = Columns 1 through 8
d=input('enter the third sequence d(n)=') 5 26 86 232
lx=length(x) 494 850 1256 1604
lh=length(h)
ld=length(d)
Columns 9 through 13
1658 1446 1070 588
if ((ld==lx) & (lh==lx))
135
disp('testing all the proprieties of linear
convolution')
a4 =Columns 1 through 8
a1=conv(x,h) 5 26 86 232
a2=conv(h,x) 494 850 1256 1604
a3=conv(conv(x,h),d) Columns 9 through 13
a4=conv(x,conv(h,d)) 1658 1446 1070 588
a5=conv(x,(h+d)) 135
a6=conv(x,h)+conv(x,d)
a5 =6 20 45 86 139 156 155 128
if (a1==a2) 60
disp('commutative property is
satisfied!!'); a6 =6 20 45 86 139 156 155 128
else 60
disp('commutative property is not
satisfied!!'); commutative property is satisfied!!
end associative property is satisfied!!
if (a3==a4) distributive property is satisfied!!
disp('associative property is
satisfied!!');
else
disp('associative property is not
satisfied!!');
end
if (a5==a6)
disp('distributive property is
satisfied!!');
else
disp('distributive property is not
satisfied!!');
end
else disp('dimensional error of input
sequence');
end

OUTPUT

enter the frist sequence x(n)=[1 2 3 4 5]

x =1 2 3 4 5

enter the second sequence h(n)=[5 6 7 8 9]

h =5 6 7 8 9

enter the third sequence d(n)=[1 2 4 8 3]

d =1 2 4 8 3

lx =5
lh =5
ld =5

testing all the proprieties of linear


LINEARITY PROPERTY: 1.0e+02 *
MATLAB CODE:
clc 1.2800 + 0.0000i -0.1600 + 0.2000i
clear all 0.0800 + 0.0000i -0.1600 - 0.2000i
close all
1.0e+02 *
x1=input('enter the sequence of x(n):')
x2=input('enter the sequence of x2(n):') 1.2800 + 0.0000i -0.1600 + 0.2000i
a=input('enter the value of a:') 0.0800 + 0.0000i -0.1600 - 0.2000i
b=input('enter the value of b:')
N=input('enter the value of N:')
lx1=length(x1);
lx2=length(x2);
x11=[x1,zeros(1,(N-lx1))];
x22=[x2,zeros(1,(N-lx2))];
X1=fft(x1,N)
X2=fft(x2,N)

y1=a*x11+b*x22
lhs=fft(y1,N)
rhs=((a.*X1)+(b.*X2))

disp(lhs)
disp(rhs)

OUTPUT:

enter the sequence of x(n):[1 2 3 4 5]

x1 = 1 2 3 4 5

enter the sequence of x2(n):[5 2 6 4 8]

x2 =5 2 6 4 8

enter the value of a:6

a =6

enter the value of b:4

b = 4

enter the value of N:4

N =4

X1 =10.0000 + 0.0000i -2.0000 + 2.0000i -


2.0000 + 0.0000i -2.0000 - 2.0000i

X2 =17.0000 + 0.0000i -1.0000 + 2.0000i


5.0000 + 0.0000i -1.0000 - 2.0000i

y1 =26 20 42 40 62

lhs =1.0e+02 *

1.2800 + 0.0000i -0.1600 + 0.2000i


0.0800 + 0.0000i -0.1600 - 0.2000i

rhs =1.0e+02 *

1.2800 + 0.0000i -0.1600 + 0.2000i


0.0800 + 0.0000i -0.1600 - 0.2000i
TIME SHIFT PROPERTY: CIRCULAR FREQUENCY SHIFT PROPERTY:
MATLAB CODE: MATLAB CODE:

clc
close all clc
clear all close all
clear all
x=input('enter the sequence of x(n):') x=input('enter tHe sequence of x(n):')
L=input('enter the value of L:')
L=input('enter tHe value of L:')
N=input('enter the value of N:')
x1=[x,zeros(1,N-length(x))]; N=input('enter tHe value of N:')
X=fft(x1,N) n=0:N-1;
k=0:N-1; X=fft(x,N);
w=exp(-i*2*pi*k*L/N);
LHS1=circshift(x1,L,2) lx=length(x);
y1=w.*X; w=exp(2*pi*n*L/N);
RHS1=ifft(y1,N)
x1=[x,zeros(1,(N-lx))];
y2=fft(circshift(x1,L,2),N);
lhs=fft(w.*x1,N)
LHS2=y2 rhs=circshift(X,L,2)
RHS2=y1

OUTPUT: OUTPUT:
enter tHe sequence of x(n):[1 2 3 4]
enter the sequence of x(n):[1 2 3 4 5]
x =1 2 3 4
x=1 2 3 4 5
enter tHe value of L:4
enter the value of L:5
enter tHe value of N:4
enter the value of N:4
lhs =1.0e+08 *
X =10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i
6.1507 + 0.0000i -0.0086 + 6.1421i -
-2.0000 - 2.0000i 6.1335 + 0.0000i -0.0086 - 6.1421i
LHS1 = 1 2 3 4 5
rhs =10.0000 + 0.0000i -2.0000 + 2.0000i -
RHS1 =4.0000 + 0.0000i 1.0000 - 0.0000i 2.0000 - 0.0000i 2.0000 + 0.0000i -2.0000 - 2.0000i
3.0000 + 0.0000i

LHS2 = 10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -


2.0000 - 2.0000i

RHS2 =10.0000 + 0.0000i 2.0000 + 2.0000i 2.0000 + 0.0000i


2.0000 - 2.0000i
CIRCULAR CONVOLUTION: N=max(lx,lh);
xx=[x zeros(N-lx)];
MATLAB CODE: HH=[h zeros(N-lh)];
W=zeros(N,N);
clc; clear all; close all; for n=0:N-1
x1=input('Enter the first sequence for k=0:N-1
x1(n)='); W(n+1,k+1)=exp(-i*2*pi*n*k/N);
x2=input('Enter the second sequence end;
x2(n)='); end;
lx1=length(x1); X=W*xx.';
lx2=length(x2); H=W*HH.';
N=max(lx1,lx2); disp('DFT of 1st sequence is');
if(N==lx1) disp(X.');
x2=[x2,zeros(1,N-lx2)]; disp('DFT of 2nd sequence is');
else disp(H.');
x1=[x1,zeros(1,N-lx1)]; figure
end subplot(2,1,1);
x11=x1; stem(x);
x22=x2; title('1st i/p sequence');
x1=x1'; subplot(2,1,2);
x2=x2'; compass(X);
A=x2; title('dft of 1st sequence');
for i=1:N-1 figure
w=[A(N,i);x2(1:N-1)]; subplot(2,1,1);
x2=w; stem(h);
A=[A,w]; title('2nd i/p sequence');
end subplot(2,1,2);
y=A*x1 compass(H);
y=cconv(x1',x2',N) title('dft of 2nd sequence');
YY=X.*H;
w=zeros(N,N);
for n=0:N-1
for k=0:N-1
OUTPUT: w(n+1,k+1)=exp(i*2*pi*n*k/N);
end;
Enter the first sequence x1(n)=[2 4 6 8]
end;
Enter the second sequence x2(n)=[1 3 5 7] B=w*YY;
Y=B/N;
y = 84 disp('Dft of o/p sequence is');
disp(YY.');
92 disp('IDFT of o/p sequence is');
disp(Y.');
84 figure
subplot(2,1,1)
60 title('o/p DFT sequence of result');
subplot(2,1,2);
y =92 84 60 84 title('circular convoluted o/p');
CIRCULAR CONVOLUTION USING DFT AND IDFT
OUTPUT:
METHOD:
enter the value of first input sequence[2 4
MATLAB CODE:
6 8]
close all;
enter the values of 2nd dft sequence[1 3 5
clear all;
7]
clc;
x=input('enter the value of first input DFT of 1st sequence is
sequence');
disp('1st i/p sequence is'); 20.0000 + 0.0000i -4.0000 + 4.0000i -
disp(x); 4.0000 - 0.0000i -4.0000 - 4.0000i
h=input('enter the values of 2nd dft
sequence');
disp('2nd i/p sequence is');
disp(h); DFT of 2nd sequence is
lx=length(x);
lh=length(h);
16.0000 + 0.0000i -4.0000 + 4.0000i -
4.0000 - 0.0000i -4.0000 - 4.0000i

Dft of o/p sequence is

1.0e+02 *

3.2000 + 0.0000i 0.0000 - 0.3200i


0.1600 + 0.0000i -0.0000 + 0.3200i

IDFT of o/p sequence is

84.0000 + 0.0000i 92.0000 + 0.0000i


84.0000 + 0.0000i 60.0000 - 0.0000i

Você também pode gostar