Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
This is a program in Matlab to do the DFT and the FFT (2-sided and 1-sided)
Good luck
%FFT lecture.
clc;close all; clear all;
%Generate the sine wave sequences
fs=8000; % sampling rate
N=1000; % Number of data points.
x=2*sin(2000*pi*[0:1:N-1]/fs) ;
figure(1) ;
stem( 1 : 1:N/10,x( 1 : 1 :N/10 ) ,'filled' ) ;%plot first 100 sample of x
%% Apply the DFT algorithm
figure(2) ;
xf=abs(fft(x) )/N;% compute the amplitude spectrum
P=xf.*xf;%compute the power spectrum
% Map the frequency bin to the frequency (hz)
f=[0:1:N-1]*fs/N;%fk=k fs/N where k=0,1,2,...N-1
% now we will plot the DFT spectrums
subplot(211);plot(f,xf);grid
xlabel('Frequency (Hz)');ylabel('Amplitude Spectrum (DFT) ' ) ;
subplot(212);plot(f,P);grid
xlabel('Frequency (Hz)');ylabel('Power Spectrum (DFT) ' ) ;
%% Convert it to one sided spectrum
figure(3);
xf(2:N)=2*xf(2:N);%Get the single side spectrum
P=xf.*xf;%compute the power spectrum
% Map the frequency bin to the frequency (hz)
f=[0:1:N/2]*fs/N;%frequency up to the folding frequency fs/2
% now we will plot the DFT spectrums
subplot(211);plot(f,xf(1:N/2+1));grid
xlabel('Frequency (Hz)');ylabel('Amplitude Spectrum (DFT)');
title('One Sided-Spectrum')
subplot(212);plot(f,P(1:N/2+1));grid
xlabel('Frequency (Hz)');ylabel('Power Spectrum (DFT)');
title('One Sided-Spectrum')
%% Zero Padding to the length of 1024
figure(4);
x=[x,zeros(1,24)];%24 zero to extend the sequence x from 1000 to 1024
N=length(x);
xf=abs(fft(x))/N; %compute the amplitude spectrum with zero padding
P=xf.*xf;%compute the power spectrum
% Map the frequency bin to the frequency (hz)
f=[0:1:N-1]*fs/N;%fk=k fs/N where k=0,1,2,...N-1
subplot(211);plot(f,xf);grid
xlabel('Frequency (Hz)');ylabel('Amplitude Spectrum (FFT)');
subplot(212);plot(f,P);grid
xlabel('Frequency (Hz)');ylabel('Power Spectrum (FFT)');
%% Convert it to one sided spectrum
figure(5);
xf(2:N)=2*xf(2:N);%Get the single side spectrum
P=xf.*xf;%compute the power spectrum
% Map the frequency bin to the frequency (hz)
f=[0:1:N/2]*fs/N;%frequency up to the folding frequency fs/2
% now we will plot the FFT spectrums
subplot(211);plot(f,xf(1:N/2+1));grid
xlabel('Frequency (Hz)');ylabel('Amplitude Spectrum (FFT)');
title('One Sided-Spectrum')
subplot(212);plot(f,P(1:N/2+1));grid
xlabel('Frequency (Hz)');ylabel('Power Spectrum (FFT)');
title('One Sided-Spectrum')
Thanks for reply , but in this program u r using FFT function , which I m nt supposed to use , I wanna to write program which use mathematcial formula of converting the Time domain signal into frequency domain using matlab.
I found you post very useful, and infact it is not difficult to understand what you mean. However, I need one more help. I have a data in frequency-space domain and I want to transform it to frequency wavenumber domian by writing the fourier transform for the space domain part of the data. I am encountering problem with it, do you have further idea?
The Pseudo code is like this
close all; clear all; clc;
Dat = load('filename'); % dat is in t-x domain;
Datw = fft(Dat); % Datw is in w-x domain
Now I want to write a code that transforms the space part from space to wavenumber leaving the data in w-k domain. You can reply in Portugese I can translate it in google.
hi, Adraino.
i have one question here . i need help very much,..please help me...
Understanding the DFT by writing programs using Matlab. Used the Matlab program to
a) write a FFT program.
b) write a DFT program.
c) Compare the processing speed of FFT to DFT.
d) Compare the processing speed of the FFT program you write with the FFT program available in matlab. Which one is faster? Give the reasons for it.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.