You are on page 1of 5

1.

%DFT using mathematical expression


clc;
clear all;
close all;
x=input('enter the number of samples');
N=input('enter the point representation');
subplot(2,3,1)
stem(x);
title('input signal');
xlabel('n--->');
ylabel('amplitude');
n=[0:N-1];
k=[0:N-1];
wn=exp(-1j*2*pi/N);
nk=n'*k;
w=wn.^nk;
xk=x*w;
disp('DFT of X');
disp(xk);
w=0:0.1:pi;
[h,om]=freqz(xk,1,w);
m=abs(h);
an=angle(h);
subplot(2,3,2);
plot(om/pi,20*log(m));
title('magnitude response of DFT');
xlabel('normalized frequency');
ylabel('gain in db');
subplot(2,3,3);
plot(om/pi,(an));
title('phase response of DFT');
xlabel('normalized frequency');
ylabel('phase');
yn=exp(1j*2*pi/N);
nk=n'*k;
w=yn.^nk;
y=1/N*(xk*w);
subplot(2,3,4);
stem(abs(y));

%FFT inbuilt function


y=fft(x,N);
disp(FFT of X);
disp(y);
w=0:0.1:pi;
[h,om]=freqz(y,1,w);
m=abs(h);
an=angle(h);
figure(2);
subplot(1,3,1);
plot(om/pi,20*log(m));
title('magnitude response of FFT');
xlabel('normalized frequency');
ylabel(gain in db');
subplot(1,3,2);
plot(om/pi,(an));
title('phase response of FFT');
xlabel('normalized frequency');
ylabel('phase');
z=ifft(y,N);
subplot(1,3,3);
stem(z);
title(' IFFT');
xlabel('n--->');
ylabel('amplitude');

2. PROGRAMS:

FREQUENCY RESPONSE
clear all;
close all;
clc;
b=input('enter the numerator coefficients');

a=input('enter the denominator coefficients');


w=0:.1:pi;
[h,om]=freqz(b,a,w);
m=20*log(abs(h));
an=angle(h);
subplot(2,1,1);
plot(m,om/pi);
title('magnitude response');
xlabel('normalised frequency');
ylabel('magnitude');
subplot(2,1,2);
plot(an,om/pi);
title('phase response');

DFT COMPUTATION
clc;
close all;
clear all;
x1=input('enter the seq');
N1=length(x1);
for i=0:1:N1-1
for j=0:1:N1-1
Wn(i+1,j+1)=exp(((-1)*sqrt(-1)*2*pi*i*j)/N1);
end
end
xx=x1*Wn;
disp('x(k)');
disp(xx);
y=abs(xx);
o=angle(xx);
n=0:1:N1-1;
subplot(2,2,1);
plot(n,y);
xlabel('k---->');
ylabel('x(k)---->');
title('magnitude plot for dft');
subplot(2,2,2);
plot(n,o);
xlabel('k---->');
ylabel('x(k)---->');
title('phase plot for dft');
n=0:1:N1-1;
for i=0:1:N1-1

for j=0:1:N1-1
Wn(i+1,j+1)=exp(((1)*sqrt(-1)*2*pi*i*j)/N1);
end
end
yx=xx*Wn./N1;
disp('y(n)');
disp(abs(yx));
ya=abs(yx);
subplot(2,2,3);
plot(n,ya);
xlabel('n---->');
ylabel('x(n)---->');
title('magnitude plot for idft');

FFT COMPUTATION
clc;
clear all;
close all;
N=input('enter the length of FFT');
x=input('enter the input signal');
for i=0:1
v11(i+1)=x(4*i+1);
v12(i+1)=x(4*i+3);
v21(i+1)=x(4*i+2);
v22(i+1)=x(4*i+4);
end
v11k=dft(v11,N/4);
v12k=dft(v12,N/4);
v21k=dft(v21,N/4);
v22k=dft(v22,N/4);
N1=N/2;
for n=0:1:N/4-1
Wn1(n+1)=exp(((-1)*sqrt(-1)*2*pi*n)/N1);
end
v12kk =v12k.*Wn1;
F1k=v11k+v12kk;
F1nk=v11k-v12kk;
F1=[F1k F1nk];
v22kk =v22k.*Wn1;
F2k=v21k+v22kk;
F2nk=v21k-v22kk;
F2=[F2k F2nk];
for n=0:1:N/2-1
Wn1(n+1)=exp(((-1)*sqrt(-1)*2*pi*n)/N);
end
F2w =F2.*Wn1;
Xk=F1+F2w;
Xkn=F1-F2w;
XK=[Xk Xkn];
n=0:N-1;

stem(n,abs(XK));
figure(2);
an=angle(XK);
stem(n,an);

FREQUENCY SPECTRUM
N=256; % Total Number of Samples
fs=8000; % Sampling frequency
f=1000;
n=0:N-1;
% Now generate the sinusoidal signal
x=sin(2*pi*(f/fs)*n);
% Estimate its spectrum using fft command
X=fft(x);
magX=abs(X);
% Build up an appropriate frequency axis
fx=0:(N/2)-1; % first make a vector for f=0,1,2,...(N/2)-1
fx=(fx*fs)/N; % Now scale it so that it represents
frequencies in Hz
figure(1);
subplot(1,1,1);
plot(fx,20*log10(magX(1:N/2)));
grid;
title('Spectrum of a sinusoidal signal with f=1KHz');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');

You might also like