MATLAB program to find DFT without using Matlab function

Progam Code
% DFT program without function
clc;
close all;
clear all;
x=input('Enter the sequence x= ');
N=input('Enter the length of the DFT N= ');
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;
disp(X);
subplot(211);
stem(k,abs(X));
title('Magnitude Spectrum');
xlabel('Discrete frequency');
ylabel('Amplitude');
grid on;
subplot(212);
stem(k,angle(X));
title('Phase Spectrum');
xlabel('Discrete frequency');
ylabel('Phase Angle');
grid on;



Example of output

Enter the sequence x= [4 5 6 9]
Enter the length of the DFT N= 7
  Columns 1 through 4 

  24.0000            -2.3264 -13.6637i                    3.0930 + 4.7651i   
1.2334 - 6.2528i

  Columns 5 through 7 


 1.2334 + 6.2528i            3.0930 - 4.7651i            -2.3264 +13.6637i
____________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .


14 comments:

  1. could it be explained with an example?

    ReplyDelete
    Replies
    1. Respected Sir,
      As per your opinion I have included an example. Thank you for visiting this blog. Wishing you all success in life.

      Delete
  2. how can i do dft on sinusoidal fuction or ex: X=4-n etc

    ReplyDelete
    Replies
    1. Respected Sir,
      As per your opinion I have written a MATLAB program to do dft on sinusoidal function. The program to do dft on sinusoidal function can be accessed at http://s5electronicsandcommunication.blogspot.com/2016/01/home.html

      Thank you very much for spending your precious time to visit my humble weblog. Thank you very much for asking me this question and thus allowing me to serve you. I am greatly obliged to you.

      Wish you and your friends and family all happiness and good health. Wish you all success in life and career.

      Delete
  3. how can it be done with a 4 point output?

    ReplyDelete
    Replies
    1. Respected Sir,
      Will you please make clear what you mean by 4 point output?
      It is my pleasure to answer your question after that.

      Thank you very much for spending your precious time to visit my humble weblog. Thank you very much for asking me this question
      Wish you and your friends and family all happiness and good health. Wish you all success in life and career.
      Please reply, Sir

      Delete
  4. is it possible to get a MATLAB code to compute Discrete Fourier Transform and Iverse Discrete Fourier Transform of a given sequence using FFT (Fast Fourier Transform) Algorithms (DIT-FFT & DIF-FFT)

    ReplyDelete
  5. Yes, Respected Sir.
    I have written a MATLAB program code to compute Discrete Fourier Transform and Inverse Discrete Fourier Transform of a given sequence using FFT (fast fourier transform) algorithms.

    You can get the program code by copy pasting the following link below on your browser.
    http://s5electronicsandcommunication.blogspot.com/2012/11/matlab-program-to-find-dft-and-idft.html

    Thank you very much for asking me this question. Thank you for allowing me to serve you. I am always at your service. Thank you for spending your valuable time to visit my simple weblog. I am greatly obliged to you.

    Wish you and your family and all your friends good health, happiness and success. :)

    ReplyDelete
  6. Instead of inputting a sequence of X values, what if I already have an array of them. How would I call the array?

    ReplyDelete
    Replies
    1. Respected Sir / madam,
      If you already have an array of values, then you should replace the sentence
      x=input('Enter the sequence x= ');
      in the above program code with the sentence
      x=[value_1 value_2 value_3 value_4 value_5 .... value_n];
      The words value_1, value_2, Value_3, value_n, e.t.c should be replaced with the values in the array you already have.
      For example if your array is [21 1 11 7 7 7],
      then you should replace the sentence
      x=input('Enter the sequence x= ');
      with the sentence
      x=[21 1 11 7 7 7];

      If your array is [19 5 1],
      then you should replace the sentence
      x=input('Enter the sequence x= ');
      with the sentence
      x=[19 5 1];

      This is the only change that has to be made in the above program code. The rest of the program code is the same. I am looking forward to knowing your opinion about this. So please reply.
      Thank you for asking me this question and thus giving me an opportunity to serve you.

      May you, your family and all your friends be healthy and happy.
      -Anju K

      Delete
  7. What a MATLAB program to calculate the 8-Point DFT of sequence x[n] ={1,2,3,2,1,3,4,1}. And also how to Plot its Magnitude and Phase.

    Please answer as soon as possible.

    ReplyDelete
  8. What is MATLAB program to calculate the 8-Point DFT of sequence x[n] ={1,2,3,2,1,3,4,1}. Plot its Magnitude and Phase.

    ReplyDelete
    Replies
    1. Respected Sir / Madam,
      I am the author of all the programs in this blog.
      The matlab program given at the top of this page can be used to calculate the 8-point DFT of the sequence x[n] = {1,2,3,2,1,3,4,1}

      For your convenience, I am typing the program once again below.

      % DFT program without function
      clc;
      close all;
      clear all;
      x=input('Enter the sequence x= ');
      N=input('Enter the length of the DFT N= ');
      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;
      disp(X);
      subplot(211);
      stem(k,abs(X));
      title('Magnitude Spectrum');
      xlabel('Discrete frequency');
      ylabel('Amplitude');
      grid on;
      subplot(212);
      stem(k,angle(X));
      title('Phase Spectrum');
      xlabel('Discrete frequency');
      ylabel('Phase Angle');
      grid on;

      When the program asks you to enter the sequence, you should input [1 2 3 2 1 3 4 1]
      When the program asks you the length of the DFT, you should input 8
      Then the output will be displayed.
      I have uploaded the output at https://s5electronicsandcommunication.blogspot.com/p/enter-sequence-x-1-2-3-2-1-3-4-1-enter.html .
      You can see the output by copying the link https://s5electronicsandcommunication.blogspot.com/p/enter-sequence-x-1-2-3-2-1-3-4-1-enter.html and pasting the link to your browser.
      The output contains DFT of the given sequence, its magnitude plot and phase plot.

      Kindly let me know your opinion after reading the output.
      May you, your relatives and your friends be always safe, happy and healthy.
      I saw that all of your blogs are about music. Are you a musician?
      -Anju K

      Delete
  9. What is the MATLAB program to calculate the 8-Point DFT of sequence x[n] ={1,2,0,0,1,2,0,0} ? Plot its Magnitude and Phase. Thanks a lot.

    ReplyDelete

Please write your opinion about this MATLAB program here, only in English.