Pages

Monday, 7 October 2013

FFT and IFFT Without Using Function (Matlab 2013a)

Program :

clc;
clear all;
x = input('Enter the input sequence = ');
N = length(x);
for k = 1:N
y(k) = 0;
for n = 1:N
y(k) = y(k)+x(n)*exp(-1i*2*pi*(k-1)*(n-1)/N);
end
end
%code block to plot the input sequence
t = 0:N-1;
subplot(2,2,1);
stem(t,x);
ylabel('Amplitude ---->');
xlabel('n ---->');
title('Input Sequence');
grid on;
magnitude = abs(y); % Find the magnitudes of individual FFT points
disp('FFT Sequence = ');
disp(magnitude);
%code block to plot the FFT sequence
t = 0:N-1;
subplot(2,2,2);
stem(t,magnitude);
ylabel('Amplitude ---->');
xlabel('K ---->');
title('FFT Sequence');
grid on;
R = length(y);
for n = 1:R
x1(n) = 0;
for k = 1:R
    x1(n) = x1(n)+(1/R)*y(k)*exp(1i*2*pi*(k-1)*(n-1)/R);
end
end
%code block to plot the IFFT sequence
t = 0:R-1;
subplot(2,2,3);
stem(t,x1);
disp('IFFT Sequence = ');
disp(x1);
ylabel('Amplitude ---->');
xlabel('n ---->');
title('IFFT sequence');
grid on;


Output :

Enter the input sequence = [1 4 2 5 2]

FFT Sequence =
   14.0000    2.8124    4.3692    4.3692    2.8124

IFFT Sequence =
  Columns 1 through 4

   1.0000 - 0.0000i   4.0000 - 0.0000i   2.0000 - 0.0000i   5.0000 + 0.0000i

  Column 5

   2.0000 + 0.0000i


Waveforms:

  

Sunday, 6 October 2013

DCT and IDCT Without Using Function (Matlab 2013a)

Program:

clc
clear all
x = input('Enter the sequence = ');
l = length(x);
for k = 1:l
    y(k) = 0;
    if (k==1)
        w(k) = 1/sqrt(l);
    else
        w(k) = sqrt(2/l);
    end       
    for n = 1:l
        y(k) = y(k)+x(n)*cos(pi*(2*n-1)*(k-1)/(2*l));
    end
    y(k) = y(k)*w(k);
end
t = 0:l-1;
subplot(2,2,1);
stem(t,x);
ylabel('Amplitude ---->');
xlabel('n ---->');
title('Input Sequence');
grid on;
magnitude = abs(y); % Find the magnitudes of individual DCT points
disp('DCT Sequence = ');
disp(magnitude);
%code block to plot the DCT sequence
t = 0:l-1;
subplot(2,2,2);
stem(t,magnitude);
ylabel('Amplitude ---->');
xlabel('K ---->');
title('DCT Sequence');
grid on;
phase = angle(y); % Find the phases of individual DCT points
disp('Phase = ');
disp(phase);
%code block to plot the phase response
t = 0:l-1;
subplot(2,2,3);
stem(t,phase);
ylabel('Phase ---->');
xlabel('K ---->');
title('Phase Response');
grid on;
for n = 1:l
    X(n) = 0;
    if (n==1)
        w(n) = 1/sqrt(l);
    else
        w(n) = sqrt(2/l);
    end        
    for k = 1:l
        X(n) = X(n)+w(k)*y(k)*cos(pi*(2*n-1)*(k-1)/(2*l));
    end
end
t = 0:l-1;
subplot(2,2,4);
stem(t,X);
disp('IDCT Sequence = ');
disp(X);
ylabel('Amplitude ---->');
xlabel('n ---->');
title('IDCT sequence');
grid on;


Output:

Enter the sequence = [2 4 3 1 2]
DCT Sequence =
    5.3666    1.1152    0.8279    1.8045    0.1208

Phase =
         0         0    3.1416    3.1416         0

IDCT Sequence =
    2.0000    4.0000    3.0000    1.0000    2.0000


Waveforms: