Showing posts with label IDFT. Show all posts
Showing posts with label IDFT. Show all posts

Impulse Response, Step Response, DFT, IDFT, DTFT, IDTFT MATLAB code

%impulse and step response%
sys=tf([1],[1 4 3]);
subplot(2,1,1); step(sys);
ylabel('amplitude');
xlabel('time in seconds');
subplot(2,1,2); impulse(sys);
ylabel('amplitude');
xlabel('time in seconds');

%DFT%
x=input('enter the input matrix: ');
mul=(-2)*j*pi;
xlen=length(x);
for k=0:xlen-1
    y(1,k+1)=0;
    for n=0:xlen-1
        y(1,k+1)=y(1,k+1)+x(1,n+1)*exp(mul*n*k/xlen);
    end
end
nm=1:xlen;
subplot(2,1,1); stem(nm,real(y),'fill')
title('real part');
subplot(2,1,2); stem(nm,imag(y),'fill')
title('imaginary part');

%IDFT%
x=input('enter the input matrix');
mul=(2)*j*pi;
xlen=length(x);
for n=1:xlen-1
    y(1,n+1)=0;
    for k=1:xlen-1
        y(1,n+1)=y(1,n+1)+x(1,k+1)*exp(mul*n*k/xlen);
    end
    y(1,n+1)=y(1,n+1)/xlen;
end
nm=1:xlen;
subplot(2,1,1); stem(nm,real(y),'fill')
title('real part');
subplot(2,1,2); stem(nm,imag(y),'fill')
title('imaginary part');

%DFT and IDFT using matlab functions

x=input('Enter the sequence x(n)=');
N=input('Enter the length of the DFT N=');
X=fft(x)/N;
n=0:length(X)-1;
subplot(311);
stem(n,x);
title('Input Sequence');
subplot(323);
stem(n,X);
title('DFT');
subplot(324);
stem(n,abs(X));
title('Magnitude spectrum');
subplot(325);
stem(n,angle(X));
title('Phase spectrum');
xr=N*ifft(X);
subplot(326);
stem(n,abs(xr));
title('IDFT');

%DTFT and IDTFT%
 N=10;
w=0:0.01*pi:2*pi;
dtft=N.*sinc(w.*N./2./pi)./(sinc(w./2./pi)).*exp(-j.*w.*(N-1)./2);
subplot(2,1,1)
Mag=abs(dtft);
plot(w./pi,Mag);
subplot(2,1,2)
Pha=angle(dtft);
plot(w./pi,Pha);

Continue Reading

IDFT without using MATLAB function

%IDFT without using MATLAB function%
X=input('Enter the sequence');
N=input('Enter the length of the IDFT');
len=length(X);
if N>len
    X=[X zeros(1,N-len)];
elseif N<len
    X=X(1:N);
end
i=sqrt(-1);
w=exp(-i*2*pi/N);
n=0:(N-1);
k=0:(N-1);
nk=n'*k;
W=w.^(-nk);
x=(X*W)/N;
disp(x);
subplot(211);
stem(k,abs(x));
title('Magnitude Plot');
xlabel('N');
ylabel('Amplitude');
grid on;
subplot(212);
stem(k,angle(x));
title('Phase Plot');
xlabel('N');
ylabel('Phase Angle');
grid on;

Continue Reading